gemini

package
v1.6.0 Latest Latest
Warning

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

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

Documentation

Overview

Package gemini implements models.Provider for the Gemini family, covering both the public Gemini API (API-key auth) and Vertex AI (Application Default Credentials + GCP project).

The two are exposed as distinct provider names ("gemini" and "vertex") so users and automation can pin to a backend explicitly. Both delegate to google.golang.org/adk/model/gemini under the hood.

Index

Constants

View Source
const AuthorGoogleSearch = "gemini/google_search"

AuthorGoogleSearch is the synthetic event Author used for both "model issued a search query" and "model grounded on this web source" projected events. Stable across releases so eventlog consumers can filter on it.

Variables

This section is empty.

Functions

func GroundingProjection added in v1.1.0

func GroundingProjection(inner session.Service) session.Service

GroundingProjection wraps a session.Service so that every event carrying Gemini server-side built-in evidence (today: GoogleSearch grounding) is followed by one or more synthetic events surfacing that evidence under a stable Author namespace:

  • "gemini/google_search" — one event per web search query the model issued, then one per grounded web source (title + URI)

Synthetic events share the parent event's InvocationID and Branch so they participate in eventlog queries (WithAuthor, WithBranchPrefix, WithSessionTree). Their Content.Role is left empty so ADK's content processor skips them when assembling the LLM's conversation history — they're for audit + display, not conversation context.

Wiring:

svc, _ := eventlog.Open(ctx, dialector)
svc.Service = gemini.GroundingProjection(svc.Service) // wrap before WithSessionService
agent.New(model, agent.WithSessionService(svc.Service))

URLContext evidence is not projected today: ADK's gemini wrapper drops URLContextMetadata at conversion (only GroundingMetadata is lifted onto model.LLMResponse). Capturing it would require intercepting raw genai responses below the ADK boundary; deferred until a consumer needs it.

Types

type BuiltinTools

type BuiltinTools struct {
	GoogleSearch  bool // Public web search grounding (default: on)
	URLContext    bool // Fetch + ground on URLs the model decides to visit (default: on)
	CodeExecution bool // Sandboxed Python execution on Google's servers (default: off)
}

BuiltinTools toggles Gemini's server-side built-in tools surfaced by core-agent. Each enabled flag becomes its own *genai.Tool entry injected into the request's Config.Tools alongside any user-defined function declarations.

Defaults:

  • GoogleSearch + URLContext are on (universally useful, no setup)
  • CodeExecution is off (useful but a real action surface — opt in when you've decided sandboxed Python on Google's servers is acceptable for your security and cost posture)

To turn one off:

provider, _ := gemini.NewAPIKey(key, gemini.WithURLContext(false))

To turn CodeExecution on:

provider, _ := gemini.NewAPIKey(key, gemini.WithCodeExecution(true))

To replace the whole set:

provider, _ := gemini.NewAPIKey(key, gemini.WithBuiltinTools(gemini.BuiltinTools{
    GoogleSearch: true, // URL context + CodeExecution off
}))

Other genai built-ins (FileSearch, GoogleMaps, ComputerUse, EnterpriseWebSearch, GoogleSearchRetrieval, Retrieval) aren't surfaced here. They require upstream setup (a corpus, a Maps API key, a hosted environment) or are Vertex-only — flipping them on without configuring the upstream resource yields an API error, not a working tool. Add them when an actual consumer needs them.

func DefaultBuiltinTools

func DefaultBuiltinTools() BuiltinTools

DefaultBuiltinTools returns the on-by-default baseline applied to every Provider unless overridden via WithBuiltinTools or one of the per-tool helpers.

type Option

type Option func(*Provider)

Option configures a Gemini Provider at construction time.

func WithBuiltinTools

func WithBuiltinTools(b BuiltinTools) Option

WithBuiltinTools replaces the Provider's whole BuiltinTools set.

func WithCodeExecution

func WithCodeExecution(on bool) Option

WithCodeExecution toggles the CodeExecution built-in (sandboxed Python on Google's servers). Off by default.

func WithGoogleSearch

func WithGoogleSearch(on bool) Option

WithGoogleSearch toggles the Google Search built-in.

func WithURLContext

func WithURLContext(on bool) Option

WithURLContext toggles the URL Context built-in.

type Provider

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

Provider is the Gemini-family implementation of models.Provider.

func NewAPIKey

func NewAPIKey(key string, opts ...Option) (*Provider, error)

NewAPIKey returns a Provider authenticated against the public Gemini API using key. Empty key is rejected so the failure mode is clear at startup.

Built-in tools (Google Search + URL Context) are enabled by default; pass WithBuiltinTools / WithGoogleSearch / WithURLContext to override.

func NewVertex

func NewVertex(project, location string, opts ...Option) (*Provider, error)

NewVertex returns a Provider authenticated against Vertex AI for the given GCP project and location, using Application Default Credentials.

Built-in tools (Google Search + URL Context) are enabled by default; pass WithBuiltinTools / WithGoogleSearch / WithURLContext to override.

func (*Provider) Model

func (p *Provider) Model(ctx context.Context, modelID string) (adkmodel.LLM, error)

Model constructs a model.LLM for the given model ID. When the Provider has any built-in tools enabled, the returned LLM is wrapped to inject them into Config.Tools on every request.

func (*Provider) Name

func (p *Provider) Name() string

Name reports the provider identity (e.g. "gemini" or "vertex").

Jump to

Keyboard shortcuts

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