Skip to content

An AI-powered search engine with a generative UI

Support use of Google provider

Yoshiki Miuraad179be20e6f
diff --git a/app/actions.tsx b/app/actions.tsx
index 2e4dffc1..7cfa6202 100644
--- a/app/actions.tsx
+++ b/app/actions.tsx
@@ -22,6 +22,7 @@ import SearchRelated from '@/components/search-related'
import { CopilotDisplay } from '@/components/copilot-display'
import RetrieveSection from '@/components/retrieve-section'
import { VideoSearchSection } from '@/components/video-search-section'
+import { transformToolMessages } from '@/lib/utils'
async function submit(formData?: FormData, skip?: boolean) {
'use server'
@@ -164,16 +165,7 @@ async function submit(formData?: FormData, skip?: boolean) {
// If useSpecificAPI is enabled, generate the answer using the specific model
if (useSpecificAPI && answer.length === 0) {
// modify the messages to be used by the specific model
- const modifiedMessages = aiState.get().messages.map(msg =>
- msg.role === 'tool'
- ? {
- ...msg,
- role: 'assistant',
- content: JSON.stringify(msg.content),
- type: 'tool'
- }
- : msg
- ) as CoreMessage[]
+ const modifiedMessages = transformToolMessages(aiState.get().messages)
const latestMessages = modifiedMessages.slice(maxMessages * -1)
const { response, hasError } = await writer(
uiStream,
@@ -187,8 +179,15 @@ async function submit(formData?: FormData, skip?: boolean) {
}
if (!errorOccurred) {
+ const useGoogleProvider = process.env.GOOGLE_GENERATIVE_AI_API_KEY
+ let processedMessages = messages
+ // If using Google provider, we need to modify the messages
+ if (useGoogleProvider) {
+ processedMessages = transformToolMessages(aiState.get().messages)
+ }
+
// Generate related queries
- const relatedQueries = await querySuggestor(uiStream, messages)
+ const relatedQueries = await querySuggestor(uiStream, processedMessages)
// Add follow-up panel
uiStream.append(
<Section title="Follow-up">
@@ -272,7 +271,7 @@ export const AI = createAI<AIState, UIState>({
const aiState = getAIState()
if (aiState) {
- const uiState = getUIStateFromAIState(aiState)
+ const uiState = getUIStateFromAIState(aiState as Chat)
return uiState
} else {
return
diff --git a/bun.lockb b/bun.lockb
index d5d60d9e..fcbb3f9d 100755
Binary files a/bun.lockb and b/bun.lockb differ
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
index 7ca6a921..92a5a996 100644
--- a/lib/agents/researcher.tsx
+++ b/lib/agents/researcher.tsx
@@ -40,7 +40,8 @@ export async function researcher(
tools: getTools({
uiStream,
fullResponse
- })
+ }),
+ toolChoice: 'required'
}).catch(err => {
hasError = true
fullResponse = 'Error: ' + err.message
diff --git a/lib/schema/search.tsx b/lib/schema/search.tsx
index 0ced1865..6001e288 100644
--- a/lib/schema/search.tsx
+++ b/lib/schema/search.tsx
@@ -3,14 +3,9 @@ import { z } from 'zod'
export const searchSchema = z.object({
query: z.string().describe('The query to search for'),
- max_results: z
- .number()
- .max(20)
- .default(5)
- .describe('The maximum number of results to return'),
+ max_results: z.number().describe('The maximum number of results to return'),
search_depth: z
.enum(['basic', 'advanced'])
- .default('basic')
.describe('The depth of the search')
})
diff --git a/lib/utils/index.ts b/lib/utils/index.ts
index 7ef64012..97715968 100644
--- a/lib/utils/index.ts
+++ b/lib/utils/index.ts
@@ -2,7 +2,8 @@ import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { OpenAI } from '@ai-sdk/openai'
import { createGoogleGenerativeAI } from '@ai-sdk/google'
-import { createAnthropic } from '@ai-sdk/anthropic'
+import { AIMessage } from '../types'
+import { CoreMessage } from 'ai'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
@@ -10,15 +11,10 @@ export function cn(...inputs: ClassValue[]) {
export function getModel() {
// Currently does not work with Google or Anthropic
- // if (process.env.GOOGLE_GENERATIVE_AI_API_KEY) {
- // const google = createGoogleGenerativeAI()
- // return google('models/gemini-1.5-pro-latest')
- // }
-
- // if (process.env.ANTHROPIC_API_KEY) {
- // const anthropic = createAnthropic()
- // return anthropic('claude-3-haiku-20240307')
- // }
+ if (process.env.GOOGLE_GENERATIVE_AI_API_KEY) {
+ const google = createGoogleGenerativeAI()
+ return google('models/gemini-1.5-pro-latest')
+ }
const openai = new OpenAI({
baseUrl: process.env.OPENAI_API_BASE, // optional base URL for proxies etc.
@@ -27,3 +23,24 @@ export function getModel() {
})
return openai.chat(process.env.OPENAI_API_MODEL || 'gpt-4o')
}
+
+/**
+ * Takes an array of AIMessage and modifies each message where the role is 'tool'.
+ * Changes the role to 'assistant' and converts the content to a JSON string.
+ * Returns the modified messages as an array of CoreMessage.
+ *
+ * @param aiMessages - Array of AIMessage
+ * @returns modifiedMessages - Array of modified messages
+ */
+export function transformToolMessages(aiMessages: AIMessage[]): CoreMessage[] {
+ return aiMessages.map(message =>
+ message.role === 'tool'
+ ? {
+ ...message,
+ role: 'assistant',
+ content: JSON.stringify(message.content),
+ type: 'tool'
+ }
+ : message
+ ) as CoreMessage[]
+}