ollama_web_search
Perform web searches to augment AI models with current information, reducing hallucinations by accessing up-to-date data through API integration.
Instructions
Perform a web search using Ollama's web search API. Augments models with latest information to reduce hallucinations. Requires OLLAMA_API_KEY environment variable.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The search query string | |
| max_results | No | Maximum number of results to return (1-10, default 5) | |
| format | No | json |
Implementation Reference
- src/tools/web-search.ts:10-59 (handler)Core implementation of the web search tool logic. Performs API call to Ollama's web search endpoint with authentication, retry, timeout, error handling, and response formatting./** * Perform a web search using Ollama's web search API */ export async function webSearch( ollama: Ollama, query: string, maxResults: number, format: ResponseFormat ): Promise<string> { // Web search requires direct API call as it's not in the SDK const apiKey = process.env.OLLAMA_API_KEY; if (!apiKey) { throw new Error( 'OLLAMA_API_KEY environment variable is required for web search' ); } return retryWithBackoff( async () => { const response = await fetchWithTimeout( 'https://ollama.com/api/web_search', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}`, }, body: JSON.stringify({ query, max_results: maxResults, }), }, WEB_API_TIMEOUT ); if (!response.ok) { const retryAfter = response.headers.get('retry-after') ?? undefined; throw new HttpError( `Web search failed: ${response.status} ${response.statusText}`, response.status, retryAfter ); } const data = await response.json(); return formatResponse(JSON.stringify(data), format); }, WEB_API_RETRY_CONFIG ); }
- src/tools/web-search.ts:61-89 (registration)Tool registration definition exported for discovery by the autoloader. Includes name, description, inline input schema, and handler that validates inputs using Zod schema and delegates to webSearch function.export const toolDefinition: ToolDefinition = { name: 'ollama_web_search', description: 'Perform a web search using Ollama\'s web search API. Augments models with latest information to reduce hallucinations. Requires OLLAMA_API_KEY environment variable.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The search query string', }, max_results: { type: 'number', description: 'Maximum number of results to return (1-10, default 5)', default: 5, }, format: { type: 'string', enum: ['json', 'markdown'], default: 'json', }, }, required: ['query'], }, handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = WebSearchInputSchema.parse(args); return webSearch(ollama, validated.query, validated.max_results, format); }, };
- src/schemas.ts:208-214 (schema)Zod input validation schema for the ollama_web_search tool, used in the handler for parsing and validating arguments.* Schema for ollama_web_search tool */ export const WebSearchInputSchema = z.object({ query: z.string().min(1), max_results: z.number().int().min(1).max(10).default(5), format: ResponseFormatSchema.default('json'), });