Skip to content

An AI-powered search engine with a generative UI

feat: Support the include_image_descriptions parameter of Tavily

Yoshiki Miuraaea60f01ff9d
diff --git a/components/search-results-image.tsx b/components/search-results-image.tsx
index 5a3857ab..ac4c412b 100644
--- a/components/search-results-image.tsx
+++ b/components/search-results-image.tsx
@@ -20,9 +20,10 @@ import {
} from '@/components/ui/carousel'
import { useEffect, useState } from 'react'
import { PlusCircle } from 'lucide-react'
+import { SearchResultImage } from '@/lib/types'
interface SearchResultsImageSectionProps {
- images: string[]
+ images: SearchResultImage[]
query?: string
}
@@ -59,9 +60,21 @@ export const SearchResultsImageSection: React.FC<
return <div className="text-muted-foreground">No images found</div>
}
+ // If enabled the include_images_description is true, the images will be an array of { url: string, description: string }
+ // Otherwise, the images will be an array of strings
+ let convertedImages: { url: string; description: string }[] = []
+ if (typeof images[0] === 'string') {
+ convertedImages = (images as string[]).map(image => ({
+ url: image,
+ description: ''
+ }))
+ } else {
+ convertedImages = images as { url: string; description: string }[]
+ }
+
return (
<div className="flex flex-wrap gap-2">
- {images.slice(0, 4).map((image, index) => (
+ {convertedImages.slice(0, 4).map((image, index) => (
<Dialog key={index}>
<DialogTrigger asChild>
<div
@@ -72,7 +85,7 @@ export const SearchResultsImageSection: React.FC<
<CardContent className="p-2 h-full w-full">
{image ? (
<img
- src={image}
+ src={image.url}
alt={`Image ${index + 1}`}
className="h-full w-full object-cover"
onError={e =>
@@ -102,11 +115,11 @@ export const SearchResultsImageSection: React.FC<
className="w-full bg-muted max-h-[60vh]"
>
<CarouselContent>
- {images.map((img, idx) => (
+ {convertedImages.map((img, idx) => (
<CarouselItem key={idx}>
<div className="p-1 flex items-center justify-center h-full">
<img
- src={img}
+ src={img.url}
alt={`Image ${idx + 1}`}
className="h-auto w-full object-contain max-h-[60vh]"
onError={e =>
diff --git a/lib/agents/tools/search.tsx b/lib/agents/tools/search.tsx
index d7a0eda7..69bc191e 100644
--- a/lib/agents/tools/search.tsx
+++ b/lib/agents/tools/search.tsx
@@ -5,7 +5,7 @@ import { searchSchema } from '@/lib/schema/search'
import { SearchSection } from '@/components/search-section'
import { ToolProps } from '.'
import { sanitizeUrl } from '@/lib/utils'
-import { SearchResults } from '@/lib/types'
+import { SearchResultImage, SearchResults } from '@/lib/types'
export const searchTool = ({ uiStream, fullResponse }: ToolProps) =>
tool({
@@ -70,6 +70,7 @@ async function tavilySearch(
excludeDomains: string[] = []
): Promise<any> {
const apiKey = process.env.TAVILY_API_KEY
+ const includeImageDescriptions = true
const response = await fetch('https://api.tavily.com/search', {
method: 'POST',
headers: {
@@ -81,6 +82,7 @@ async function tavilySearch(
max_results: maxResults < 5 ? 5 : maxResults,
search_depth: searchDepth,
include_images: true,
+ include_image_descriptions: includeImageDescriptions,
include_answers: true,
include_domains: includeDomains,
exclude_domains: excludeDomains
@@ -91,14 +93,30 @@ async function tavilySearch(
throw new Error(`Error: ${response.status}`)
}
- // sanitize the image urls
const data = await response.json()
- const sanitizedData: SearchResults = {
+
+ const processedImages = includeImageDescriptions
+ ? data.images
+ .map(({ url, description }: { url: string; description: string }) => ({
+ url: sanitizeUrl(url),
+ description
+ }))
+ .filter(
+ (
+ image: SearchResultImage
+ ): image is { url: string; description: string } =>
+ typeof image === 'object' &&
+ image.description !== undefined &&
+ image.description !== ''
+ )
+ : data.images.map((url: string) => sanitizeUrl(url))
+
+ const convertedData: SearchResults = {
...data,
- images: data.images.map((url: any) => sanitizeUrl(url))
+ images: processedImages
}
- return sanitizedData
+ return convertedData
}
async function exaSearch(
diff --git a/lib/types/index.ts b/lib/types/index.ts
index 12593c1e..b8d65aa1 100644
--- a/lib/types/index.ts
+++ b/lib/types/index.ts
@@ -1,9 +1,18 @@
export type SearchResults = {
- images: string[]
+ images: SearchResultImage[]
results: SearchResultItem[]
query: string
}
+// If enabled the include_images_description is true, the images will be an array of { url: string, description: string }
+// Otherwise, the images will be an array of strings
+export type SearchResultImage =
+ | string
+ | {
+ url: string
+ description: string
+ }
+
export type ExaSearchResults = {
results: ExaSearchResultItem[]
}