websearch

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProviderDuckDuckGo = "duckduckgo"
	ProviderGoogle     = "google"
	ProviderBing       = "bing"
	ProviderSerpAPI    = "serpapi"
)

Supported provider names for the Provider config field.

Variables

This section is empty.

Functions

func NewBingTool

func NewBingTool(apiKey string) tool.CallableTool

NewBingTool creates a new Bing Web Search tool wrapped in the trpc-agent tool schema. The returned tool has the name "bing_search" and can be tested or composed independently.

func NewDDGTool

func NewDDGTool(opts ...DDGOption) tool.CallableTool

NewDDGTool creates a new DuckDuckGo HTML search tool that scrapes real web search results from html.duckduckgo.com. This replaces the Instant Answer API tool which only worked for factual/encyclopedic queries. Without this tool, the agent has no free, key-less web search fallback and would fail on any query that isn't a well-known entity or definition.

func NewSerpAPINewsTool added in v0.1.7

func NewSerpAPINewsTool(cfg SerpAPIConfig, opts ...SerpAPIOption) tool.CallableTool

NewSerpAPINewsTool creates a SerpAPI Google News search tool. Uses engine=google_news to search for current news articles. The returned results include title, source, date, link, and snippet from Google News. This is useful when the agent needs recent news or current events rather than general web pages.

func NewSerpAPIScholarTool added in v0.1.7

func NewSerpAPIScholarTool(cfg SerpAPIConfig, opts ...SerpAPIOption) tool.CallableTool

NewSerpAPIScholarTool creates a SerpAPI Google Scholar search tool. Uses engine=google_scholar to search for academic papers and publications. The returned results include title, authors, publication info, link, and snippet. This is useful when the agent needs academic or scientific information such as research papers, citations, or peer-reviewed content.

func NewSerpAPITool added in v0.1.7

func NewSerpAPITool(cfg SerpAPIConfig, opts ...SerpAPIOption) tool.CallableTool

NewSerpAPITool creates a new SerpAPI search tool for Google web search. This tool calls the SerpAPI Google Search endpoint and returns structured organic results including title, link, and snippet. Without this tool, users would need a Google Custom Search Engine (CSE) ID and API key, which are harder to set up than SerpAPI's single API key.

func NewTool

func NewTool(cfg Config, secretProvider security.SecretProvider, opts ...DDGOption) tool.CallableTool

NewTool creates a new web search tool configured to use a single provider. When Provider is empty it defaults to DuckDuckGo which requires no API keys. If the selected provider is missing credentials, it logs a warning and falls back to DuckDuckGo. When provider is Google and secretProvider is non-nil, OAuth is used for search when UseGoogleOAuth is true or GoogleAPIKey is empty (same token as Calendar/Drive/Gmail).

Optional DDGOptions can be provided to configure the fallback DuckDuckGo tool (e.g. for testing).

func NewWikipediaTool

func NewWikipediaTool(opts ...WikipediaOption) tool.CallableTool

Types

type BingSearchRequest

type BingSearchRequest struct {
	Query string `json:"query" jsonschema:"description=The search query to execute"`
}

BingSearchRequest is the input schema for the standalone Bing search tool.

type Config

type Config struct {
	Provider       string        `yaml:"provider,omitempty" toml:"provider,omitempty"`
	GoogleAPIKey   string        `yaml:"google_api_key,omitempty" toml:"google_api_key,omitempty"`
	GoogleCX       string        `yaml:"google_cx,omitempty" toml:"google_cx,omitempty"`
	UseGoogleOAuth bool          `yaml:"use_google_oauth,omitempty" toml:"use_google_oauth,omitempty"` // use shared Google OAuth token for search when SecretProvider is set
	BingAPIKey     string        `yaml:"bing_api_key,omitempty" toml:"bing_api_key,omitempty"`
	SerpAPI        SerpAPIConfig `yaml:"serpapi,omitempty" toml:"serpapi,omitempty"`
}

