The webSearch tool performs web searches using SerpApi’s Google search engine integration. It returns a list of search results including titles, links, and snippets to provide a comprehensive overview of available information.
export const webSearch = ai.defineTool( { name: 'webSearch', description: 'Searches the web for a given query and returns a list of search results, including organic results, news, and academic papers to get a comprehensive overview.', inputSchema: WebSearchInputSchema, outputSchema: WebSearchOutputSchema, }, async (input) => { ... });
Returns an array of search results conforming to WebSearchOutputSchema:
const WebSearchResultSchema = z.object({ title: z.string().describe('The title of the search result.'), link: z.string().describe('The URL of the search result.'), snippet: z.string().describe('A brief summary of the search result.'),});const WebSearchOutputSchema = z.array(WebSearchResultSchema);
import { webSearch } from '@/ai/tools/web-search';// Perform a web searchconst results = await webSearch({ query: 'climate change evidence' });console.log(results);// [// {// title: 'Climate Change Evidence: How Do We Know?',// link: 'https://climate.nasa.gov/evidence/',// snippet: 'This evidence is compelling: Ninety-seven percent of climate scientists agree that climate-warming trends over the past century...'// },// // ... up to 7 results// ]