Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add auto generate the meaning title for the message using AI #101

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions frontend/lib/tools/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ImageSource, Message as StoreMessage, SearchCategory, TextSource, Video
import { streamText, tool } from 'ai';
import util from 'util';
import { z } from 'zod';
import { generateText } from 'ai';

export async function autoAnswer(
messages: StoreMessage[],
Expand All @@ -30,6 +31,21 @@ export async function autoAnswer(
const newMessages = getHistoryMessages(isPro, messages);
const query = newMessages[newMessages.length - 1].content;

let summaryTitle = '';
let summaryText = '';

try {
const { text } = await generateText({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for await, we should prioritize streamText, and we can await generateText after streamText ends.

model: getLLM('gpt-4o-mini'),
system: 'You are a professional writer. You write simple, clear, and concise content.',
prompt: `Summarize the following article in 3-5 sentences: ${query}`,
ahaapple marked this conversation as resolved.
Show resolved Hide resolved
});
summaryTitle = `Summary of "${query}"`;
summaryText = text;
} catch (error) {
console.error('Error generating summary:', error);
}

let texts: TextSource[] = [];
let images: ImageSource[] = [];
let videos: VideoSource[] = [];
Expand Down Expand Up @@ -173,6 +189,11 @@ export async function autoAnswer(
await streamResponse({ videos: videos }, onStream);
}

if (summaryText) {
await streamResponse({ sources: texts, title: summaryTitle }, onStream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should update the title in the UI

await streamResponse({ summary: summaryText }, onStream);
}

incSearchCount(userId).catch((error) => {
console.error(`Failed to increment search count for user ${userId}:`, error);
});
Expand Down
Loading