Skip to content

Commit

Permalink
chat schema and apis are added and updated
Browse files Browse the repository at this point in the history
  • Loading branch information
naqi committed Oct 2, 2023
1 parent 68578f9 commit 1900a7a
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 5 deletions.
30 changes: 30 additions & 0 deletions app/api/chat/init/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from "next/server"
import { initialChatMessage } from "@/utils/chat"

import { createPrisma } from "@/lib/prisma"

export async function POST(request: Request) {
const body = await request.json()
const { childId, childContext } = body

const credentials = {
supabaseDatabaseUrl: process.env.DATABASE_URL,
}

const childInitialContextMessage = [
{
name: initialChatMessage.name,
text: `${initialChatMessage.text} ${childContext}`,
},
]
const prisma = createPrisma({ url: credentials.supabaseDatabaseUrl })
const data = await prisma.chatHistory.create({
data: {
childId: childId,
messages: childInitialContextMessage,
},
})

return NextResponse.json({ data })
}

58 changes: 53 additions & 5 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { NextRequest, NextResponse } from "next/server"
import { Prisma } from "@prisma/client"

import { getChain } from "@/lib/langchain/chain"
import { ModelHandler } from "@/lib/langchain/model"
import { getPineconeStore } from "@/lib/langchain/vectorstores/pinecone"
import { NextRequest, NextResponse } from "next/server"
import { createPrisma } from "@/lib/prisma"

export const runtime = "edge"
// export const runtime = "edge"

export async function POST(request: NextRequest) {
const body = await request.json()
Expand All @@ -17,7 +20,7 @@ export async function POST(request: NextRequest) {
supabaseUrl: process.env.SUPABASE_URL,
supabaseBucket: process.env.SUPABASE_BUCKET,
supabaseDatabaseUrl: process.env.DATABASE_URL,
supabaseDirectUrl: process.env.DIRECT_URL
supabaseDirectUrl: process.env.DIRECT_URL,
}
if (
!credentials ||
Expand All @@ -28,7 +31,22 @@ export async function POST(request: NextRequest) {
return NextResponse.redirect("/credentials")
}

const { prompt, messages: history } = body
const { prompt, chatId } = body

//Get history from supabase against child id
const prisma = createPrisma({ url: credentials.supabaseDatabaseUrl })
const historyFromDB = await prisma.chatHistory.findFirst({
where: {
id: chatId,
},
})

//Construct an array of message history to send it to model
let messageHistory = []
const chatMessages = historyFromDB.messages as Prisma.JsonArray
chatMessages.map((message) => {
messageHistory.push(message)
})
// OpenAI recommends replacing newlines with spaces for best results
const sanitizedQuestion = `${prompt.trim().replaceAll("\n", " ")}`

Expand All @@ -41,7 +59,37 @@ export async function POST(request: NextRequest) {
const modelHandler = new ModelHandler(writer)
const model = modelHandler.getModel(credentials.openaiApiKey)

const response = getChain(model, vectorStore, sanitizedQuestion, history)
const response = getChain(
model,
vectorStore,
sanitizedQuestion,
messageHistory
)

// Push current prompt to message history array
messageHistory.push({
name: "human",
text: prompt,
})

//Resolve the promise returned by langchain
Promise.resolve(response).then(async (res) => {
//Push response to message history array
messageHistory.push({
name: "ai",
text: res.text,
})

//Update message history array in table against chatId
await prisma.chatHistory.update({
where: {
id: chatId,
},
data: {
messages: messageHistory,
},
})
})

return new NextResponse(stream.readable, {
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- CreateTable
CREATE TABLE "Documents" (
"id" TEXT NOT NULL,
"name" TEXT,
"url" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Documents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ChatHistory" (
"id" TEXT NOT NULL,
"childId" TEXT,
"messages" JSONB NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "ChatHistory_pkey" PRIMARY KEY ("id")
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `title` to the `ChatHistory` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "ChatHistory" ADD COLUMN "title" TEXT NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- Made the column `childId` on table `ChatHistory` required. This step will fail if there are existing NULL values in that column.
*/
-- AlterTable
ALTER TABLE "ChatHistory" ALTER COLUMN "childId" SET NOT NULL,
ALTER COLUMN "title" DROP NOT NULL;
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
9 changes: 9 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ model Documents {
name String?
url String
created_at DateTime @default(now())
}

model ChatHistory {
id String @id @default(cuid())
childId String
title String?
messages Json
created_at DateTime @default(now())
updated_at DateTime @default(now())
}
4 changes: 4 additions & 0 deletions utils/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const initialChatMessage = {
name: "system",
text: "Act as an expert. Reply to questions about given data. Self reflect on your answers. Following is some of my child's information and you need to answer all my questions considering this context, ",
}

0 comments on commit 1900a7a

Please sign in to comment.