Skip to content

An AI-powered search engine with a generative UI

refactor: Update getModel function to accept model parameter

Yoshiki Miura62e33e0c835b
diff --git a/app/actions.tsx b/app/actions.tsx
index 934a5081..cbca4d8e 100644
--- a/app/actions.tsx
+++ b/app/actions.tsx
@@ -20,6 +20,7 @@ import RetrieveSection from '@/components/retrieve-section'
import { VideoSearchSection } from '@/components/video-search-section'
import { AnswerSection } from '@/components/answer-section'
import { workflow } from '@/lib/actions/workflow'
+import { isProviderEnabled } from '@/lib/utils/registry'
const MAX_MESSAGES = 6
@@ -70,6 +71,18 @@ async function submit(
? 'input_related'
: 'inquiry'
+ // Get the model from the form data (e.g., openai:gpt-4o-mini)
+ const model = (formData?.get('model') as string) || 'openai:gpt-4o-mini'
+ const providerId = model.split(':')[0]
+ const modelId = model.split(':')[1]
+ console.log('model', model, providerId, modelId)
+ // Check if provider is enabled
+ if (!isProviderEnabled(providerId)) {
+ throw new Error(
+ `Provider ${providerId} is not available (API key not configured)`
+ )
+ }
+
// Add the user message to the state
if (content) {
aiState.update({
@@ -95,7 +108,8 @@ async function submit(
{ uiStream, isCollapsed, isGenerating },
aiState,
messages,
- skip ?? false
+ skip ?? false,
+ model
)
return {
diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx
index 1feebb77..eadc9859 100644
--- a/components/chat-panel.tsx
+++ b/components/chat-panel.tsx
@@ -13,19 +13,21 @@ import Textarea from 'react-textarea-autosize'
import { generateId } from 'ai'
import { useAppState } from '@/lib/utils/app-state'
import { ModelSelector } from './model-selector'
+import { Model, models } from '@/lib/types/models'
+import { useLocalStorage } from '@/lib/hooks/use-local-storage'
interface ChatPanelProps {
messages: UIState
query?: string
- onModelChange?: (modelId: string) => void
- defaultModel?: string
+ onModelChange?: (model: Model) => void
+ defaultModel?: Model
}
export function ChatPanel({
messages,
query,
onModelChange,
- defaultModel
+ defaultModel = models[0]
}: ChatPanelProps) {
const [input, setInput] = useState('')
const [showEmptyScreen, setShowEmptyScreen] = useState(false)
@@ -36,7 +38,12 @@ export function ChatPanel({
const router = useRouter()
const inputRef = useRef<HTMLTextAreaElement>(null)
const isFirstRender = useRef(true) // For development environment
- const [selectedModel, setSelectedModel] = useState(defaultModel || 'gpt-4')
+
+ // Use localStorage for selectedModel
+ const [selectedModel, setSelectedModel] = useLocalStorage<Model>(
+ 'selectedModel',
+ defaultModel
+ )
const [isComposing, setIsComposing] = useState(false) // Composition state
const [enterDisabled, setEnterDisabled] = useState(false) // Disable Enter after composition ends
@@ -64,11 +71,24 @@ export function ChatPanel({
}
])
- // Submit and get response message
+ // Use existing formData or create new one
const data = formData || new FormData()
+
+ // Add or update the model information
+ const modelString = `${selectedModel.providerId}:${selectedModel.id}`
+ data.set('model', modelString)
+
+ // Add or update the input query if not already present
if (!formData) {
- data.append('input', query)
+ data.set('input', query)
}
+
+ console.log('Submitting data:', {
+ model: modelString,
+ input: data.get('input'),
+ formDataEntries: Object.fromEntries(data)
+ })
+
const responseMessage = await submit(data)
setMessages(currentMessages => [...currentMessages, responseMessage])
}
@@ -76,7 +96,11 @@ export function ChatPanel({
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const formData = new FormData(e.currentTarget)
- await handleQuerySubmit(input, formData)
+ try {
+ await handleQuerySubmit(input, formData)
+ } catch (error) {
+ console.error('Error submitting form:', error)
+ }
}
// if query is not empty, submit the query
@@ -143,9 +167,9 @@ export function ChatPanel({
<div className="relative flex items-center w-full">
<ModelSelector
selectedModel={selectedModel}
- onModelChange={modelId => {
- setSelectedModel(modelId)
- onModelChange?.(modelId)
+ onModelChange={model => {
+ setSelectedModel(model)
+ onModelChange?.(model)
}}
/>
<Textarea
diff --git a/components/model-selector.tsx b/components/model-selector.tsx
index 32e12e3b..6c09020f 100644
--- a/components/model-selector.tsx
+++ b/components/model-selector.tsx
@@ -14,11 +14,10 @@ import { Model, models } from '@/lib/types/models'
import { useLocalStorage } from '@/lib/hooks/use-local-storage'
interface ModelSelectorProps {
- selectedModel: string
- onModelChange: (modelId: string) => void
+ selectedModel: Model
+ onModelChange: (model: Model) => void
}
-// Group models by provider
function groupModelsByProvider(models: Model[]) {
return models.reduce((groups, model) => {
const provider = model.provider
@@ -34,22 +33,25 @@ export function ModelSelector({
selectedModel: initialModel,
onModelChange
}: ModelSelectorProps) {
- const [selectedModel, setSelectedModel] = useLocalStorage(
+ const [selectedModel, setSelectedModel] = useLocalStorage<Model>(
'selectedModel',
initialModel
)
const groupedModels = groupModelsByProvider(models)
const handleModelChange = (modelId: string) => {
- setSelectedModel(modelId)
- onModelChange(modelId)
+ const selectedModelData = models.find(model => model.id === modelId)
+ if (selectedModelData) {
+ setSelectedModel(selectedModelData)
+ onModelChange(selectedModelData)
+ }
}
return (
<div className="absolute -top-8 left-2">
<Select
name="model"
- value={selectedModel}
+ value={selectedModel.id}
onValueChange={handleModelChange}
>
<SelectTrigger className="mr-2 h-7 text-xs border-none shadow-none focus:ring-0">
diff --git a/lib/actions/workflow.tsx b/lib/actions/workflow.tsx
index c138ae78..dce30a84 100644
--- a/lib/actions/workflow.tsx
+++ b/lib/actions/workflow.tsx
@@ -22,7 +22,8 @@ export async function workflow(
},
aiState: any,
messages: CoreMessage[],
- skip: boolean
+ skip: boolean,
+ model: string
) {
const { uiStream, isCollapsed, isGenerating } = uiState
const id = generateId()
@@ -32,11 +33,11 @@ export async function workflow(
let action = { object: { next: 'proceed' } }
// If the user does not skip the task, run the task manager
- if (!skip) action = (await taskManager(messages)) ?? action
+ if (!skip) action = (await taskManager(messages, model)) ?? action
if (action.object.next === 'inquire') {
// Generate inquiry
- const inquiry = await inquire(uiStream, messages)
+ const inquiry = await inquire(uiStream, messages, model)
uiStream.done()
aiState.done({
...aiState.get(),
@@ -65,8 +66,8 @@ export async function workflow(
const useOllama = process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL
// Select the appropriate researcher function based on the environment variables
const { text, toolResults } = useOllama
- ? await researcherWithOllama(uiStream, messages)
- : await researcher(uiStream, messages)
+ ? await researcherWithOllama(uiStream, messages, model)
+ : await researcher(uiStream, messages, model)
aiState.update({
...aiState.get(),
@@ -97,7 +98,11 @@ export async function workflow(
]
// Generate related queries
- const relatedQueries = await querySuggestor(uiStream, messagesWithAnswer)
+ const relatedQueries = await querySuggestor(
+ uiStream,
+ messagesWithAnswer,
+ model
+ )
// Add follow-up panel
uiStream.append(
<Section title="Follow-up">
diff --git a/lib/agents/inquire.tsx b/lib/agents/inquire.tsx
index 9d94749c..67ba359f 100644
--- a/lib/agents/inquire.tsx
+++ b/lib/agents/inquire.tsx
@@ -2,18 +2,19 @@ import { Copilot } from '@/components/copilot'
import { createStreamableUI, createStreamableValue } from 'ai/rsc'
import { CoreMessage, streamObject } from 'ai'
import { PartialInquiry, inquirySchema } from '@/lib/schema/inquiry'
-import { getModel } from '../utils'
+import { getModel } from '../utils/registry'
export async function inquire(
uiStream: ReturnType<typeof createStreamableUI>,
- messages: CoreMessage[]
+ messages: CoreMessage[],
+ model: string
) {
const objectStream = createStreamableValue<PartialInquiry>()
uiStream.update(<Copilot inquiry={objectStream.value} />)
let finalInquiry: PartialInquiry = {}
await streamObject({
- model: getModel(),
+ model: getModel(model),
system: `As a professional web researcher, your role is to deepen your understanding of the user's input by conducting further inquiries when necessary.
After receiving an initial response from the user, carefully assess whether additional questions are absolutely essential to provide a comprehensive and accurate answer. Only proceed with further inquiries if the available information is insufficient or ambiguous.
diff --git a/lib/agents/query-suggestor.tsx b/lib/agents/query-suggestor.tsx
index 6bdff7dc..760f5f90 100644
--- a/lib/agents/query-suggestor.tsx
+++ b/lib/agents/query-suggestor.tsx
@@ -2,11 +2,12 @@ import { createStreamableUI, createStreamableValue } from 'ai/rsc'
import { CoreMessage, streamObject } from 'ai'
import { PartialRelated, relatedSchema } from '@/lib/schema/related'
import SearchRelated from '@/components/search-related'
-import { getModel } from '../utils'
+import { getModel } from '../utils/registry'
export async function querySuggestor(
uiStream: ReturnType<typeof createStreamableUI>,
- messages: CoreMessage[]
+ messages: CoreMessage[],
+ model: string
) {
const objectStream = createStreamableValue<PartialRelated>()
uiStream.append(<SearchRelated relatedQueries={objectStream.value} />)
@@ -20,7 +21,7 @@ export async function querySuggestor(
let finalRelatedQueries: PartialRelated = {}
await streamObject({
- model: getModel(),
+ model: getModel(model),
system: `As a professional web researcher, your task is to generate a set of three queries that explore the subject matter more deeply, building upon the initial query and the information uncovered in its search results.
For instance, if the original query was "Starship's third test flight key milestones", your output should follow this format:
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
index c95a62da..bab7aec3 100644
--- a/lib/agents/researcher.tsx
+++ b/lib/agents/researcher.tsx
@@ -1,7 +1,7 @@
import { createStreamableUI, createStreamableValue } from 'ai/rsc'
import { CoreMessage, generateText, streamText } from 'ai'
import { getTools } from './tools'
-import { getModel } from '../utils'
+import { getModel } from '../utils/registry'
import { AnswerSection } from '@/components/answer-section'
const SYSTEM_PROMPT = `As a professional search expert, you possess the ability to search for any information on the web.
@@ -11,7 +11,8 @@ Aim to directly address the user's question, augmenting your response with insig
export async function researcher(
uiStream: ReturnType<typeof createStreamableUI>,
- messages: CoreMessage[]
+ messages: CoreMessage[],
+ model: string
) {
try {
let fullResponse = ''
@@ -20,7 +21,7 @@ export async function researcher(
const currentDate = new Date().toLocaleString()
const result = await streamText({
- model: getModel(),
+ model: getModel(model),
system: `${SYSTEM_PROMPT} Current date and time: ${currentDate}`,
messages: messages,
tools: getTools({
@@ -61,7 +62,8 @@ export async function researcher(
export async function researcherWithOllama(
uiStream: ReturnType<typeof createStreamableUI>,
- messages: CoreMessage[]
+ messages: CoreMessage[],
+ model: string
) {
try {
const fullResponse = ''
@@ -70,7 +72,7 @@ export async function researcherWithOllama(
const currentDate = new Date().toLocaleString()
const result = await generateText({
- model: getModel(),
+ model: getModel(model),
system: `${SYSTEM_PROMPT} Current date and time: ${currentDate}`,
messages: messages,
tools: getTools({
diff --git a/lib/agents/task-manager.tsx b/lib/agents/task-manager.tsx
index 44dca3e5..373f8fd1 100644
--- a/lib/agents/task-manager.tsx
+++ b/lib/agents/task-manager.tsx
@@ -1,12 +1,12 @@
import { CoreMessage, generateObject } from 'ai'
import { nextActionSchema } from '../schema/next-action'
-import { getModel } from '../utils'
+import { getModel } from '../utils/registry'
// Decide whether inquiry is required for the user input
-export async function taskManager(messages: CoreMessage[]) {
+export async function taskManager(messages: CoreMessage[], model: string) {
try {
const result = await generateObject({
- model: getModel(),
+ model: getModel(model),
system: `As a professional web researcher, your primary objective is to fully comprehend the user's query, conduct thorough web searches to gather the necessary information, and provide an appropriate response.
To achieve this, you must first analyze the user's input and determine the optimal course of action. You have two options at your disposal:
1. "proceed": If the provided information is sufficient to address the query effectively, choose this option to proceed with the research and formulate a response.
diff --git a/lib/types/index.ts b/lib/types/index.ts
index 079963f8..5d95c343 100644
--- a/lib/types/index.ts
+++ b/lib/types/index.ts
@@ -97,10 +97,8 @@ export interface SearXNGResponse {
results: SearXNGResult[]
}
-
export type SearXNGImageResult = string
-
export type SearXNGSearchResults = {
images: SearXNGImageResult[]
results: SearchResultItem[]
diff --git a/lib/types/models.ts b/lib/types/models.ts
index 64ce4fc4..f4980202 100644
--- a/lib/types/models.ts
+++ b/lib/types/models.ts
@@ -19,7 +19,7 @@ export const models: Model[] = [
providerId: 'openai'
},
{
- id: 'claude-3.5-sonnet',
+ id: 'claude-3-5-sonnet-latest',
name: 'Claude 3.5 Sonnet',
provider: 'Anthropic',
providerId: 'anthropic'
diff --git a/lib/utils/index.ts b/lib/utils/index.ts
index 64a9ef46..1e28d160 100644
--- a/lib/utils/index.ts
+++ b/lib/utils/index.ts
@@ -11,10 +11,9 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
-export function getModel(useSubModel = false) {
+export function getModel(model: string) {
const ollamaBaseUrl = process.env.OLLAMA_BASE_URL + '/api'
const ollamaModel = process.env.OLLAMA_MODEL
- const ollamaSubModel = process.env.OLLAMA_SUB_MODEL
const openaiApiBase = process.env.OPENAI_API_BASE
const openaiApiKey = process.env.OPENAI_API_KEY
let openaiApiModel = process.env.OPENAI_API_MODEL || 'gpt-4o'
@@ -40,11 +39,6 @@ export function getModel(useSubModel = false) {
// Ollama
if (ollamaBaseUrl && ollamaModel) {
const ollama = createOllama({ baseURL: ollamaBaseUrl })
-
- if (useSubModel && ollamaSubModel) {
- return ollama(ollamaSubModel)
- }
-
return ollama(ollamaModel)
}
diff --git a/lib/utils/registry.ts b/lib/utils/registry.ts
new file mode 100644
index 00000000..e1901f04
--- /dev/null
+++ b/lib/utils/registry.ts
@@ -0,0 +1,39 @@
+import { openai, createOpenAI } from '@ai-sdk/openai'
+import { anthropic } from '@ai-sdk/anthropic'
+import { experimental_createProviderRegistry as createProviderRegistry } from 'ai'
+import { google } from '@ai-sdk/google'
+import { createOllama } from 'ollama-ai-provider'
+import { createAzure } from '@ai-sdk/azure'
+import { Model } from '../types/models'
+
+export const registry = createProviderRegistry({
+ openai,
+ anthropic,
+ google,
+ groq: createOpenAI({
+ apiKey: process.env.GROQ_API_KEY,
+ baseURL: 'https://api.groq.com/openai/v1'
+ }),
+ ollama: createOllama({
+ baseURL: process.env.OLLAMA_BASE_URL
+ }),
+ azure: createAzure({
+ apiKey: process.env.AZURE_API_KEY,
+ resourceName: process.env.AZURE_RESOURCE_NAME
+ })
+})
+
+export function getModel(model: string) {
+ return registry.languageModel(model)
+}
+
+export function isProviderEnabled(providerId: string): boolean {
+ switch (providerId) {
+ case 'openai':
+ return !!process.env.OPENAI_API_KEY
+ case 'anthropic':
+ return !!process.env.ANTHROPIC_API_KEY
+ default:
+ return false
+ }
+}