-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: package updates * chore: prisma chat thread and suggested articles * feat: new ui components * feat: context chat store * feat: vector store lib * feat: chat lib methods * feat: page and api updates * chore: tailwind css * chore: radix ui packages * feat: delete chat * chore: vercel ai * feat: chat completion * chore: package updates * feat: data migration script update * feat: chat lib updates * feat: metadata lib update * feat: page & component updates * feat: trigger dev script updates * feat: chat history keep previous data * chore: pulled prisma schema * chore: pinecone and llamaindex packages * feat: answear generation improvements * fix: missing schema * Revert "chore: pinecone and llamaindex packages" This reverts commit 3c56e49. * chore: pinecone package * feat: local chat history handling * fix: temporal weight * feat: extending prompt context * feat: links and performance * feat: demo chat component * fix: array reverse fix * fix: default prompt change
- Loading branch information
1 parent
bd38b86
commit e9259aa
Showing
77 changed files
with
4,720 additions
and
501 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { LlamaIndexAdapter, StreamData } from 'ai'; | ||
import { NextRouteFunction } from '@/lib/route-validator.server'; | ||
import * as logger from '@/lib/logger'; | ||
import { chatCompletionSchema } from './schema'; | ||
import * as threadService from '@/lib/chat/thread.server'; | ||
import * as chatService from '@/lib/chat/chat.server'; | ||
import * as queryService from '@/lib/chat/query.server'; | ||
import { chat_message_role } from '@prisma/client'; | ||
|
||
type Params = { params: { slug: string } }; | ||
|
||
const chatCompletion: NextRouteFunction<Params> = async (req, { params }) => { | ||
const reqJson = await req.json(); | ||
|
||
const parsedParams = chatCompletionSchema.parse(reqJson); | ||
|
||
const temporalAnalysis = await queryService.analyzeTemporalQuery(parsedParams.prompt); | ||
|
||
const thread = await threadService.getMessagesByThreadSlug(params.slug); | ||
|
||
const suggestions = await chatService.getDocumentSuggestions( | ||
thread.chat_message, | ||
parsedParams.prompt, | ||
temporalAnalysis | ||
); | ||
|
||
const stream = await chatService.chatCompletion(thread.chat_message, parsedParams.prompt, true, suggestions); | ||
|
||
const data = new StreamData(); | ||
|
||
suggestions.forEach((document) => { | ||
data.appendMessageAnnotation(JSON.stringify({ type: 'article_metadata', article_metadata: document })); | ||
}); | ||
|
||
return LlamaIndexAdapter.toDataStreamResponse(stream, { | ||
data, | ||
callbacks: { | ||
onCompletion: async (response) => { | ||
await threadService.createMessageWithSuggestions( | ||
params.slug, | ||
response, | ||
chat_message_role.ASSISTANT, | ||
suggestions | ||
); | ||
}, | ||
onFinal: async () => { | ||
await data.close(); | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
export const POST = chatCompletion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { escapeHtml } from '@/lib/utils'; | ||
import { z } from 'zod'; | ||
|
||
export const chatCompletionSchema = z.object({ | ||
prompt: z.string().transform(escapeHtml), | ||
}); | ||
|
||
export type ChatCompletion = z.infer<typeof chatCompletionSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NextRouteFunction } from '@/lib/route-validator.server'; | ||
import * as threadService from '@/lib/chat/thread.server'; | ||
import { createNewThreadSchema } from './schema'; | ||
|
||
type Params = { params: { prompt: string } }; | ||
|
||
const createNewThread: NextRouteFunction<{}, Params> = async (req) => { | ||
const reqJson = await req.json(); | ||
|
||
const parsedParams = createNewThreadSchema.parse(reqJson); | ||
|
||
const thread = await threadService.create(parsedParams.prompt); | ||
|
||
return Response.json({ data: thread }, { status: 200 }); | ||
}; | ||
|
||
export const POST = createNewThread; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { escapeHtml } from '@/lib/utils'; | ||
import { z } from 'zod'; | ||
|
||
export const createNewThreadSchema = z.object({ | ||
prompt: z.string().transform(escapeHtml), | ||
}); | ||
|
||
export type CreateNewThread = z.infer<typeof createNewThreadSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NextRouteFunction } from '@/lib/route-validator.server'; | ||
import * as threadService from '@/lib/chat/thread.server'; | ||
|
||
type Params = { params: { slug: string } }; | ||
|
||
const deleteThread: NextRouteFunction<Params> = async (_, { params }) => { | ||
try { | ||
await threadService.deleteThread(params.slug); | ||
} catch (error) { | ||
return Response.json({ status: 500, error }); | ||
} | ||
|
||
return Response.json({ status: 200 }); | ||
}; | ||
|
||
export const DELETE = deleteThread; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NextRouteFunction } from '@/lib/route-validator.server'; | ||
import * as chatService from '@/lib/chat/chat.server'; | ||
|
||
const getThreads: NextRouteFunction<{}> = async (req) => { | ||
const searchParams = req.nextUrl.searchParams; | ||
const limit = searchParams.get('limit'); | ||
|
||
try { | ||
const threads = await chatService.getChatHistory({ limit: limit ? parseInt(limit) : undefined }); | ||
|
||
return Response.json({ status: 200, data: threads }); | ||
} catch (error) { | ||
return Response.json({ status: 500, error }); | ||
} | ||
}; | ||
|
||
export const GET = getThreads; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default async function ArticlesLayout(props: { children: React.ReactNode }) { | ||
export default async function ArticlesLayout(props: Readonly<{ children: React.ReactNode }>) { | ||
return <>{props.children}</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
|
||
import { Label } from '@/components/ui/label'; | ||
import { MessageBubble } from '@/components/ui/message'; | ||
import { chat_message } from '@prisma/client'; | ||
import { useCompletion } from '@/hooks/use-completion'; | ||
import { CardSmall } from '@/components/article-metadata/card-small'; | ||
import { ChatThreadWithMessages } from '@/stores/chat-history'; | ||
import { ChatDemo } from './chat-demo'; | ||
|
||
type Props = { | ||
slug: string; | ||
thread: ChatThreadWithMessages; | ||
}; | ||
|
||
export const ChatCompletion: React.FC<Props> = ({ slug, thread }) => { | ||
const [trigger, { isLoading }] = useCompletion(slug); | ||
|
||
useEffect(() => { | ||
if (thread.chat_message.length === 1 && thread.chat_message[0].role === 'USER') { | ||
trigger({ prompt: thread.chat_message[0].content }); | ||
} | ||
}, []); | ||
|
||
const getMessageContent = (message: chat_message, isLast: boolean, isLoading: boolean) => { | ||
if (message.role === 'ASSISTANT') { | ||
return isLoading && isLast && !message.content ? '...' : message.content; | ||
} | ||
return message.content; | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="flex flex-col p-4 justify-between min-h-screen"> | ||
<div className="flex flex-col gap-4"> | ||
{thread.chat_message.map((message, index) => ( | ||
<MessageBubble variant={message.role === 'ASSISTANT' ? 'answer' : 'question'} key={message.id}> | ||
{getMessageContent(message, index === thread.chat_message.length - 1, isLoading)} | ||
</MessageBubble> | ||
))} | ||
</div> | ||
|
||
<ChatDemo /> | ||
</div> | ||
<div className="flex flex-col gap-4 p-4 max-w-[360px]"> | ||
<Label>Suggested Articles</Label> | ||
{thread.suggested_articles?.map(({ article_metadata: article }) => ( | ||
<CardSmall | ||
key={article.id} | ||
id={article.id} | ||
slug={article.slug} | ||
title={article.title} | ||
abstract={article.abstract} | ||
published={new Date(article.published).toDateString()} | ||
/> | ||
))} | ||
</div> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.