Skip to content

An AI-powered search engine with a generative UI

Refactor researcher function and update tools

Yoshiki Miura99d1fcdea443
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
index 79fb653e..1f01fadc 100644
--- a/lib/agents/researcher.tsx
+++ b/lib/agents/researcher.tsx
@@ -5,13 +5,10 @@ import {
ToolResultPart,
streamText as nonexperimental_streamText
} from 'ai'
-import { searchSchema } from '@/lib/schema/search'
import { Section } from '@/components/section'
import { OpenAI } from '@ai-sdk/openai'
import { BotMessage } from '@/components/message'
-import Exa from 'exa-js'
-import { Card } from '@/components/ui/card'
-import { SearchSection } from '@/components/search-section'
+import { getTools } from './tools'
export async function researcher(
uiStream: ReturnType<typeof createStreamableUI>,
@@ -25,8 +22,6 @@ export async function researcher(
organization: '' // optional organization
})
- const searchAPI: 'tavily' | 'exa' = 'tavily'
-
let fullResponse = ''
let hasError = false
const answerSection = (
@@ -39,74 +34,30 @@ export async function researcher(
const result = await nonexperimental_streamText({
model: openai.chat(process.env.OPENAI_API_MODEL || 'gpt-4o'),
maxTokens: 2500,
- system: `As a professional search expert, you possess the ability to search for any information on the web.
+ system: `As a professional search expert, you possess the ability to search for any information on the web.
+ or 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.
Whenever quoting or referencing information from a specific URL, always cite the source URL explicitly.
Please match the language of the response to the user's language.`,
messages,
- tools: {
- search: {
- description: 'Search the web for information',
- parameters: searchSchema,
- execute: async ({
- query,
- max_results,
- search_depth
- }: {
- query: string
- max_results: number
- search_depth: 'basic' | 'advanced'
- }) => {
- // If this is the first tool response, remove spinner
- if (isFirstToolResponse) {
- isFirstToolResponse = false
- uiStream.update(null)
- }
- // Append the search section
- const streamResults = createStreamableValue<string>()
- uiStream.append(<SearchSection result={streamResults.value} />)
-
- // Tavily API requires a minimum of 5 characters in the query
- const filledQuery =
- query.length < 5 ? query + ' '.repeat(5 - query.length) : query
- let searchResult
- try {
- searchResult =
- searchAPI === 'tavily'
- ? await tavilySearch(filledQuery, max_results, search_depth)
- : await exaSearch(query)
- } catch (error) {
- console.error('Search API error:', error)
- hasError = true
- }
-
- if (hasError) {
- fullResponse += `\nAn error occurred while searching for "${query}.`
- uiStream.update(
- <Card className="p-4 mt-2 text-sm">
- {`An error occurred while searching for "${query}".`}
- </Card>
- )
- return searchResult
- }
-
- streamResults.done(JSON.stringify(searchResult))
-
- return searchResult
- }
- }
- }
+ tools: getTools({
+ uiStream,
+ fullResponse,
+ hasError,
+ isFirstToolResponse
+ })
})
+ // Process the response
const toolCalls: ToolCallPart[] = []
const toolResponses: ToolResultPart[] = []
for await (const delta of result.fullStream) {
switch (delta.type) {
case 'text-delta':
if (delta.textDelta) {
- // If the first text delata is available, add a ui section
+ // If the first text delta is available, add a UI section
if (fullResponse.length === 0 && delta.textDelta.length > 0) {
// Update the UI
uiStream.update(answerSection)
@@ -144,41 +95,3 @@ export async function researcher(
return { result, fullResponse, hasError, toolResponses }
}
-
-async function tavilySearch(
- query: string,
- maxResults: number = 10,
- searchDepth: 'basic' | 'advanced' = 'basic'
-): Promise<any> {
- const apiKey = process.env.TAVILY_API_KEY
- const response = await fetch('https://api.tavily.com/search', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- api_key: apiKey,
- query,
- max_results: maxResults < 5 ? 5 : maxResults,
- search_depth: searchDepth,
- include_images: true,
- include_answers: true
- })
- })
-
- if (!response.ok) {
- throw new Error(`Error: ${response.status}`)
- }
-
- const data = await response.json()
- return data
-}
-
-async function exaSearch(query: string, maxResults: number = 10): Promise<any> {
- const apiKey = process.env.EXA_API_KEY
- const exa = new Exa(apiKey)
- return exa.searchAndContents(query, {
- highlights: true,
- numResults: maxResults
- })
-}
diff --git a/lib/agents/tools/index.tsx b/lib/agents/tools/index.tsx
new file mode 100644
index 00000000..3e734786
--- /dev/null
+++ b/lib/agents/tools/index.tsx
@@ -0,0 +1,23 @@
+import { searchTool } from './search'
+import { createStreamableUI } from 'ai/rsc'
+
+interface GetToolsProps {
+ uiStream: ReturnType<typeof createStreamableUI>
+ fullResponse: string
+ hasError: boolean
+ isFirstToolResponse: boolean
+}
+
+export const getTools = ({
+ uiStream,
+ fullResponse,
+ hasError,
+ isFirstToolResponse
+}: GetToolsProps) => ({
+ search: searchTool({
+ uiStream,
+ fullResponse,
+ hasError,
+ isFirstToolResponse
+ })
+})
diff --git a/lib/agents/tools/search.tsx b/lib/agents/tools/search.tsx
new file mode 100644
index 00000000..377824c1
--- /dev/null
+++ b/lib/agents/tools/search.tsx
@@ -0,0 +1,107 @@
+import { createStreamableUI, createStreamableValue } from 'ai/rsc'
+import Exa from 'exa-js'
+import { searchSchema } from '@/lib/schema/search'
+import { Card } from '@/components/ui/card'
+import { SearchSection } from '@/components/search-section'
+
+interface searchToolProps {
+ uiStream: ReturnType<typeof createStreamableUI>
+ fullResponse: string
+ hasError: boolean
+ isFirstToolResponse: boolean
+}
+
+export const searchTool = ({
+ uiStream,
+ fullResponse,
+ hasError,
+ isFirstToolResponse
+}: searchToolProps) => ({
+ description: 'Search the web for information',
+ parameters: searchSchema,
+ execute: async ({
+ query,
+ max_results,
+ search_depth
+ }: {
+ query: string
+ max_results: number
+ search_depth: 'basic' | 'advanced'
+ }) => {
+ // If this is the first tool response, remove spinner
+ if (isFirstToolResponse) {
+ isFirstToolResponse = false
+ uiStream.update(null)
+ }
+ // Append the search section
+ const streamResults = createStreamableValue<string>()
+ uiStream.append(<SearchSection result={streamResults.value} />)
+
+ // Tavily API requires a minimum of 5 characters in the query
+ const filledQuery =
+ query.length < 5 ? query + ' '.repeat(5 - query.length) : query
+ let searchResult
+ const searchAPI: 'tavily' | 'exa' = 'tavily'
+ try {
+ searchResult =
+ searchAPI === 'tavily'
+ ? await tavilySearch(filledQuery, max_results, search_depth)
+ : await exaSearch(query)
+ } catch (error) {
+ console.error('Search API error:', error)
+ hasError = true
+ }
+
+ if (hasError) {
+ fullResponse += `\nAn error occurred while searching for "${query}.`
+ uiStream.update(
+ <Card className="p-4 mt-2 text-sm">
+ {`An error occurred while searching for "${query}".`}
+ </Card>
+ )
+ return searchResult
+ }
+
+ streamResults.done(JSON.stringify(searchResult))
+
+ return searchResult
+ }
+})
+
+async function tavilySearch(
+ query: string,
+ maxResults: number = 10,
+ searchDepth: 'basic' | 'advanced' = 'basic'
+): Promise<any> {
+ const apiKey = process.env.TAVILY_API_KEY
+ const response = await fetch('https://api.tavily.com/search', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ api_key: apiKey,
+ query,
+ max_results: maxResults < 5 ? 5 : maxResults,
+ search_depth: searchDepth,
+ include_images: true,
+ include_answers: true
+ })
+ })
+
+ if (!response.ok) {
+ throw new Error(`Error: ${response.status}`)
+ }
+
+ const data = await response.json()
+ return data
+}
+
+async function exaSearch(query: string, maxResults: number = 10): Promise<any> {
+ const apiKey = process.env.EXA_API_KEY
+ const exa = new Exa(apiKey)
+ return exa.searchAndContents(query, {
+ highlights: true,
+ numResults: maxResults
+ })
+}