An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: centralize related questions model configuration
Yoshiki Miuradc135d4ab03e
diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.tsindex 5abc880f..b17d7fee 100644--- a/lib/agents/generate-related-questions.ts+++ b/lib/agents/generate-related-questions.ts@@ -1,9 +1,12 @@import { convertToModelMessages, generateObject, type UIMessage } from 'ai'import { z } from 'zod'+import { RELATED_QUESTIONS_MODEL_CONFIG } from '../config/model-types'import { getModel } from '../utils/registry'import { isTracingEnabled } from '../utils/telemetry'+import { RELATED_QUESTIONS_PROMPT } from './prompts/related-questions-prompt'+const relatedQuestionsSchema = z.object({questions: z.array(@@ -15,45 +18,23 @@ const relatedQuestionsSchema = z.object({})export async function generateRelatedQuestions(- model: string,messages: UIMessage[],abortSignal?: AbortSignal,parentTraceId?: string) {+ // Use the related questions model configuration+ const modelId = `${RELATED_QUESTIONS_MODEL_CONFIG.providerId}:${RELATED_QUESTIONS_MODEL_CONFIG.id}`+// Convert UIMessages to ModelMessagesconst modelMessages = convertToModelMessages(messages)- const systemPrompt = `You are a professional web researcher tasked with generating follow-up questions. Based on the conversation history and search results, create 3 DIFFERENT related questions that:--1. Explore NEW aspects not covered in the original query-2. Dig deeper into specific details mentioned in the search results-3. Connect to broader implications or related topics--Guidelines:-- NEVER repeat or rephrase the original question-- Each question should explore a UNIQUE angle or aspect-- Questions should build upon information found in the search results-- Use natural, conversational language-- Be specific and actionable--Example:-Original: "Why is Nvidia growing rapidly?"-Good follow-ups:-- "What specific AI technologies is Nvidia developing that competitors lack?"-- "How does Nvidia's data center revenue compare to its gaming division?"-- "Which companies are Nvidia's biggest customers for AI chips?"--Bad follow-ups (avoid these):-- "Why is Nvidia growing so fast?" (rephrases original)-- "Is Nvidia growing?" (less specific than original)-- "Tell me about Nvidia" (too general)`const { object } = await generateObject({- model: getModel(model),+ model: getModel(modelId),schema: relatedQuestionsSchema,schemaName: 'RelatedQuestions',schemaDescription:- 'Generate 3 unique follow-up questions that explore different aspects of the topic',- system: systemPrompt,+ 'Generate 3 concise follow-up questions (max 10-12 words each)',+ system: RELATED_QUESTIONS_PROMPT,messages: [...modelMessages,{@@ -67,7 +48,7 @@ Bad follow-ups (avoid these):isEnabled: isTracingEnabled(),functionId: 'related-questions',metadata: {- modelId: model,+ modelId,agentType: 'related-questions-generator',messageCount: messages.length,...(parentTraceId && {diff --git a/lib/agents/prompts/related-questions-prompt.ts b/lib/agents/prompts/related-questions-prompt.tsnew file mode 100644index 00000000..e3453911--- /dev/null+++ b/lib/agents/prompts/related-questions-prompt.ts@@ -0,0 +1,24 @@+export const RELATED_QUESTIONS_PROMPT = `You are a professional web researcher tasked with generating follow-up questions. Based on the conversation history and search results, create 3 CONCISE related questions that:++1. Explore NEW aspects not covered in the original query+2. Dig deeper into specific details from the search results+3. Connect to related topics or implications++Guidelines:+- Keep questions SHORT and CONCISE (max 10-12 words)+- NEVER repeat or rephrase the original question+- Each question should explore a UNIQUE angle+- Be specific and focused+- Use clear, simple language++Example:+Original: "Why is Nvidia growing rapidly?"+Good follow-ups:+- "What are Nvidia's main AI chip competitors?"+- "How much revenue comes from data centers?"+- "Which companies buy Nvidia's AI chips?"++Bad follow-ups (avoid these):+- "Besides Broadcom, which other specific companies are emerging as significant competitors..." (too long)+- "Why is Nvidia growing so fast?" (rephrases original)+- "Tell me about Nvidia" (too general)`diff --git a/lib/config/model-types.ts b/lib/config/model-types.tsindex eb5151ab..009f5f5d 100644--- a/lib/config/model-types.ts+++ b/lib/config/model-types.ts@@ -10,7 +10,7 @@ export const MODEL_TYPE_CONFIGS: Record<ModelType, Model> = {providerId: 'openai',providerOptions: {openai: {- reasoningEffort: 'medium',+ reasoningEffort: 'low',reasoningSummary: 'auto'}}@@ -28,6 +28,14 @@ export const MODEL_TYPE_CONFIGS: Record<ModelType, Model> = {}}+// Model configuration for related questions generation+export const RELATED_QUESTIONS_MODEL_CONFIG: Model = {+ id: 'gemini-2.0-flash',+ name: 'Gemini 2.0 Flash',+ provider: 'Google',+ providerId: 'google'+}+// Helper function to get model for a specific typeexport function getModelForType(type: ModelType): Model {return MODEL_TYPE_CONFIGS[type]diff --git a/lib/streaming/helpers/handle-stream-finish.ts b/lib/streaming/helpers/handle-stream-finish.tsindex cda5d898..a072d62c 100644--- a/lib/streaming/helpers/handle-stream-finish.ts+++ b/lib/streaming/helpers/handle-stream-finish.ts@@ -40,11 +40,7 @@ export async function handleStreamFinish(data: { status: 'loading' }})- // Use RELATED_QUESTION_MODEL env var if set, otherwise use the main model- const questionModel = process.env.RELATED_QUESTION_MODEL || modelId-const relatedQuestions = await generateRelatedQuestions(- questionModel,[...messagesToModel, responseMessage],abortSignal,parentTraceId