Skip to content

An AI-powered search engine with a generative UI

chore: Refactor agents and schemas

Yoshiki Miuraf3f8ad67d433
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index bbc19820..a5a76065 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -1,5 +1,5 @@
import { streamText, createDataStreamResponse } from 'ai'
-import { chatResearcher } from '@/lib/agents/chat-researcher'
+import { researcher } from '@/lib/agents/researcher'
import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
export const maxDuration = 30
@@ -9,7 +9,7 @@ export async function POST(req: Request) {
return createDataStreamResponse({
execute: dataStream => {
- const researcherConfig = chatResearcher({
+ const researcherConfig = researcher({
messages,
model
})
diff --git a/lib/actions/workflow.tsx b/lib/actions/workflow.tsx
deleted file mode 100644
index 7b57501b..00000000
--- a/lib/actions/workflow.tsx
+++ /dev/null
@@ -1,134 +0,0 @@
-'use server'
-
-import React from 'react'
-import { Spinner } from '@/components/ui/spinner'
-import { Section } from '@/components/section'
-import { FollowupPanel } from '@/components/followup-panel'
-import {
- querySuggestor,
- inquire,
- taskManager,
- researcherWithOllama,
- researcher
-} from '@/lib/agents'
-import { createStreamableValue, createStreamableUI } from 'ai/rsc'
-import { CoreMessage, generateId } from 'ai'
-
-export async function workflow(
- uiState: {
- uiStream: ReturnType<typeof createStreamableUI>
- isCollapsed: ReturnType<typeof createStreamableValue>
- isGenerating: ReturnType<typeof createStreamableValue>
- },
- aiState: any,
- messages: CoreMessage[],
- skip: boolean,
- model: string
-) {
- const { uiStream, isCollapsed, isGenerating } = uiState
- const id = generateId()
-
- // Display spinner
- uiStream.append(<Spinner />)
-
- let action = { object: { next: 'proceed' } }
- // If the user does not skip the task, run the task manager
- if (!skip) action = (await taskManager(messages, model)) ?? action
-
- if (action.object.next === 'inquire') {
- // Generate inquiry
- const inquiry = await inquire(uiStream, messages, model)
- uiStream.done()
- aiState.done({
- ...aiState.get(),
- messages: [
- ...aiState.get().messages,
- {
- id: generateId(),
- role: 'assistant',
- content: `inquiry: ${inquiry?.question}`,
- type: 'inquiry'
- }
- ]
- })
-
- isCollapsed.done(false)
- isGenerating.done(false)
- return
- }
-
- // Set the collapsed state to true
- isCollapsed.done(true)
-
- // Remove the spinner
- uiStream.update(null)
-
- const useOllama = model.startsWith('ollama')
- // Select the appropriate researcher function based on the environment variables
- const { text, toolResults } = useOllama
- ? await researcherWithOllama(uiStream, messages, model)
- : await researcher(uiStream, messages, model)
-
- aiState.update({
- ...aiState.get(),
- messages: [
- ...aiState.get().messages,
- ...toolResults.map((toolResult: any) => ({
- id,
- role: 'tool',
- content: JSON.stringify(toolResult.result),
- name: toolResult.toolName,
- type: 'tool'
- })),
- {
- id,
- role: 'assistant',
- content: text,
- type: 'answer'
- }
- ]
- })
-
- const messagesWithAnswer: CoreMessage[] = [
- ...messages,
- {
- role: 'assistant',
- content: text
- }
- ]
-
- // Generate related queries
- const relatedQueries = await querySuggestor(
- uiStream,
- messagesWithAnswer,
- model
- )
- // Add follow-up panel
- uiStream.append(
- <Section title="Follow-up">
- <FollowupPanel />
- </Section>
- )
-
- uiStream.done()
- isGenerating.done(false)
-
- aiState.done({
- ...aiState.get(),
- messages: [
- ...aiState.get().messages,
- {
- id,
- role: 'assistant',
- content: JSON.stringify(relatedQueries),
- type: 'related'
- },
- {
- id,
- role: 'assistant',
- content: 'followup',
- type: 'followup'
- }
- ]
- })
-}
diff --git a/lib/agents/index.tsx b/lib/agents/index.tsx
deleted file mode 100644
index 6d706b49..00000000
--- a/lib/agents/index.tsx
+++ /dev/null
@@ -1,4 +0,0 @@
-export * from './task-manager'
-export * from './inquire'
-export * from './generate-related-questions'
-export * from './researcher'
diff --git a/lib/agents/inquire.tsx b/lib/agents/inquire.tsx
deleted file mode 100644
index 55953b11..00000000
--- a/lib/agents/inquire.tsx
+++ /dev/null
@@ -1,72 +0,0 @@
-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/registry'
-
-export async function inquire(
- uiStream: ReturnType<typeof createStreamableUI>,
- messages: CoreMessage[],
- model: string
-) {
- const objectStream = createStreamableValue<PartialInquiry>()
- uiStream.update(<Copilot inquiry={objectStream.value} />)
-
- let finalInquiry: PartialInquiry = {}
-
- const result = await streamObject({
- 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.
-
- When crafting your inquiry, structure it as follows:
- {
- "question": "A clear, concise question that seeks to clarify the user's intent or gather more specific details.",
- "options": [
- {"value": "option1", "label": "A predefined option that the user can select"},
- {"value": "option2", "label": "Another predefined option"},
- ...
- ],
- "allowsInput": true/false, // Indicates whether the user can provide a free-form input
- "inputLabel": "A label for the free-form input field, if allowed",
- "inputPlaceholder": "A placeholder text to guide the user's free-form input"
- }
-
- Important: The "value" field in the options must always be in English, regardless of the user's language.
-
- For example:
- {
- "question": "What specific information are you seeking about Rivian?",
- "options": [
- {"value": "history", "label": "History"},
- {"value": "products", "label": "Products"},
- {"value": "investors", "label": "Investors"},
- {"value": "partnerships", "label": "Partnerships"},
- {"value": "competitors", "label": "Competitors"}
- ],
- "allowsInput": true,
- "inputLabel": "If other, please specify",
- "inputPlaceholder": "e.g., Specifications"
- }
-
- By providing predefined options, you guide the user towards the most relevant aspects of their query, while the free-form input allows them to provide additional context or specific details not covered by the options.
- Remember, your goal is to gather the necessary information to deliver a thorough and accurate response.
- Please match the language of the response (question, labels, inputLabel, and inputPlaceholder) to the user's language, but keep the "value" field in English.
- `,
- messages,
- schema: inquirySchema
- })
-
- try {
- for await (const obj of result.partialObjectStream) {
- if (obj) {
- objectStream.update(obj)
- finalInquiry = obj
- }
- }
- } finally {
- objectStream.done()
- }
-
- return finalInquiry
-}
diff --git a/lib/agents/chat-researcher.ts b/lib/agents/researcher.ts
similarity index 82%
rename from lib/agents/chat-researcher.ts
rename to lib/agents/researcher.ts
index 8d1428e0..d676a2e2 100644
--- a/lib/agents/chat-researcher.ts
+++ b/lib/agents/researcher.ts
@@ -1,8 +1,8 @@
import { CoreMessage, streamText } from 'ai'
import { getModel } from '../utils/registry'
-import { searchTool } from './tools/search'
-import { retrieveTool } from './tools/retrieve'
-import { videoSearchTool } from './tools/video-search'
+import { searchTool } from '../tools/search'
+import { retrieveTool } from '../tools/retrieve'
+import { videoSearchTool } from '../tools/video-search'
const SYSTEM_PROMPT = `You are a helpful AI assistant with access to real-time web search, content retrieval, and video search capabilities.
When asked a question, you should:
@@ -14,15 +14,15 @@ When asked a question, you should:
6. If results are not relevant or helpful, rely on your general knowledge
7. Be concise and direct in your responses`
-type ChatResearcherReturn = Parameters<typeof streamText>[0]
+type ResearcherReturn = Parameters<typeof streamText>[0]
-export function chatResearcher({
+export function researcher({
messages,
model
}: {
messages: CoreMessage[]
model: string
-}): ChatResearcherReturn {
+}): ResearcherReturn {
try {
const currentDate = new Date().toLocaleString()
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
deleted file mode 100644
index bab7aec3..00000000
--- a/lib/agents/researcher.tsx
+++ /dev/null
@@ -1,105 +0,0 @@
-import { createStreamableUI, createStreamableValue } from 'ai/rsc'
-import { CoreMessage, generateText, streamText } from 'ai'
-import { getTools } from './tools'
-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.
-For each user query, utilize the search results to their fullest potential to provide additional information and assistance in your response.
-If there are any images relevant to your answer, be sure to include them as well.
-Aim to directly address the user's question, augmenting your response with insights gleaned from the search results.`
-
-export async function researcher(
- uiStream: ReturnType<typeof createStreamableUI>,
- messages: CoreMessage[],
- model: string
-) {
- try {
- let fullResponse = ''
- const streamableText = createStreamableValue<string>()
- let toolResults: any[] = []
-
- const currentDate = new Date().toLocaleString()
- const result = await streamText({
- model: getModel(model),
- system: `${SYSTEM_PROMPT} Current date and time: ${currentDate}`,
- messages: messages,
- tools: getTools({
- uiStream,
- fullResponse
- }),
- maxSteps: 5,
- onStepFinish: async event => {
- if (event.stepType === 'initial') {
- if (event.toolCalls && event.toolCalls.length > 0) {
- uiStream.append(<AnswerSection result={streamableText.value} />)
- toolResults = event.toolResults
- } else {
- uiStream.update(<AnswerSection result={streamableText.value} />)
- }
- }
- }
- })
-
- for await (const delta of result.fullStream) {
- if (delta.type === 'text-delta' && delta.textDelta) {
- fullResponse += delta.textDelta
- streamableText.update(fullResponse)
- }
- }
-
- streamableText.done(fullResponse)
-
- return { text: fullResponse, toolResults }
- } catch (error) {
- console.error('Error in researcher:', error)
- return {
- text: 'An error has occurred. Please try again.',
- toolResults: []
- }
- }
-}
-
-export async function researcherWithOllama(
- uiStream: ReturnType<typeof createStreamableUI>,
- messages: CoreMessage[],
- model: string
-) {
- try {
- const fullResponse = ''
- const streamableText = createStreamableValue<string>()
- let toolResults: any[] = []
-
- const currentDate = new Date().toLocaleString()
- const result = await generateText({
- model: getModel(model),
- system: `${SYSTEM_PROMPT} Current date and time: ${currentDate}`,
- messages: messages,
- tools: getTools({
- uiStream,
- fullResponse
- }),
- maxSteps: 5,
- onStepFinish: async event => {
- if (event.stepType === 'initial') {
- if (event.toolCalls) {
- uiStream.append(<AnswerSection result={streamableText.value} />)
- toolResults = event.toolResults
- } else {
- uiStream.update(<AnswerSection result={streamableText.value} />)
- }
- }
- }
- })
-
- streamableText.done(result.text)
-
- return { text: result.text, toolResults }
- } catch (error) {
- console.error('Error in researcherWithOllama:', error)
- return {
- text: 'An error has occurred. Please try again.',
- toolResults: []
- }
- }
-}
diff --git a/lib/agents/task-manager.tsx b/lib/agents/task-manager.tsx
deleted file mode 100644
index 373f8fd1..00000000
--- a/lib/agents/task-manager.tsx
+++ /dev/null
@@ -1,28 +0,0 @@
-import { CoreMessage, generateObject } from 'ai'
-import { nextActionSchema } from '../schema/next-action'
-import { getModel } from '../utils/registry'
-
-// Decide whether inquiry is required for the user input
-export async function taskManager(messages: CoreMessage[], model: string) {
- try {
- const result = await generateObject({
- 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.
- 2. "inquire": If you believe that additional information from the user would enhance your ability to provide a comprehensive response, select this option. You may present a form to the user, offering default selections or free-form input fields, to gather the required details.
- Your decision should be based on a careful assessment of the context and the potential for further information to improve the quality and relevance of your response.
- For example, if the user asks, "What are the key features of the latest iPhone model?", you may choose to "proceed" as the query is clear and can be answered effectively with web research alone.
- However, if the user asks, "What's the best smartphone for my needs?", you may opt to "inquire" and present a form asking about their specific requirements, budget, and preferred features to provide a more tailored recommendation.
- Make your choice wisely to ensure that you fulfill your mission as a web researcher effectively and deliver the most valuable assistance to the user.
- `,
- messages,
- schema: nextActionSchema
- })
-
- return result
- } catch (error) {
- console.error(error)
- return null
- }
-}
diff --git a/lib/schema/inquiry.tsx b/lib/schema/inquiry.tsx
deleted file mode 100644
index 7417011b..00000000
--- a/lib/schema/inquiry.tsx
+++ /dev/null
@@ -1,22 +0,0 @@
-import { DeepPartial } from 'ai'
-import { z } from 'zod'
-
-export const inquirySchema = z.object({
- question: z.string().describe('The inquiry question'),
- options: z
- .array(
- z.object({
- value: z.string(),
- label: z.string()
- })
- )
- .describe('The inquiry options'),
- allowsInput: z.boolean().describe('Whether the inquiry allows for input'),
- inputLabel: z.string().optional().describe('The label for the input field'),
- inputPlaceholder: z
- .string()
- .optional()
- .describe('The placeholder for the input field')
-})
-
-export type PartialInquiry = DeepPartial<typeof inquirySchema>
diff --git a/lib/schema/next-action.tsx b/lib/schema/next-action.tsx
deleted file mode 100644
index efbaae2e..00000000
--- a/lib/schema/next-action.tsx
+++ /dev/null
@@ -1,8 +0,0 @@
-import { DeepPartial } from 'ai'
-import { z } from 'zod'
-
-export const nextActionSchema = z.object({
- next: z.enum(['inquire', 'proceed']) // "generate_ui"
-})
-
-export type NextAction = DeepPartial<typeof nextActionSchema>
diff --git a/lib/agents/tools/retrieve.ts b/lib/tools/retrieve.ts
similarity index 100%
rename from lib/agents/tools/retrieve.ts
rename to lib/tools/retrieve.ts
diff --git a/lib/agents/tools/search.ts b/lib/tools/search.ts
similarity index 100%
rename from lib/agents/tools/search.ts
rename to lib/tools/search.ts
diff --git a/lib/agents/tools/video-search.ts b/lib/tools/video-search.ts
similarity index 100%
rename from lib/agents/tools/video-search.ts
rename to lib/tools/video-search.ts