Implementation:Ollama Ollama XTools WebSearch
| Knowledge Sources | |
|---|---|
| Domains | Agent Loop, Web Integration |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Implements the web search tool for the Ollama agent loop, performing web searches via Ollama's hosted search API with authenticated access.
Description
WebSearchTool implements the Tool interface. Execute sends a JSON query to the ollama.com/api/web_search endpoint, requesting up to 5 results. The request is signed using the Ollama Ed25519 key for authentication, with a 15-second timeout. Results are formatted as a numbered list with title, URL, and content snippet (truncated to 300 runes for UTF-8 safety). Returns ErrWebSearchAuthRequired on 401 responses. Currently disabled in DefaultRegistry pending release readiness.
Usage
Registered as the "web_search" tool. Enables the LLM agent to search for current information not in its training data.
Code Reference
Source Location
- Repository: Ollama
- File: x/tools/websearch.go
- Lines: 1-180
Signature
const webSearchAPI = "https://ollama.com/api/web_search"
const webSearchTimeout = 15 * time.Second
var ErrWebSearchAuthRequired = errors.New("web search requires authentication")
type WebSearchTool struct{}
func (w *WebSearchTool) Name() string
func (w *WebSearchTool) Description() string
func (w *WebSearchTool) Schema() api.ToolFunction
func (w *WebSearchTool) Execute(args map[string]any) (string, error)
type webSearchRequest struct {
Query string `json:"query"`
MaxResults int `json:"max_results,omitempty"`
}
type webSearchResult struct {
Title string `json:"title"`
URL string `json:"url"`
Content string `json:"content"`
}
Import
import "github.com/ollama/ollama/x/tools"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| query | string | Yes | The search query to look up on the web |
Outputs
| Name | Type | Description |
|---|---|---|
| output | string | Formatted search results with titles, URLs, and snippets |
| error | error | Auth required, cloud disabled, or HTTP errors |
Usage Examples
tool := &tools.WebSearchTool{}
output, err := tool.Execute(map[string]any{
"query": "latest Go 1.24 release features",
})
// output: "Search results for: latest Go 1.24 release features\n\n1. Go 1.24 Release Notes\n URL: ..."