Skip to content

An AI-powered search engine with a generative UI

feat: add manual researcher for search result analysis

Yoshiki Miuraf8206b6d1ebf
diff --git a/lib/agents/manual-researcher.ts b/lib/agents/manual-researcher.ts
new file mode 100644
index 00000000..0e8fa163
--- /dev/null
+++ b/lib/agents/manual-researcher.ts
@@ -0,0 +1,51 @@
+import { CoreMessage, smoothStream, streamText } from 'ai'
+import { getModel } from '../utils/registry'
+
+const MANUAL_SYSTEM_PROMPT = `
+Instructions:
+
+You are a helpful AI assistant analyzing search results to provide accurate information.
+
+1. Analyze the provided search results carefully to answer the user's question
+2. Always cite sources using the [number](url) format, matching the order of search results
+3. If multiple sources are relevant, include all of them using comma-separated citations
+4. Only use information that has a URL available for citation
+5. Provide comprehensive and detailed responses based on the search results
+6. Use markdown to structure your responses with appropriate headings
+7. Include relevant images only when they significantly aid understanding
+8. If the search results don't contain relevant information, acknowledge this and provide a general response
+
+Citation Format:
+[number](url)
+
+Image Format:
+![alt text](image_url)
+
+Remember: Focus on the search results provided to you and maintain high accuracy in your responses.
+`
+
+interface ManualResearcherConfig {
+ messages: CoreMessage[]
+ model: string
+}
+
+type ManualResearcherReturn = Parameters<typeof streamText>[0]
+
+export function manualResearcher({
+ messages,
+ model
+}: ManualResearcherConfig): ManualResearcherReturn {
+ try {
+ const currentDate = new Date().toLocaleString()
+
+ return {
+ model: getModel(model),
+ system: `${MANUAL_SYSTEM_PROMPT}\nCurrent date and time: ${currentDate}`,
+ messages,
+ experimental_transform: smoothStream({ chunking: 'word' })
+ }
+ } catch (error) {
+ console.error('Error in manualResearcher:', error)
+ throw error
+ }
+}
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index 4c4ffbb5..bc7ec21c 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -21,7 +21,10 @@ When asked a question, you should:
10. **Use the retrieve tool only with user-provided URLs.**
Citation Format:
-<cite_format>[number](url)</cite_format>
+[number](url)
+
+Image Format:
+![alt text](image_url)
`
type ResearcherReturn = Parameters<typeof streamText>[0]
diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.ts
index 782152cd..decebf05 100644
--- a/lib/streaming/create-manual-tool-stream.ts
+++ b/lib/streaming/create-manual-tool-stream.ts
@@ -3,11 +3,10 @@ import {
createDataStreamResponse,
DataStreamWriter,
JSONValue,
- smoothStream,
streamText
} from 'ai'
+import { manualResearcher } from '../agents/manual-researcher'
import { ExtendedCoreMessage } from '../types'
-import { getModel } from '../utils/registry'
import { handleStreamFinish } from './handle-stream-finish'
import { executeToolCall } from './tool-execution'
import { BaseStreamConfig } from './types'
@@ -17,19 +16,17 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {
execute: async (dataStream: DataStreamWriter) => {
try {
const coreMessages = convertToCoreMessages(config.messages)
- const model = getModel(config.model)
const { toolCallDataAnnotation, toolCallMessages } =
await executeToolCall(coreMessages, dataStream)
- const result = streamText({
- model,
- system:
- 'You are a helpful assistant. You received a search results. You must use the search results to answer the user question.',
+ const researcherConfig = manualResearcher({
messages: [...coreMessages, ...toolCallMessages],
- experimental_transform: smoothStream({
- chunking: 'word'
- }),
+ model: config.model
+ })
+
+ const result = streamText({
+ ...researcherConfig,
onFinish: async result => {
const annotations: ExtendedCoreMessage[] = [
...(toolCallDataAnnotation ? [toolCallDataAnnotation] : []),
diff --git a/lib/utils/registry.ts b/lib/utils/registry.ts
index 3ad6d04c..c6fd4bda 100644
--- a/lib/utils/registry.ts
+++ b/lib/utils/registry.ts
@@ -109,5 +109,8 @@ export function getToolCallModel(model?: string) {
export function isToolCallSupported(model?: string) {
const modelName = model?.split(':')[1]
- return modelName?.includes('deepseek-r1')
+ return (
+ !modelName?.includes('deepseek-r1') &&
+ !modelName?.includes('deepseek-reasoner')
+ )
}