Skip to content

An AI-powered search engine with a generative UI

chore: Update chat researcher and search video tool

Yoshiki Miura3cfd0e9cb52a
diff --git a/lib/agents/chat-researcher.ts b/lib/agents/chat-researcher.ts
index f8c100ee..e61d074a 100644
--- a/lib/agents/chat-researcher.ts
+++ b/lib/agents/chat-researcher.ts
@@ -2,20 +2,17 @@ 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'
-const SYSTEM_PROMPT = `You are a helpful AI assistant with access to real-time web search capabilities and content retrieval.
+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:
1. Search for relevant information using the search tool when needed
2. Use the retrieve tool to get detailed content from specific URLs
-3. Analyze the search and retrieved results to provide accurate, up-to-date information
-4. Always cite your sources when using information from search or retrieved content
-5. If results are not relevant or helpful, rely on your general knowledge
-6. Be concise and direct in your responses
-
-Remember to:
-- Use search for current events, facts, or when you need to verify information
-- Clearly indicate when you're using search results vs. your general knowledge
-- Admit if you cannot find relevant information for a query`
+3. Use the video search tool when looking for video content
+4. Analyze all search results to provide accurate, up-to-date information
+5. Always cite your sources when using information from any search results
+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]
@@ -33,7 +30,11 @@ export async function chatResearcher({
model: getModel(model),
system: `${SYSTEM_PROMPT}\nCurrent date and time: ${currentDate}`,
messages,
- tools: { search: searchTool, retrieve: retrieveTool },
+ tools: {
+ search: searchTool,
+ retrieve: retrieveTool,
+ videoSearch: videoSearchTool
+ },
maxSteps: 5
}
} catch (error) {
diff --git a/lib/agents/tools/index.tsx b/lib/agents/tools/index.tsx
deleted file mode 100644
index 8ebd85c3..00000000
--- a/lib/agents/tools/index.tsx
+++ /dev/null
@@ -1,31 +0,0 @@
-import { createStreamableUI } from 'ai/rsc'
-import { retrieveTool } from './retrieve'
-import { searchTool } from './search'
-import { videoSearchTool } from './video-search'
-
-export interface ToolProps {
- uiStream: ReturnType<typeof createStreamableUI>
- fullResponse: string
-}
-
-export const getTools = ({ uiStream, fullResponse }: ToolProps) => {
- const tools: any = {
- search: searchTool({
- uiStream,
- fullResponse
- }),
- retrieve: retrieveTool({
- uiStream,
- fullResponse
- })
- }
-
- if (process.env.SERPER_API_KEY) {
- tools.videoSearch = videoSearchTool({
- uiStream,
- fullResponse
- })
- }
-
- return tools
-}
diff --git a/lib/agents/tools/video-search.ts b/lib/agents/tools/video-search.ts
new file mode 100644
index 00000000..a500d510
--- /dev/null
+++ b/lib/agents/tools/video-search.ts
@@ -0,0 +1,28 @@
+import { tool } from 'ai'
+import { searchSchema } from '@/lib/schema/search'
+
+export const videoSearchTool = tool({
+ description: 'Search for videos from YouTube',
+ parameters: searchSchema,
+ execute: async ({ query }) => {
+ try {
+ const response = await fetch('https://google.serper.dev/videos', {
+ method: 'POST',
+ headers: {
+ 'X-API-KEY': process.env.SERPER_API_KEY || '',
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({ q: query })
+ })
+
+ if (!response.ok) {
+ throw new Error('Network response was not ok')
+ }
+
+ return await response.json()
+ } catch (error) {
+ console.error('Video Search API error:', error)
+ return null
+ }
+ }
+})
diff --git a/lib/agents/tools/video-search.tsx b/lib/agents/tools/video-search.tsx
deleted file mode 100644
index 190e4e43..00000000
--- a/lib/agents/tools/video-search.tsx
+++ /dev/null
@@ -1,47 +0,0 @@
-import { tool } from 'ai'
-import { createStreamableValue } from 'ai/rsc'
-import { searchSchema } from '@/lib/schema/search'
-import { ToolProps } from '.'
-import { VideoSearchSection } from '@/components/video-search-section'
-
-// Start Generation Here
-export const videoSearchTool = ({ uiStream, fullResponse }: ToolProps) => tool({
- description: 'Search for videos from YouTube',
- parameters: searchSchema,
- execute: async ({ query }) => {
- let hasError = false
- // Append the search section
- const streamResults = createStreamableValue<string>()
- uiStream.append(<VideoSearchSection result={streamResults.value} />)
-
- let searchResult
- try {
- const response = await fetch('https://google.serper.dev/videos', {
- method: 'POST',
- headers: {
- 'X-API-KEY': process.env.SERPER_API_KEY || '',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({ q: query })
- })
- if (!response.ok) {
- throw new Error('Network response was not ok')
- }
- searchResult = await response.json()
- } catch (error) {
- console.error('Video Search API error:', error)
- hasError = true
- }
-
- if (hasError) {
- fullResponse = `An error occurred while searching for videos with "${query}.`
- uiStream.update(null)
- streamResults.done()
- return searchResult
- }
-
- streamResults.done(JSON.stringify(searchResult))
-
- return searchResult
- }
-})