Google Search Tool
Web search functionality using Google Custom Search JSON API
|
The Google Search tool provides web search functionality using the Google Custom Search JSON API.
Installation
import "github.com/model-box/agent-kit/tool/google_search"
Setup
Requirements
- Google API Key: Create a Google Cloud project and enable the Custom Search API
- Custom Search Engine ID: Create a custom search engine at Google CSE
Environment Variables
export GOOGLE_API_KEY="your-google-api-key"
export GOOGLE_SEARCH_ENGINE_ID="your-custom-search-engine-id"
Usage
package main
import (
"context"
"os"
"github.com/model-box/agent-kit/agent"
"github.com/model-box/agent-kit/model"
"github.com/model-box/agent-kit/session"
"github.com/model-box/agent-kit/tool/google_search"
)
func main() {
// Create Google Search tools
googleTools := google_search.NewGoogleSearchTools()
// Create model
model := model.Model("gpt-4o").
SetAPIKey(os.Getenv("OPENAI_API_KEY"))
// Create agent with Google Search tool
agent := agent.New().
SetModel(model).
SetSystemPrompt("You are a helpful assistant that can search the web.").
AddTool(googleTools.Search())
// Create session and run
session := session.New(agent)
ctx := context.Background()
response, err := session.Run(ctx, []agent.ChatMessage{
agent.NewUserMessage("Search for the latest Go programming tutorials"),
}, nil)
if err != nil {
panic(err)
}
println(response.GetLastMessage().GetContent())
}
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
query | string | Yes | The search query to perform |
num_results | int | No | Number of results (1-10, default: 10) |
start | int | No | Starting index for pagination (1-91, default: 1) |
search_type | string | No | Leave empty for web search or "image" for images |
language | string | No | Language restriction (e.g., "lang_en") |
country | string | No | Country restriction (e.g., "countryUS") |
API Limits
- Free tier: 100 search queries per day
- Paid tier: 10,000 queries per day
Error Handling
The tool handles common errors such as invalid API keys, missing search engine IDs, and API rate limits.