Config holds configuration for the web search tool. Provider selects the search backend: "google", "bing", "serpapi", or "duckduckgo" (default). Only one provider is active at a time. When Provider is "google", either set GoogleAPIKey+GoogleCX (API key auth; 100 free queries/day, then billing) or set UseGoogleOAuth true and GoogleCX (uses shared Google sign-in token; requires scope https://www.googleapis.com/auth/cse per Custom Search JSON API). Re-run the Google OAuth browser flow if the token was created before the cse scope was added. When Provider is "serpapi", set serpapi_api_key. SerpAPI uses a single API key for all Google engines (Search, News, Scholar). See https://serpapi.com for details.

type DDGOption

type DDGOption func(*ddgHTMLTool)

DDGOption is a functional option for configuring the DuckDuckGo HTML tool.

func WithDDGEndpoint

func WithDDGEndpoint(endpoint string) DDGOption

WithDDGEndpoint overrides the default DuckDuckGo HTML endpoint. Primarily used for testing with httptest.Server.

func WithDDGHTTPClient

func WithDDGHTTPClient(c *http.Client) DDGOption

WithDDGHTTPClient sets a custom HTTP client for the DuckDuckGo tool. Useful for testing with httptest.Server or for custom transport configuration. The provided *http.Client is wrapped in a retryablehttp.Client with DDG-specific retry settings.

type DDGSearchRequest

type DDGSearchRequest struct {
	Query string `json:"query" jsonschema:"description=The search query to execute"`
}

DDGSearchRequest is the input schema for the DuckDuckGo HTML search tool.

type ProviderOption

type ProviderOption func(*ToolProvider)

ProviderOption configures the websearch ToolProvider (e.g. WithSecretProvider).

func WithDDGOptions

func WithDDGOptions(ddgOpts ...DDGOption) ProviderOption

WithDDGOptions passes options to the fallback DuckDuckGo tool (e.g. for testing).

func WithSecretProvider

func WithSecretProvider(sp security.SecretProvider) ProviderOption

WithSecretProvider sets the SecretProvider so Google search can use the shared Google OAuth token (same sign-in as Calendar, Drive, Gmail) when UseGoogleOAuth is true or google_api_key is not set.

type SearchRequest

type SearchRequest struct {
	Query string `json:"query" jsonschema:"description=The search query to execute"`
}

SearchRequest is the input for the web_search tool.

type SerpAPIConfig added in v0.1.7

type SerpAPIConfig struct {
	APIKey   string `yaml:"serpapi_api_key,omitempty" toml:"serpapi_api_key,omitempty"`
	Location string `yaml:"location,omitempty" toml:"location,omitempty"` // e.g. "Austin, Texas, United States"
	GL       string `yaml:"gl,omitempty" toml:"gl,omitempty"`             // country code, e.g. "us"
	HL       string `yaml:"hl,omitempty" toml:"hl,omitempty"`             // language code, e.g. "en"
}

SerpAPIConfig holds configuration for the SerpAPI provider. SerpAPI is a paid search API that provides structured JSON results from Google Search, Google News, Google Scholar, and many other engines. See https://serpapi.com/search-api for the full API reference.

type SerpAPIOption added in v0.1.7

type SerpAPIOption func(*serpAPITool)

SerpAPIOption is a functional option for configuring SerpAPI tools.

func WithSerpAPIEndpoint added in v0.1.7

func WithSerpAPIEndpoint(endpoint string) SerpAPIOption

WithSerpAPIEndpoint overrides the default SerpAPI endpoint. Primarily used for testing with httptest.Server.

type SerpAPISearchRequest added in v0.1.7

type SerpAPISearchRequest struct {
	Query string `json:"query" jsonschema:"description=The search query to execute"`
}

SerpAPISearchRequest is the input schema for SerpAPI search tools.

type ToolProvider

type ToolProvider struct {
	// contains filtered or unexported fields
}

ToolProvider wraps websearch configuration and satisfies the tools.ToolProviders interface so it can be passed directly to tools.NewRegistry. Without this, websearch tools would need to be constructed inline inside the registry.

func NewToolProvider

func NewToolProvider(cfg Config, opts ...ProviderOption) *ToolProvider

NewToolProvider creates a ToolProvider from the given websearch config. Pass WithSecretProvider(sp) to enable Google search via the shared OAuth token.

func (*ToolProvider) GetTools

func (p *ToolProvider) GetTools() []tool.Tool

GetTools returns the websearch tool configured from the provider's config, as well as the separate wikipedia_search tool. When SerpAPI is the selected provider, the google_news_search and google_scholar_search tools are also included for specialised search workflows.

type WikipediaOption

type WikipediaOption func(*wikipediaTool)

func WithWikipediaEndpoint

func WithWikipediaEndpoint(endpoint string) WikipediaOption

func WithWikipediaHTTPClient

func WithWikipediaHTTPClient(c *http.Client) WikipediaOption

type WikipediaSearchRequest

type WikipediaSearchRequest struct {
	Query string `json:"query" jsonschema:"description=The search query to execute"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL