From da562ce3511d94b3a3edde3c8038fa36d35e8da4 Mon Sep 17 00:00:00 2001 From: krushnarout Date: Fri, 25 Oct 2024 21:20:04 +0530 Subject: [PATCH 1/2] feat: add auto generate the meaning title for the message --- frontend/lib/tools/auto.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/frontend/lib/tools/auto.ts b/frontend/lib/tools/auto.ts index 84c931f0..43e6f194 100644 --- a/frontend/lib/tools/auto.ts +++ b/frontend/lib/tools/auto.ts @@ -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[], @@ -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({ + 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}`, + }); + summaryTitle = `Summary of "${query}"`; + summaryText = text; + } catch (error) { + console.error('Error generating summary:', error); + } + let texts: TextSource[] = []; let images: ImageSource[] = []; let videos: VideoSource[] = []; @@ -173,6 +189,11 @@ export async function autoAnswer( await streamResponse({ videos: videos }, onStream); } + if (summaryText) { + await streamResponse({ sources: texts, title: summaryTitle }, onStream); + await streamResponse({ summary: summaryText }, onStream); + } + incSearchCount(userId).catch((error) => { console.error(`Failed to increment search count for user ${userId}:`, error); }); From d4332bf92b8aa636d82715235e16a12d5ea5bf0c Mon Sep 17 00:00:00 2001 From: krushnarout Date: Sat, 26 Oct 2024 09:01:19 +0530 Subject: [PATCH 2/2] fix: remove await & change prompt text --- frontend/lib/tools/auto.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/frontend/lib/tools/auto.ts b/frontend/lib/tools/auto.ts index 43e6f194..2e08420d 100644 --- a/frontend/lib/tools/auto.ts +++ b/frontend/lib/tools/auto.ts @@ -35,13 +35,14 @@ export async function autoAnswer( let summaryText = ''; try { - const { text } = await generateText({ + generateText({ 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}`, + system: 'You are a professional writer. Write simple, clear, and concise content.', + prompt: `Please give the following question a concise title: ${query}`, + }).then(({ text }) => { + summaryTitle = `Summary of "${query}"`; + summaryText = text; }); - summaryTitle = `Summary of "${query}"`; - summaryText = text; } catch (error) { console.error('Error generating summary:', error); }