Give your Vercel AI SDK apps the context they need. With Valyu’s DeepSearch API, you can plug in real-time, authoritative information from academic papers, market data, web content, and more—no fuss, just answers your users can trust.
1. Get API Key & Install
- Get your Valyu API Key: Head to Valyu Exchange to grab your key (you get $10 free credits!). Store it as an environment variable:
VALYU_API_KEY="your_actual_api_key"
.
- Install Packages:
Here’s the complete tool definition for DeepSearch. This allows your Vercel AI app to call Valyu for various search types.
import { z } from 'zod';
import { tool } from 'ai';
import { Valyu, SearchType as ValyuSearchSDKType } from 'valyu-js';
export const valyuDeepSearchTool = tool({
description: 'Search Valyu for real-time academic papers, web content, market data, etc. Use for specific, up-to-date information across various domains.',
parameters: z.object({
query: z.string().describe('Detailed search query (e.g., "latest advancements in AI for healthcare" or "current price of Bitcoin").'),
searchType: z.enum(['all', 'web', 'market', 'academic', 'proprietary'])
.describe('Search domain: "academic", "web", "market", "all", or "proprietary" for specific Valyu datasets.'),
}),
execute: async ({ query, searchType }) => {
const VALYU_API_KEY = process.env.VALYU_API_KEY;
if (!VALYU_API_KEY) {
console.error('VALYU_API_KEY is not set.');
return JSON.stringify({ success: false, error: 'Valyu API key not configured.', results: [] });
}
const valyu = new Valyu(VALYU_API_KEY);
const searchTypeMap: { [key: string]: ValyuSearchSDKType } = {
all: 'all',
web: 'web',
market: 'all',
academic: 'proprietary',
proprietary: 'proprietary',
};
const mappedSearchType: ValyuSearchSDKType = searchTypeMap[searchType] || 'all';
try {
console.log(`[ValyuDeepSearchTool] Query: "${query}", LLM Type: ${searchType}, Valyu Type: ${mappedSearchType}`);
const response = await valyu.context(
query,
{
searchType: mappedSearchType,
maxNumResults: 5,
maxPrice: 10,
similarityThreshold: 0.3,
queryRewrite: false,
...(searchType === 'academic' ? { dataSources: ['valyu/valyu-arxiv'] } : {})
}
);
if (!response.success) {
console.error('[ValyuDeepSearchTool] API Error:', response.error);
return JSON.stringify({ success: false, error: response.error || 'Valyu API request failed.', query, results: [] });
}
console.log(`[ValyuDeepSearchTool] Success. Results: ${response.results?.length}, TX_ID: ${response.tx_id}`);
return JSON.stringify({ success: true, query, results: response.results || [], tx_id: response.tx_id });
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error.';
console.error('[ValyuDeepSearchTool] Exception:', errorMessage);
return JSON.stringify({ success: false, error: errorMessage, query, results: [] });
}
}
});
Explanation:
- This tool,
valyuDeepSearchTool
, is flexible and takes a searchType
.
- It securely uses
VALYU_API_KEY
.
- Returns a JSON string for the Vercel AI SDK.
3. Integrate Web Search (Copy & Paste)
For simple, dedicated web search, use this specialized tool:
Explanation:
valyuWebSearchTool
is specifically for web searches (searchType: 'web'
).
- It only requires a
query
parameter.
Add one or both tools to your Vercel AI SDK’s streamText
or generateText
call:
5. Basic Prompting
Instruct your LLM to use the appropriate tool:
6. Important Notes
- API Key Security: Always use environment variables for
VALYU_API_KEY
.
- Cost Control: Use the
maxPrice
parameter in valyu.context()
.
- Valyu SDK Type: Ensure
ValyuSearchSDKType
(and ValyuWebSearchSDKType
) correctly refers to the search type enum/type exported by your valyu-js
package.
- Zod Aliases: If defining both tools in the same file/scope, you might need to alias Zod imports (e.g.,
import { z as zodWeb } from 'zod';
) to avoid naming conflicts if you haven’t already.
That’s it! Your Vercel AI application can now leverage Valyu’s DeepSearch.