Skip to content

Commit

Permalink
feat: summarize chats (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored May 24, 2024
1 parent 78d4ea9 commit 13b99df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/MenuItem.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<NuxtLink
:to="to"
:title="title"
class="inline-flex items-center text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 duration-200 hover:bg-stone-200 hover:text-stone-900 h-9 rounded-md px-3 justify-start flex-grow gap-2 w-full"
>
<img v-if="img" :src="img" alt="icon" class="w-5 h-5 flex-shrink-0 rounded-md" />
Expand Down
15 changes: 11 additions & 4 deletions server/api/chats/[chat_id]/chat.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { formatDocumentsAsString } from 'langchain/util/document';
import { StringOutputParser } from '@langchain/core/output_parsers';
import { chatMessageSchema, chatSchema, repoSchema } from '~/server/schemas';
import { and, eq } from 'drizzle-orm';
import { TokenTextSplitter } from 'langchain/text_splitter';
import { HumanMessage, SystemMessage } from 'langchain/schema';

export default defineEventHandler(async (event) => {
const user = await requireUser(event);
Expand Down Expand Up @@ -134,10 +136,15 @@ export default defineEventHandler(async (event) => {
question: message,
});

if (messages.length === 2) {
const context = [`Repo: ${repo.name}`, `AI: ${messages[0].content}`, `User: ${message}`, `AI: ${result}`];
const summarize = (text: string) => 'Summary: ' + text; // TODO: Implement summarization using llm
const chatSummary = summarize(context.join('\n'));
// summarize the dialog when we got the second question from the user
if (messages.length >= 2 && chat.name.startsWith('Chat with')) {
const context = [
'Provide keywords or a short summary with maximal six words for the following dialog:\n',
...messages.map((m) => `${m.from}: ${m.content}`),
`user: ${message}`,
`ai: ${result}`,
];
const chatSummary = await model.invoke(context.join('\n'));
await db.update(chatSchema).set({ name: chatSummary }).where(eq(chatSchema.id, chat.id)).run();
}

Expand Down

0 comments on commit 13b99df

Please sign in to comment.