gemini

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gemini implements chat.Client for Gemini on top of the official google.golang.org/genai SDK.

Index

Constants

View Source
const (

	// API is the protocol this client speaks, see chat.ModelInfo.API.
	API = "gemini"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIKeyProvider

type APIKeyProvider func(ctx context.Context) (string, error)

APIKeyProvider resolves the API key dynamically, for short-lived tokens.

type Client

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

Client wraps the genai SDK.

func New

func New(cfg Config) (*Client, error)

New creates a Gemini chat.Client. It returns an error when cfg.Model.Name is empty.

func (*Client) DeleteFile

func (c *Client) DeleteFile(ctx context.Context, id string) error

DeleteFile deletes an uploaded file; id is the file URI (FileRef.ID) or the file name (files/...).

func (*Client) Info

func (c *Client) Info() chat.ModelInfo

Info returns the model info.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, req chat.Request) iter.Seq[chat.Event]

Stream sends a streaming request.

func (*Client) UploadFile

func (c *Client) UploadFile(ctx context.Context, name, mimeType string, r io.Reader, ttl time.Duration) (chat.ContentBlock, error)

UploadFile uploads a file to the Files API and returns a media block referring to it, ready to put in a message; FileRef.ID is the file URI. It waits until the file has been processed (video can take a while). The Gemini API deletes files after 48 hours and does not take a ttl, so ttl must be 0. The returned block has no fallback: to keep the session usable after that, set its URL or data too.

type Compat added in v0.3.0

type Compat struct {
	// NoThinkingLevel means only a thinking budget is accepted, not thinkingLevel (Gemini 2.x).
	NoThinkingLevel bool
	// NoLowThinking, NoMediumThinking and NoXHighThinking mean the model rejects that thinking level; a rejected level
	// falls to the next lower one the model accepts, MINIMAL at the bottom. XHIGH is accepted only by Gemini 4 Hydrogen
	// and Neon; Nano Banana 2.1 rejects LOW, and Nano Banana 2 and 2 Lite reject LOW and MEDIUM.
	NoLowThinking    bool
	NoMediumThinking bool
	NoXHighThinking  bool
	// NoMediaInToolResults means media embedded in function results is not accepted (Gemini 2.x);
	// the media is sent as separate parts of the same message instead.
	NoMediaInToolResults bool
}

Compat lists what is missing compared with the full Gemini API, as its current models speak it. The zero value lacks nothing. It is set on an Endpoint for what holds for every model of the service and on a Model for what holds for that model only; a Client lacks whatever either of them lacks.

type Config

type Config struct {
	// Endpoint is the service the client talks to: the zero value is Google's Gemini API, and Vertex AI or a
	// compatible endpoint is described by filling one in.
	Endpoint Endpoint
	// Model is the model the client talks to; its Name must be set. Another model is another Client.
	Model          Model
	APIKey         string
	APIKeyProvider APIKeyProvider
	Timeout        time.Duration
	// MaxRetries is how many times a failed request is retried (408, 429 and 5xx); 0 leaves the SDK's default,
	// which is not to retry.
	MaxRetries int
	Headers    map[string]string
	HTTPClient *http.Client

	// ImageGeneration means the model can generate images (such as the gemini-*-image models):
	// requests ask for both TEXT and IMAGE output, and generated images in history are replayed as is.
	ImageGeneration bool
	// FileScope names the Files API store this key reads, see chat.FileRef.Scope. It defaults to "gemini" for the official
	// Gemini API endpoint and to none on Vertex (which has no Files API; use gs:// URLs) or a custom BaseURL.
	FileScope string
	// HTTPHooks are debugging hooks at the raw HTTP layer.
	HTTPHooks chat.HTTPHooks
}

Config configures the Gemini Client.

type Endpoint added in v0.3.0

type Endpoint struct {
	// Provider names the vendor in messages, usage and errors; "gemini" by default.
	Provider string
	// BaseURL is the address of the API; empty for Google's.
	BaseURL string
	// Vertex means the API key belongs to Vertex AI (express mode) and requests go to the Vertex endpoint;
	// by default the Gemini API (generativelanguage.googleapis.com) is used.
	// Full Vertex access based on project, region and credentials is not provided yet.
	Vertex bool
	// Compat says where every model of the endpoint differs from the full protocol; see Compat.
	Compat Compat
}

Endpoint is a service that speaks the Gemini API: Google's Gemini API itself (the zero value), Vertex AI or a compatible one. It says where the service is and how to reach it; the model served from it is described by Model.

type Model added in v0.3.0

type Model struct {
	// Name is the model name sent to the API.
	Name string
	// MaxInputTokens is the context limit of the model; 0 declares none, and the runner then never compacts history.
	MaxInputTokens int
	// MaxOutputTokens is the output limit of the model; 0 declares none. It is only reported by Info: a request
	// that sets no MaxTokens sends no limit.
	MaxOutputTokens int
	// Compat says where this model differs from the full protocol; see Compat.
	Compat Compat
}

Model is the model a Client talks to. Beyond the zero value only Name must be set: a model is taken to support the whole protocol, and one that does not says what it lacks in Compat.

Jump to

Keyboard shortcuts

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