Skip to content

An AI-powered search engine with a generative UI

feat: Add manual tool stream

Yoshiki Miura678cd985fdd3
diff --git a/bun.lockb b/bun.lockb
index 00802ea0..98c3f94f 100755
Binary files a/bun.lockb and b/bun.lockb differ
diff --git a/components/render-message.tsx b/components/render-message.tsx
index f6e269d5..1d053238 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -1,8 +1,8 @@
import { Message } from 'ai'
-import { UserMessage } from './user-message'
-import { ToolSection } from './tool-section'
import { AnswerSection } from './answer-section'
import RelatedQuestions from './related-questions'
+import { ToolSection } from './tool-section'
+import { UserMessage } from './user-message'
interface RenderMessageProps {
message: Message
@@ -42,6 +42,7 @@ export function RenderMessage({
return (
<>
+ {message.reasoning && <pre>{message.reasoning}</pre>}
<AnswerSection
content={message.content}
isOpen={getIsOpen(messageId)}
diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.ts
index eaff69cd..23715075 100644
--- a/lib/streaming/create-manual-tool-stream.ts
+++ b/lib/streaming/create-manual-tool-stream.ts
@@ -1,11 +1,39 @@
-import { createDataStreamResponse, DataStreamWriter } from 'ai'
+import {
+ convertToCoreMessages,
+ createDataStreamResponse,
+ DataStreamWriter,
+ streamText
+} from 'ai'
+import { getModel } from '../utils/registry'
+import { handleStreamFinish } from './handle-stream-finish'
import { BaseStreamConfig } from './types'
export function createManualToolStreamResponse(config: BaseStreamConfig) {
return createDataStreamResponse({
execute: async (dataStream: DataStreamWriter) => {
try {
- // TODO: Implement manual tool calling
+ const coreMessages = convertToCoreMessages(config.messages)
+ console.log(config.model)
+
+ const result = streamText({
+ model: getModel(config.model),
+ system: 'You are a helpful assistant.',
+ messages: coreMessages,
+ onFinish: async result => {
+ await handleStreamFinish({
+ responseMessages: result.response.messages,
+ originalMessages: config.messages,
+ model: config.model,
+ chatId: config.chatId,
+ dataStream,
+ skipRelatedQuestions: true
+ })
+ }
+ })
+
+ result.mergeIntoDataStream(dataStream, {
+ sendReasoning: true
+ })
} catch (error) {
console.error('Stream execution error:', error)
}
diff --git a/lib/streaming/handle-stream-finish.ts b/lib/streaming/handle-stream-finish.ts
index 4bdbed7a..fa2c2b9b 100644
--- a/lib/streaming/handle-stream-finish.ts
+++ b/lib/streaming/handle-stream-finish.ts
@@ -10,6 +10,7 @@ interface HandleStreamFinishParams {
model: string
chatId: string
dataStream: DataStreamWriter
+ skipRelatedQuestions?: boolean
}
export async function handleStreamFinish({
@@ -17,7 +18,8 @@ export async function handleStreamFinish({
originalMessages,
model,
chatId,
- dataStream
+ dataStream,
+ skipRelatedQuestions = false
}: HandleStreamFinishParams) {
try {
const extendedCoreMessages = convertToExtendedCoreMessages(originalMessages)
@@ -29,32 +31,38 @@ export async function handleStreamFinish({
}
}
- // Notify related questions loading
- dataStream.writeMessageAnnotation(annotation)
+ if (!skipRelatedQuestions) {
+ // Notify related questions loading
+ dataStream.writeMessageAnnotation(annotation)
- // Generate related questions
- const relatedQuestions = await generateRelatedQuestions(
- responseMessages,
- model
- )
+ // Generate related questions
+ const relatedQuestions = await generateRelatedQuestions(
+ responseMessages,
+ model
+ )
- // Update the annotation with the related questions
- annotation = {
- ...annotation,
- data: relatedQuestions.object
- }
+ // Update the annotation with the related questions
+ annotation = {
+ ...annotation,
+ data: relatedQuestions.object
+ }
- // Send related questions to client
- dataStream.writeMessageAnnotation(annotation)
+ // Send related questions to client
+ dataStream.writeMessageAnnotation(annotation)
+ }
// Create the message to save
const generatedMessages = [
...extendedCoreMessages,
...responseMessages.slice(0, -1),
- {
- role: 'data',
- content: annotation
- } as ExtendedCoreMessage,
+ ...(skipRelatedQuestions
+ ? []
+ : [
+ {
+ role: 'data',
+ content: annotation
+ } as ExtendedCoreMessage
+ ]),
responseMessages[responseMessages.length - 1]
] as ExtendedCoreMessage[]
diff --git a/lib/types/models.ts b/lib/types/models.ts
index f451e006..46bab261 100644
--- a/lib/types/models.ts
+++ b/lib/types/models.ts
@@ -49,8 +49,14 @@ export const models: Model[] = [
providerId: 'groq'
},
{
- id: 'qwen2.5',
- name: 'Qwen 2.5',
+ id: 'deepseek-r1-distill-llama-70b',
+ name: 'DeepSeek R1 Distill Llama 70B',
+ provider: 'Groq',
+ providerId: 'groq'
+ },
+ {
+ id: 'deepseek-r1',
+ name: 'DeepSeek R1',
provider: 'Ollama',
providerId: 'ollama'
},
@@ -62,12 +68,18 @@ export const models: Model[] = [
},
// Deepseek function calling is currently unstable: https://github.com/vercel/ai/issues/4313#issuecomment-2587891644
// If you want to use Deepseek, remove the comment and add it to the models array
- // {
- // id: 'deepseek-chat',
- // name: 'DeepSeek v3',
- // provider: 'DeepSeek',
- // providerId: 'deepseek'
- // },
+ {
+ id: 'deepseek-chat',
+ name: 'DeepSeek v3',
+ provider: 'DeepSeek',
+ providerId: 'deepseek'
+ },
+ {
+ id: 'deepseek-reasoner',
+ name: 'DeepSeek R1',
+ provider: 'DeepSeek',
+ providerId: 'deepseek'
+ },
{
id: process.env.NEXT_PUBLIC_OPENAI_COMPATIBLE_MODEL || 'undefined',
name: process.env.NEXT_PUBLIC_OPENAI_COMPATIBLE_MODEL || 'Undefined',
diff --git a/lib/utils/registry.ts b/lib/utils/registry.ts
index bae11dbf..60640b38 100644
--- a/lib/utils/registry.ts
+++ b/lib/utils/registry.ts
@@ -2,18 +2,20 @@ import { anthropic } from '@ai-sdk/anthropic'
import { createAzure } from '@ai-sdk/azure'
import { deepseek } from '@ai-sdk/deepseek'
import { google } from '@ai-sdk/google'
+import { groq } from '@ai-sdk/groq'
import { createOpenAI, openai } from '@ai-sdk/openai'
-import { experimental_createProviderRegistry as createProviderRegistry } from 'ai'
+import {
+ experimental_createProviderRegistry as createProviderRegistry,
+ extractReasoningMiddleware,
+ experimental_wrapLanguageModel as wrapLanguageModel
+} from 'ai'
import { createOllama } from 'ollama-ai-provider'
export const registry = createProviderRegistry({
openai,
anthropic,
google,
- groq: createOpenAI({
- apiKey: process.env.GROQ_API_KEY,
- baseURL: 'https://api.groq.com/openai/v1'
- }),
+ groq,
ollama: createOllama({
baseURL: `${process.env.OLLAMA_BASE_URL}/api`
}),
@@ -29,17 +31,39 @@ export const registry = createProviderRegistry({
})
export function getModel(model: string) {
- // if ollama provider, set simulateStreaming to true
if (model.includes('ollama')) {
const modelName = model.split(':')[1]
const ollama = createOllama({
baseURL: `${process.env.OLLAMA_BASE_URL}/api`
})
+
+ // if model is deepseek-r1, add reasoning middleware
+ if (model.includes('deepseek-r1')) {
+ return wrapLanguageModel({
+ model: ollama(modelName),
+ middleware: extractReasoningMiddleware({
+ tagName: 'think'
+ })
+ })
+ }
+
+ // if ollama provider, set simulateStreaming to true
return ollama(modelName, {
simulateStreaming: true
})
}
+ // if model is groq and includes deepseek-r1, add reasoning middleware
+ if (model.includes('groq') && model.includes('deepseek-r1')) {
+ const modelName = model.split(':')[1]
+ return wrapLanguageModel({
+ model: groq(modelName),
+ middleware: extractReasoningMiddleware({
+ tagName: 'think'
+ })
+ })
+ }
+
return registry.languageModel(model)
}
diff --git a/package.json b/package.json
index 959ff79f..d8548635 100644
--- a/package.json
+++ b/package.json
@@ -10,11 +10,12 @@
"lint": "next lint"
},
"dependencies": {
- "@ai-sdk/anthropic": "^1.1.0",
- "@ai-sdk/azure": "^1.1.0",
- "@ai-sdk/deepseek": "^0.1.0",
- "@ai-sdk/google": "^1.1.0",
- "@ai-sdk/openai": "^1.1.0",
+ "@ai-sdk/anthropic": "^1.1.5",
+ "@ai-sdk/azure": "^1.1.5",
+ "@ai-sdk/deepseek": "^0.1.6",
+ "@ai-sdk/google": "^1.1.5",
+ "@ai-sdk/groq": "^1.1.6",
+ "@ai-sdk/openai": "^1.1.5",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-checkbox": "^1.0.4",
@@ -30,7 +31,7 @@
"@radix-ui/react-tooltip": "^1.0.7",
"@tailwindcss/typography": "^0.5.12",
"@upstash/redis": "^1.34.0",
- "ai": "^4.1.0",
+ "ai": "^4.1.9",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"embla-carousel-react": "^8.0.0",