Documentation
¶
Overview ¶
Package gemini adapts Google's Gemini generateContent API to agent.Model. It translates the provider-neutral []agent.Message/Part model to Gemini "contents" (text, inlineData/fileData images, functionCall, functionResponse), declares tools as functionDeclarations, opens the SSE stream (streamGenerateContent?alt=sse), and normalizes Gemini's response chunks into agent's Event taxonomy. Zero external deps: net/http + stdlib, exactly like the anthropic and openai adapters (the repo enforces a lean core via architecture_test.go, so google.golang.org/genai is deliberately not imported).
Gemini sends COMPLETE functionCall objects (name + args object), not the streamed argument fragments Anthropic/OpenAI send. So one functionCall part becomes a single ToolCallDelta carrying the whole args object as ArgsFragment; the agent core's finalize() json.Valid gate still holds because a complete args object is valid JSON.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is a Google Gemini generateContent API adapter implementing agent.Model.
func New ¶
New constructs a Gemini model adapter. apiKey is your Google AI Studio API key. For Vertex AI or a proxy, override the host with WithBaseURL (the request path /v1beta/models/{model}:streamGenerateContent is appended to it).
func (*Model) Describe ¶ added in v0.8.0
Describe reports the adapter's identity: provider "gemini", the configured model ID, and support for a JSON-schema response format.
func (Model) Format ¶ added in v0.7.0
Format writes String for every verb, so no fmt verb (%v, %+v, %#v, %s, %x, ...) prints the key.
func (*Model) RequiresToolsForRequired ¶ added in v0.10.0
RequiresToolsForRequired implements agent.ToolRules: Gemini refuses tool choice "required" with no tools declared.
func (Model) String ¶ added in v0.7.0
String describes the adapter by its model id. It never includes the API key (or the base URL, which can carry credentials of its own), so a config that holds a *Model can be logged.
func (*Model) ToolNameRule ¶ added in v0.10.0
ToolNameRule implements agent.ToolRules: the tool names Gemini accepts, `^[a-zA-Z_][a-zA-Z0-9_.:-]{0,63}$`, so agent.New refuses any other name when the agent is built.
type Option ¶
type Option func(*Model)
Option configures a Model in New.
func WithBaseURL ¶
WithBaseURL sets the API host, for a proxy or a compatible endpoint; /v1beta/models/{model}:streamGenerateContent is appended to it. The default is "https://generativelanguage.googleapis.com".
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client requests are sent with. The default is http.DefaultClient.
func WithMaxResponseBytes ¶ added in v0.8.0
WithMaxResponseBytes caps how many bytes of one streamed reply the adapter reads: a reply that runs longer fails with agent.ErrResponseTooLarge, which middleware.Retryable does not retry. The default is provider.DefaultMaxResponseBytes (32 MiB); n <= 0 keeps it. Raise it for replies that legitimately run longer, such as large inline images.
func WithMaxTokens ¶
WithMaxTokens sets the default limit on tokens in a reply; a request's Sampling.MaxTokens overrides it. The default is 0, which leaves the limit to the provider.
func WithModel ¶
WithModel sets the model ID sent with every request. The default is "gemini-2.0-flash".
func WithToolResultCodec ¶ added in v0.2.0
func WithToolResultCodec(c provider.ToolResultCodec) Option
WithToolResultCodec encodes tool results sent to the model with c instead of raw JSON (for example GCF, to cut tokens on structured output). The journal keeps the JSON form; only what the model reads changes. Default is JSON. Gemini normally passes a JSON tool result through as a structured response; with a codec set, the encoded string is sent as {"result": <encoded>}.