gemini

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 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

gemini implements an API client for Google's Gemini API, which lists its models, and counts tokens. https://ai.google.dev/api

Index

Constants

View Source
const (
	// Endpoint is the endpoint of the Gemini API, which is the default
	Endpoint = "https://generativelanguage.googleapis.com/v1beta"

	// PingInterval is how long the result of pinging the API is kept, before
	// it is pinged again, so the API isn't pinged too often
	PingInterval = 5 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*client.Client
	// contains filtered or unexported fields
}

Client is a client of the Gemini API, which lists its models, and counts the tokens of text

func New

func New(endPoint, apiKey string, opts ...client.ClientOpt) (*Client, error)

New returns a client of the Gemini API, at the endpoint, which is Endpoint when it's empty, with the API key, which it needs, and which is sent in a header, rather than the URL, so it isn't logged

func (*Client) Embed

func (c *Client) Embed(ctx context.Context, model llm.Model, messages []llm.Message, opts ...llm.Opt) ([]llm.Vector, llm.Usage, error)

Embed returns a vector for each of the messages, which must be for a model of this client which embeds, in the same order, in one request to batchEmbedContents. A message is embedded by its text, and its attachments, such as images, audio, and PDFs, which a multimodal model, such as gemini-embedding-2, embeds, together. The options set what the vectors are for, as a task type, such as retrieval_query, the title of the document the messages are from, which is only for retrieval_document, and the number of dimensions of the vectors. gemini-embedding-2 takes the task type, and the title, in the text, with the prefix Google gives for each, rather than as fields, which it doesn't use, and text with attachments isn't prefixed, as Google advises, and older models take them as fields. The API doesn't say the tokens it used, so the usage is none. The request isn't timed out, but is cancelled with the context.

func (*Client) GetModel

func (c *Client) GetModel(ctx context.Context, name string) (llm.Model, error)

GetModel returns the model with the name, such as gemini-2.5-flash, with or without the API's prefix, models/. Returns ErrNotFound when there's none.

func (*Client) Illustrate

func (c *Client) Illustrate(ctx context.Context, model llm.Model, prompt string, images []llm.Resource, opts ...llm.Opt) ([]llm.Resource, llm.Usage, error)

Illustrate returns the images the model, such as Nano Banana, makes from the prompt, or by changing, or combining, the images, as the prompt says, with generateContent, which is asked for images, with the aspect ratio, and size, the options set, and the tokens used. The images it makes as it thinks, which are drafts, are left out. Returns ErrNotImplemented for a model without generateContent, such as Imagen, which predicts images. Returns an error, with what the model said, when it made no image, such as when it refused. The request isn't timed out, as an image can take a while, but is cancelled with the context.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) ([]llm.Model, error)

ListModels returns the models, a page at a time, until they're all listed

func (*Client) ListVoices added in v1.6.7

func (c *Client) ListVoices(_ context.Context, model llm.Model, opts ...llm.Opt) (*llm.VoiceList, error)

ListVoices returns the voices the model speaks in, which are Gemini's prebuilt voices, which it documents, each with its style. The model detects the language it's to speak. Returns ErrNotImplemented for a model which doesn't speak.

func (*Client) Measure

func (c *Client) Measure(ctx context.Context, model llm.Model, text string) (uint, error)

Measure returns how many tokens the text is, for the model, which must be a model of this client, by counting the tokens of a message from the user with the text, less those of the message apart from its text. Text which was counted before isn't counted again. Empty text is none. When the tokens can't be counted, such as when counting is rate limited, they're an estimate, of a token for every estimateBytes bytes, so what's sent to the model is still fitted to its context, unless the context is done.

func (*Client) Name

func (*Client) Name() string

Name returns the provider name

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks the connectivity of the client, and that the API key is accepted, by listing a model. The API is only pinged once every ping interval: otherwise, the result of the last ping is returned, unless it ended as its caller's context did.

func (*Client) Self

func (c *Client) Self() llm.Provider

Self returns the underlying client implementation

func (*Client) Speak

func (c *Client) Speak(ctx context.Context, model llm.Model, text string, opts ...llm.Opt) (llm.Resource, llm.Usage, error)

