ModelBox Open Source

/

AgentKit
Core Concepts/Tools/Built-in Tools/Google Search Tool

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

  1. Google API Key: Create a Google Cloud project and enable the Custom Search API
  2. 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

ParameterTypeRequiredDescription
querystringYesThe search query to perform
num_resultsintNoNumber of results (1-10, default: 10)
startintNoStarting index for pagination (1-91, default: 1)
search_typestringNoLeave empty for web search or "image" for images
languagestringNoLanguage restriction (e.g., "lang_en")
countrystringNoCountry 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.

Edit on GitHub