Skip to content

An AI-powered search engine with a generative UI

Update Ollama provider messages in researcher agent

Yoshiki Miura2e0edc3da136
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
index 5cd2ec7c..92590828 100644
--- a/lib/agents/researcher.tsx
+++ b/lib/agents/researcher.tsx
@@ -3,7 +3,7 @@ import { CoreMessage, ToolCallPart, ToolResultPart, streamText } from 'ai'
import { Section } from '@/components/section'
import { BotMessage } from '@/components/message'
import { getTools } from './tools'
-import { getModel } from '../utils'
+import { getModel, transformToolMessages } from '../utils'
export async function researcher(
uiStream: ReturnType<typeof createStreamableUI>,
@@ -19,6 +19,15 @@ export async function researcher(
</Section>
)
+ // Transform the messages if using Ollama provider
+ let processedMessages = messages
+ const useOllamaProvider = !!(
+ process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL
+ )
+ if (useOllamaProvider) {
+ processedMessages = transformToolMessages(messages)
+ }
+
const currentDate = new Date().toLocaleString()
const result = await streamText({
model: getModel(),
@@ -31,7 +40,7 @@ export async function researcher(
Whenever quoting or referencing information from a specific URL, always cite the source URL explicitly.
The retrieve tool can only be used with URLs provided by the user. URLs from search results cannot be used.
Please match the language of the response to the user's language. Current date and time: ${currentDate}`,
- messages,
+ messages: processedMessages,
tools: getTools({
uiStream,
fullResponse
diff --git a/lib/utils/index.ts b/lib/utils/index.ts
index b69fec16..2fa6a1f5 100644
--- a/lib/utils/index.ts
+++ b/lib/utils/index.ts
@@ -1,7 +1,7 @@
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { OpenAI } from '@ai-sdk/openai'
-import { createOllama } from 'ollama-ai-provider';
+import { createOllama } from 'ollama-ai-provider'
import { google } from '@ai-sdk/google'
import { AIMessage } from '../types'
import { CoreMessage } from 'ai'
@@ -11,25 +11,22 @@ export function cn(...inputs: ClassValue[]) {
}
export function getModel() {
- const ollamaBaseUrl = process.env.OLLAMA_BASE_URL + "/api";
- const ollamaModel = process.env.OLLAMA_MODEL;
- const openaiApiBase = process.env.OPENAI_API_BASE;
- const openaiApiKey = process.env.OPENAI_API_KEY;
- let openaiApiModel = process.env.OPENAI_API_MODEL || 'gpt-4o';
+ const ollamaBaseUrl = process.env.OLLAMA_BASE_URL + '/api'
+ const ollamaModel = process.env.OLLAMA_MODEL
+ const openaiApiBase = process.env.OPENAI_API_BASE
+ const openaiApiKey = process.env.OPENAI_API_KEY
+ let openaiApiModel = process.env.OPENAI_API_MODEL || 'gpt-4o'
- if (
- !(ollamaBaseUrl && ollamaModel) &&
- !(openaiApiBase && openaiApiKey)
- ) {
- throw new Error('Missing environment variables for Ollama and OpenAI');
+ if (!(ollamaBaseUrl && ollamaModel) && !(openaiApiBase && openaiApiKey)) {
+ throw new Error('Missing environment variables for Ollama and OpenAI')
}
// Ollama
if (ollamaBaseUrl && ollamaModel) {
- const ollama = createOllama({ baseURL: ollamaBaseUrl });
+ const ollama = createOllama({ baseURL: ollamaBaseUrl })
- return ollama(ollamaModel);
+ return ollama(ollamaModel)
}
if (process.env.GOOGLE_GENERATIVE_AI_API_KEY) {
@@ -44,7 +41,6 @@ export function getModel() {
organization: '' // optional organization
})
-
return openai.chat(openaiApiModel)
}
@@ -56,7 +52,9 @@ export function getModel() {
* @param aiMessages - Array of AIMessage
* @returns modifiedMessages - Array of modified messages
*/
-export function transformToolMessages(aiMessages: AIMessage[]): CoreMessage[] {
+export function transformToolMessages(
+ aiMessages: AIMessage[] | CoreMessage[]
+): CoreMessage[] {
return aiMessages.map(message =>
message.role === 'tool'
? {