Speak returns the text, spoken by the model, such as gemini-3.8-flash-tts, as audio, with generateContent, which is asked for audio, in the voice, such as Kore, and as the instructions say, such as "calmly", and the tokens used. The audio is WAV, unless PCM is asked for, which is audio/L16. A model is told how to speak with the text's style, or, when it doesn't take one, in the text, as directed says. Returns ErrBadParameter when there's no text, or an option isn't supported, such as the speed, which the instructions can say instead, and ErrNotImplemented for a model which doesn't speak.

func (*Client) Transcribe

func (c *Client) Transcribe(ctx context.Context, model llm.Model, audio llm.Resource, stream llm.TranscriberStreamFn, opts ...llm.Opt) (llm.Transcript, llm.Usage, error)

Transcribe returns the speech in the audio as text, with a transcription model, such as gemini-3.5-transcribe, in the language, when it's set, which it's otherwise heard in, with the transcript's segments, from when each word was spoken, and who spoke them, when they're asked for, which are each a sentence, or what was said before a pause, of a speaker. The pieces of the transcript are streamed, as they're transcribed. Audio of more than 14MB is uploaded, with the Files API, as it's read, when its length is known, such as a resource's, so it isn't held in memory, and deleted, once it's transcribed. Returns ErrNotImplemented for a model which doesn't transcribe, and ErrBadParameter for an option which isn't supported.

func (*Client) Translate

func (c *Client) Translate(ctx context.Context, model llm.Model, _ llm.Resource, _ llm.TranscriberStreamFn, _ ...llm.Opt) (llm.Transcript, llm.Usage, error)

Translate isn't supported, as Gemini's transcription models only transcribe

func (*Client) URL

func (c *Client) URL() *url.URL

URL returns the client's normalized endpoint

func (*Client) WithoutSession

func (c *Client) WithoutSession(ctx context.Context, model llm.Model, messages []llm.Message, tools []llm.Tool, stream llm.GeneratorStreamFn, opts ...llm.Opt) (llm.Message, llm.Usage, error)

WithoutSession returns the model's reply to the messages, which must be for a model of this client, and the tokens used, with streamGenerateContent. The model can call the tools, and the reply has its calls, with their thought signatures, which are sent back with them. The reply is always streamed, so a long one doesn't time out, and when the stream function isn't nil, it's called with each piece of its thinking, which is a summary of the model's thoughts, and its text, as they're generated. The request isn't timed out, as a model can take a while to reply, but is cancelled with the context.

type Model

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

Model is one of Gemini's models, satisfying the llm.Model interface

func (*Model) Aliases

func (m *Model) Aliases() []string

Aliases returns the model it's a version of, when it's another, such as gemini-1.5-flash, for gemini-1.5-flash-001

func (*Model) Capabilities

func (m *Model) Capabilities() llm.ModelCap

Capabilities returns what the model can do, of what the API says: whether it generates content, which is replying, or embeds it, by the methods it supports, whether it thinks, and takes a temperature. Models which speak, transcribe, or make images, which the API doesn't say, are known by their names: speech models, such as gemini-2.5-flash-preview-tts, and transcription models, which generate content, don't reply, and image models, such as Nano Banana, or Imagen, make images. Whether a model calls tools, or understands images, isn't known, as the API doesn't say.

func (*Model) Created

func (m *Model) Created() time.Time

Created returns the zero time, as the API doesn't say when a model was created

func (*Model) Description

func (m *Model) Description() string

Description returns the model's name for people, such as Gemini 2.5 Flash, or what it is, when it has none

func (*Model) InputTokenLimit

func (m *Model) InputTokenLimit() *uint

InputTokenLimit returns the model's context window, when it's known

func (*Model) Meta

func (m *Model) Meta() map[string]json.RawMessage

Meta returns what the API says of the model, which isn't elsewhere: its version, what it is, the methods it supports, and its sampling options

func (*Model) Name

func (m *Model) Name() string

Name returns the model's name, such as gemini-2.5-flash, without the API's prefix

func (*Model) OutputTokenLimit

func (m *Model) OutputTokenLimit() *uint

OutputTokenLimit returns the most tokens the model replies with, when it's known

func (*Model) Provider

func (m *Model) Provider() llm.Provider

Provider returns the model's provider

Jump to

Keyboard shortcuts

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