Skip to content

Commit

Permalink
Refactor retrieveTool to use jina.ai
Browse files Browse the repository at this point in the history
  • Loading branch information
miurla committed May 18, 2024
1 parent 6d40d74 commit 3db99cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
6 changes: 1 addition & 5 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ UPSTASH_REDIS_REST_TOKEN=[YOUR_UPSTASH_REDIS_REST_TOKEN]

# enable the share feature
# If you enable this feature, separate account management implementation is required.
# ENABLE_SHARE=true

# use the retrieve tool
# Exa API Key retrieved here: https://dashboard.exa.ai/api-keys
# EXA_API_KEY=[YOUR_EXA_API_KEY]
# ENABLE_SHARE=true
42 changes: 20 additions & 22 deletions lib/agents/tools/retrieve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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'

export const retrieveTool = ({
Expand All @@ -13,10 +12,8 @@ export const retrieveTool = ({
}: ToolsProps) => ({
description: 'Retrieve content from the web',
parameters: retrieveSchema,
execute: async ({ urls }: { urls: string[] }) => {
execute: async ({ url }: { url: string }) => {
let hasError = false
const apiKey = process.env.EXA_API_KEY
const exa = new Exa(apiKey)

// If this is the first tool response, remove spinner
if (isFirstToolResponse) {
Expand All @@ -28,17 +25,24 @@ export const retrieveTool = ({

let results: SearchResultsType | undefined
try {
const data = await exa.getContents(urls)

if (data.results.length === 0) {
const response = await fetch(`https://r.jina.ai/${url}`, {
method: 'GET',
headers: {
Accept: 'application/json'
}
})
const json = await response.json()
if (!json.data || json.data.length === 0) {
hasError = true
} else {
results = {
results: data.results.map((result: any) => ({
title: result.title,
content: result.text,
url: result.url
})),
results: [
{
title: json.data.title,
content: json.data.content,
url: json.data.url
}
],
query: '',
images: []
}
Expand All @@ -47,25 +51,19 @@ export const retrieveTool = ({
hasError = true
console.error('Retrieve API error:', error)

fullResponse += `\n${error} "${urls.join(', ')}".`
fullResponse += `\n${error} "${url}".`

uiStream.update(
<Card className="p-4 mt-2 text-sm">
{`${error} "${urls.join(', ')}".`}
</Card>
<Card className="p-4 mt-2 text-sm">{`${error} "${url}".`}</Card>
)
return results
}

if (hasError || !results) {
fullResponse += `\nAn error occurred while retrieving "${urls.join(
', '
)}".`
fullResponse += `\nAn error occurred while retrieving "${url}".`
uiStream.update(
<Card className="p-4 mt-2 text-sm">
{`An error occurred while retrieving "${urls.join(
', '
)}".This webiste may not be supported.`}
{`An error occurred while retrieving "${url}".This webiste may not be supported.`}
</Card>
)
return results
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/retrieve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DeepPartial } from 'ai'
import { z } from 'zod'

export const retrieveSchema = z.object({
urls: z.array(z.string().url()).describe('The urls to retrieve')
url: z.string().url().describe('The url to retrieve')
})

export type PartialInquiry = DeepPartial<typeof retrieveSchema>

0 comments on commit 3db99cb

Please # to comment.