Skip to content

An AI-powered search engine with a generative UI

Add RetrieveSection component and retrieve tool

Yoshiki Miura9f3fa0d55c1b
diff --git a/app/actions.tsx b/app/actions.tsx
index a75d14fa..6bec8354 100644
--- a/app/actions.tsx
+++ b/app/actions.tsx
@@ -20,6 +20,7 @@ import { BotMessage } from '@/components/message'
import { SearchSection } from '@/components/search-section'
import SearchRelated from '@/components/search-related'
import { CopilotDisplay } from '@/components/copilot-display'
+import RetrieveSection from '@/components/retrieve-section'
async function submit(formData?: FormData, skip?: boolean) {
'use server'
@@ -122,7 +123,7 @@ async function submit(formData?: FormData, skip?: boolean) {
while (
useSpecificAPI
? toolOutputs.length === 0 && answer.length === 0
- : answer.length === 0
+ : answer.length === 0 && !errorOccurred
) {
// Search the web and generate the answer
const { fullResponse, hasError, toolResponses } = await researcher(
@@ -391,6 +392,12 @@ export const getUIStateFromAIState = (aiState: Chat) => {
component: <SearchSection result={searchResults.value} />,
isCollapsed: isCollapsed.value
}
+ case 'retrieve':
+ return {
+ id,
+ component: <RetrieveSection data={toolOutput} />,
+ isCollapsed: isCollapsed.value
+ }
}
} catch (error) {
return {
diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsx
new file mode 100644
index 00000000..d9c4c404
--- /dev/null
+++ b/components/retrieve-section.tsx
@@ -0,0 +1,18 @@
+import React from 'react'
+import { Section } from '@/components/section'
+import { SearchResults } from '@/components/search-results'
+import { SearchResults as SearchResultsType } from '@/lib/types'
+
+interface RetrieveSectionProps {
+ data: SearchResultsType
+}
+
+const RetrieveSection: React.FC<RetrieveSectionProps> = ({ data }) => {
+ return (
+ <Section title="Sources">
+ <SearchResults results={data.results} />
+ </Section>
+ )
+}
+
+export default RetrieveSection
diff --git a/components/search-results.tsx b/components/search-results.tsx
index 75c11bfd..ea944787 100644
--- a/components/search-results.tsx
+++ b/components/search-results.tsx
@@ -28,7 +28,9 @@ export function SearchResults({ results }: SearchResultsProps) {
<Link href={result.url} passHref target="_blank">
<Card className="flex-1">
<CardContent className="p-2">
- <p className="text-xs line-clamp-2">{result.content}</p>
+ <p className="text-xs line-clamp-2">
+ {result.title || result.content}
+ </p>
<div className="mt-2 flex items-center space-x-2">
<Avatar className="h-4 w-4">
<AvatarImage
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
index 5d0b6398..5e08e541 100644
--- a/lib/agents/researcher.tsx
+++ b/lib/agents/researcher.tsx
@@ -31,7 +31,7 @@ export async function researcher(
)
let isFirstToolResponse = true
- const currentDate = new Date().toLocaleString();
+ const currentDate = new Date().toLocaleString()
const result = await nonexperimental_streamText({
model: openai.chat(process.env.OPENAI_API_MODEL || 'gpt-4o'),
maxTokens: 2500,
@@ -46,7 +46,6 @@ export async function researcher(
tools: getTools({
uiStream,
fullResponse,
- hasError,
isFirstToolResponse
})
})
@@ -73,9 +72,12 @@ export async function researcher(
break
case 'tool-result':
// Append the answer section if the specific model is not used
- if (!useSpecificModel && toolResponses.length === 0) {
+ if (!useSpecificModel && toolResponses.length === 0 && delta.result) {
uiStream.append(answerSection)
}
+ if (!delta.result) {
+ hasError = true
+ }
toolResponses.push(delta)
break
case 'error':
diff --git a/lib/agents/tools/index.tsx b/lib/agents/tools/index.tsx
index 3e734786..4628e777 100644
--- a/lib/agents/tools/index.tsx
+++ b/lib/agents/tools/index.tsx
@@ -1,23 +1,33 @@
-import { searchTool } from './search'
import { createStreamableUI } from 'ai/rsc'
+import { retrieveTool } from './retrieve'
+import { searchTool } from './search'
-interface GetToolsProps {
+export interface ToolsProps {
uiStream: ReturnType<typeof createStreamableUI>
fullResponse: string
- hasError: boolean
isFirstToolResponse: boolean
}
export const getTools = ({
uiStream,
fullResponse,
- hasError,
isFirstToolResponse
-}: GetToolsProps) => ({
- search: searchTool({
- uiStream,
- fullResponse,
- hasError,
- isFirstToolResponse
- })
-})
+}: ToolsProps) => {
+ const tools: any = {
+ search: searchTool({
+ uiStream,
+ fullResponse,
+ isFirstToolResponse
+ })
+ }
+
+ if (process.env.EXA_API_KEY) {
+ tools.retrieve = retrieveTool({
+ uiStream,
+ fullResponse,
+ isFirstToolResponse
+ })
+ }
+
+ return tools
+}
diff --git a/lib/agents/tools/retrieve.tsx b/lib/agents/tools/retrieve.tsx
new file mode 100644
index 00000000..79b355a1
--- /dev/null
+++ b/lib/agents/tools/retrieve.tsx
@@ -0,0 +1,78 @@
+import { retrieveSchema } from '@/lib/schema/retrieve'
+import { ToolsProps } from '.'
+import { Card } from '@/components/ui/card'
+import { SearchSkeleton } from '@/components/search-skeleton'
+import { SearchResults as SearchResultsType } from '@/lib/types'
+import Exa from 'exa-js'
+import RetrieveSection from '@/components/retrieve-section'
+
+const apiKey = process.env.EXA_API_KEY
+const exa = new Exa(apiKey)
+
+export const retrieveTool = ({
+ uiStream,
+ fullResponse,
+ isFirstToolResponse
+}: ToolsProps) => ({
+ description: 'Retrieve content from the web',
+ parameters: retrieveSchema,
+ execute: async ({ urls }: { urls: string[] }) => {
+ let hasError = false
+ // If this is the first tool response, remove spinner
+ if (isFirstToolResponse) {
+ isFirstToolResponse = false
+ uiStream.update(null)
+ }
+ // Append the search section
+ uiStream.append(<SearchSkeleton />)
+
+ let results: SearchResultsType | undefined
+ try {
+ const data = await exa.getContents(urls)
+
+ if (data.results.length === 0) {
+ hasError = true
+ } else {
+ results = {
+ results: data.results.map((result: any) => ({
+ title: result.title,
+ content: result.text,
+ url: result.url
+ })),
+ query: '',
+ images: []
+ }
+ }
+ } catch (error) {
+ hasError = true
+ console.error('Retrieve API error:', error)
+
+ fullResponse += `\n${error} "${urls.join(', ')}".`
+
+ uiStream.update(
+ <Card className="p-4 mt-2 text-sm">
+ {`${error} "${urls.join(', ')}".`}
+ </Card>
+ )
+ return results
+ }
+
+ if (hasError || !results) {
+ fullResponse += `\nAn error occurred while retrieving "${urls.join(
+ ', '
+ )}".`
+ uiStream.update(
+ <Card className="p-4 mt-2 text-sm">
+ {`An error occurred while retrieving "${urls.join(
+ ', '
+ )}".This webiste may not be supported.`}
+ </Card>
+ )
+ return results
+ }
+
+ uiStream.update(<RetrieveSection data={results} />)
+
+ return results
+ }
+})
diff --git a/lib/agents/tools/search.tsx b/lib/agents/tools/search.tsx
index 377824c1..9b66d8d8 100644
--- a/lib/agents/tools/search.tsx
+++ b/lib/agents/tools/search.tsx
@@ -1,22 +1,15 @@
-import { createStreamableUI, createStreamableValue } from 'ai/rsc'
+import { createStreamableValue } from 'ai/rsc'
import Exa from 'exa-js'
import { searchSchema } from '@/lib/schema/search'
import { Card } from '@/components/ui/card'
import { SearchSection } from '@/components/search-section'
-
-interface searchToolProps {
- uiStream: ReturnType<typeof createStreamableUI>
- fullResponse: string
- hasError: boolean
- isFirstToolResponse: boolean
-}
+import { ToolsProps } from '.'
export const searchTool = ({
uiStream,
fullResponse,
- hasError,
isFirstToolResponse
-}: searchToolProps) => ({
+}: ToolsProps) => ({
description: 'Search the web for information',
parameters: searchSchema,
execute: async ({
@@ -28,6 +21,7 @@ export const searchTool = ({
max_results: number
search_depth: 'basic' | 'advanced'
}) => {
+ let hasError = false
// If this is the first tool response, remove spinner
if (isFirstToolResponse) {
isFirstToolResponse = false
diff --git a/lib/schema/retrieve.tsx b/lib/schema/retrieve.tsx
new file mode 100644
index 00000000..d52cd8f7
--- /dev/null
+++ b/lib/schema/retrieve.tsx
@@ -0,0 +1,8 @@
+import { DeepPartial } from 'ai'
+import { z } from 'zod'
+
+export const retrieveSchema = z.object({
+ urls: z.array(z.string().url()).describe('The urls to retrieve')
+})
+
+export type PartialInquiry = DeepPartial<typeof retrieveSchema>