From fb19021c91a2eb271bb32f9ac639cab32588a36e Mon Sep 17 00:00:00 2001 From: zaidmukaddam Date: Thu, 7 Nov 2024 15:31:58 +0530 Subject: [PATCH] fix: suggested question generation, switched from groq to gemini. --- app/actions.ts | 20 +++++++++++--------- app/search/page.tsx | 19 ++++++++----------- package.json | 2 +- pnpm-lock.yaml | 13 ++++++------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/app/actions.ts b/app/actions.ts index ecb8cf7..2822d33 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -1,21 +1,23 @@ 'use server'; import { generateObject } from 'ai'; -import { createOpenAI as createGroq } from '@ai-sdk/openai'; +import { google } from '@ai-sdk/google' import { z } from 'zod'; import { load } from 'cheerio'; -const groq = createGroq({ - baseURL: 'https://api.groq.com/openai/v1', - apiKey: process.env.GROQ_API_KEY, -}); - export async function suggestQuestions(history: any[]) { 'use server'; + console.log(history); + const { object } = await generateObject({ - model: groq('llama-3.2-90b-text-preview'), - temperature: 0, + model: google('gemini-1.5-pro-002',{ + structuredOutputs: true, + }), + temperature: 1, + maxTokens: 300, + topP: 0.95, + topK: 40, system: `You are a search engine query generator. You 'have' to create only '3' questions for the search engine based on the message history which has been provided to you. The questions should be open-ended and should encourage further discussion while maintaining the whole context. Limit it to 5-10 words per question. @@ -54,7 +56,7 @@ export async function generateSpeech(text: string, voice: 'alloy' | 'echo' | 'fa 'xi-api-key': ELEVENLABS_API_KEY, 'Content-Type': 'application/json', } - + const data = { text, model_id: 'eleven_turbo_v2_5', diff --git a/app/search/page.tsx b/app/search/page.tsx index 7602c24..8f89e3e 100644 --- a/app/search/page.tsx +++ b/app/search/page.tsx @@ -261,7 +261,7 @@ const HomeContent = () => { const initialQuery = searchParams.get('query') || ''; const initialModel = searchParams.get('model') || 'azure:gpt4o-mini'; - const [lastSubmittedQuery, setLastSubmittedQuery] = useState(initialQuery); + const lastSubmittedQueryRef = useRef(initialQuery); const [hasSubmitted, setHasSubmitted] = useState(!!initialQuery); const [selectedModel, setSelectedModel] = useState(initialModel); const bottomRef = useRef(null); @@ -284,7 +284,7 @@ const HomeContent = () => { onFinish: async (message, { finishReason }) => { console.log("[finish reason]:", finishReason); if (message.content && finishReason === 'stop' || finishReason === 'length') { - const newHistory = [...messages, { role: "user", content: lastSubmittedQuery }, { role: "assistant", content: message.content }]; + const newHistory = [...messages, { role: "user", content: lastSubmittedQueryRef.current }, { role: "assistant", content: message.content }]; const { questions } = await suggestQuestions(newHistory); setSuggestedQuestions(questions); } @@ -1264,33 +1264,30 @@ GPT-4o has been re-enabled! You can use it by selecting the model from the dropd } }, [messages, suggestedQuestions]); - const handleExampleClick = useCallback(async (card: typeof suggestionCards[number]) => { + const handleExampleClick = async (card: typeof suggestionCards[number]) => { const exampleText = card.text; track("search example", { query: exampleText }); - setLastSubmittedQuery(exampleText.trim()); + lastSubmittedQueryRef.current = exampleText; setHasSubmitted(true); setSuggestedQuestions([]); - + console.log('exampleText', exampleText); + console.log('lastSubmittedQuery', lastSubmittedQueryRef.current); await append({ content: exampleText.trim(), role: 'user', }); - - }, [append, setLastSubmittedQuery, setHasSubmitted, setSuggestedQuestions]); + }; const handleSuggestedQuestionClick = useCallback(async (question: string) => { setHasSubmitted(true); setSuggestedQuestions([]); - - setInput(question.trim()); await append({ content: question.trim(), role: 'user' }); - - }, [setInput, append]); + }, [append]); const handleMessageEdit = useCallback((index: number) => { setIsEditingMessage(true); diff --git a/package.json b/package.json index 3b357eb..87d76db 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "@ai-sdk/anthropic": "^0.0.55", "@ai-sdk/azure": "^0.0.51", "@ai-sdk/cohere": "latest", - "@ai-sdk/google": "^0.0.52", + "@ai-sdk/google": "^0.0.55", "@ai-sdk/groq": "^0.0.1", "@ai-sdk/mistral": "^0.0.41", "@ai-sdk/openai": "^0.0.58", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0834709..d93fb9b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,8 +15,8 @@ dependencies: specifier: latest version: 0.0.28(zod@3.23.8) '@ai-sdk/google': - specifier: ^0.0.52 - version: 0.0.52(zod@3.23.8) + specifier: ^0.0.55 + version: 0.0.55(zod@3.23.8) '@ai-sdk/groq': specifier: ^0.0.1 version: 0.0.1(zod@3.23.8) @@ -281,15 +281,14 @@ packages: zod: 3.23.8 dev: false - /@ai-sdk/google@0.0.52(zod@3.23.8): - resolution: {integrity: sha512-bfsA/1Ae0SQ6NfLwWKs5SU4MBwlzJjVhK6bTVBicYFjUxg9liK/W76P1Tq/qK9OlrODACz3i1STOIWsFPpIOuQ==} + /@ai-sdk/google@0.0.55(zod@3.23.8): + resolution: {integrity: sha512-dvEMS8Ex2H0OeuFBiT4Q1Kfrxi1ckjooy/PazNLjRQ3w9o9VQq4O24eMQGCuW1Z47qgMdXjhDzsH6qD0HOX6Cw==} engines: {node: '>=18'} peerDependencies: zod: ^3.0.0 dependencies: - '@ai-sdk/provider': 0.0.24 - '@ai-sdk/provider-utils': 1.0.20(zod@3.23.8) - json-schema: 0.4.0 + '@ai-sdk/provider': 0.0.26 + '@ai-sdk/provider-utils': 1.0.22(zod@3.23.8) zod: 3.23.8 dev: false