Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor researcher function and handle search errors #28

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions app/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,31 @@ async function submit(formData?: FormData, skip?: boolean) {

// Generate the answer
let answer = ''
let errorOccurred = false
const streamText = createStreamableValue<string>()
while (answer.length === 0) {
// Search the web and generate the answer
const { fullResponse } = await researcher(uiStream, streamText, messages)
const { fullResponse, hasError } = await researcher(
uiStream,
streamText,
messages
)
answer = fullResponse
errorOccurred = hasError
}
streamText.done()

// Generate related queries
await querySuggestor(uiStream, messages)
if (!errorOccurred) {
// Generate related queries
await querySuggestor(uiStream, messages)

// Add follow-up panel
uiStream.append(
<Section title="Follow-up">
<FollowupPanel />
</Section>
)
// Add follow-up panel
uiStream.append(
<Section title="Follow-up">
<FollowupPanel />
</Section>
)
}

isGenerating.done(false)
uiStream.done()
Expand Down
32 changes: 27 additions & 5 deletions lib/agents/researcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SearchResults } from '@/components/search-results'
import { BotMessage } from '@/components/message'
import Exa from 'exa-js'
import { SearchResultsImageSection } from '@/components/search-results-image'
import { Card } from '@/components/ui/card'

export async function researcher(
uiStream: ReturnType<typeof createStreamableUI>,
Expand All @@ -29,6 +30,7 @@ export async function researcher(
const searchAPI: 'tavily' | 'exa' = 'tavily'

let fullResponse = ''
let hasError = false
const answerSection = (
<Section title="Answer">
<BotMessage content={streamText.value} />
Expand Down Expand Up @@ -70,10 +72,29 @@ export async function researcher(
</Section>
)

const searchResult =
searchAPI === 'tavily'
? await tavilySearch(query, max_results, search_depth)
: await exaSearch(query)
// Tavily API requires a minimum of 5 characters in the query
const filledQuery =
query.length < 5 ? query + ' '.repeat(5 - query.length) : query
let searchResult
try {
searchResult =
searchAPI === 'tavily'
? await tavilySearch(filledQuery, max_results, search_depth)
: await exaSearch(query)
} catch (error) {
console.error('Search API error:', error)
hasError = true
}

if (hasError) {
fullResponse += `\nAn error occurred while searching for "${query}.`
uiStream.update(
<Card className="p-4 mt-2 text-sm">
{`An error occurred while searching for "${query}".`}
</Card>
)
return searchResult
}

uiStream.update(
<Section title="Images">
Expand Down Expand Up @@ -120,6 +141,7 @@ export async function researcher(
toolResponses.push(delta)
break
case 'error':
hasError = true
fullResponse += `\nError occurred while executing the tool`
break
}
Expand All @@ -134,7 +156,7 @@ export async function researcher(
messages.push({ role: 'tool', content: toolResponses })
}

return { result, fullResponse }
return { result, fullResponse, hasError }
}

async function tavilySearch(
Expand Down