gemini

package module
v0.0.0-...-790ab80 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 12 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 adapts the Gemini Live API (BidiGenerateContent) to agentkit.LiveLLM: a duplex audio session with native function calling and server-side voice activity detection. Beside it, Transcriber turns a recording into its words with one REST generateContent request (transcribe.go).

It speaks the protocol directly over an injected Transport instead of importing a vendor SDK. Gemini Live is JSON over a WebSocket — small enough that a client is cheaper than a dependency, and keeping this module dependency-free preserves agentkit's ability to move to its own repository. It also makes the whole protocol testable offline against a scripted fake transport.

Audio contract: input is raw little-endian 16-bit PCM, natively 16 kHz (the rate is declared in the MIME type, so the server resamples other rates). Output is ALWAYS 24 kHz — a consumer feeding a fixed-rate sink must resample.

Index

Constants

View Source
const OutputSampleRate = 24000

OutputSampleRate is the rate of every LiveAudio chunk this adapter emits. Fixed by the API, exported because a consumer must resample or configure its sink to match.

Variables

View Source
var DefaultThinkingBudgets = map[agentkit.Effort]int{
	agentkit.EffortLow:    1024,
	agentkit.EffortMedium: 8192,
	agentkit.EffortHigh:   24576,
}

DefaultThinkingBudgets is how many tokens of thinking each reasoning effort buys, on the scale the 2.5 Flash models take (0 to 24576): a quick look, a real think, the ceiling.

Functions

This section is empty.

Types

type Client

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

Client is a LiveLLM backed by the Gemini Live API. It holds no connection: each Open dials its own, so one Client serves many concurrent sessions.

func New

func New(d Dialer, apiKey, model string, opts ...Option) *Client

New builds a Client that dials with d, authenticates with apiKey and runs model (the bare id, e.g. "gemini-live-2.5-flash-preview" — the "models/" prefix is added). The model is required rather than defaulted: live-capable model ids churn, and a stale built-in default would fail at connect time with a confusing error instead of at configuration time.

func (*Client) Open

func (c *Client) Open(ctx context.Context, conv []agentkit.Message, tools []agentkit.ToolSpec) (agentkit.LiveSession, error)

Open dials a session, sends the setup frame, waits for setupComplete, seeds any prior transcript, and starts the reader. The returned session is live until Close or ctx cancellation.

type Dialer

type Dialer func(ctx context.Context, url string) (Transport, error)

Dialer opens a Transport to url. nocturn supplies a coder/websocket-backed one; tests supply a scripted fake.

type Option

type Option func(*Client)

Option configures a Client.

func WithEffort

func WithEffort(e agentkit.Effort) Option

WithEffort sets the default reasoning effort, sent as a thinking budget. An effort carried on the ctx Open is called with (agentkit.EffortFrom) overrides it; none leaves the model's own default.

func WithLogger

func WithLogger(l agentkit.Logger) Option

WithLogger sets the diagnostic logger (default: agentkit.NopLogger()). A nil logger is ignored.

func WithThinkingBudgets

func WithThinkingBudgets(budgets map[agentkit.Effort]int) Option

WithThinkingBudgets replaces DefaultThinkingBudgets, for a model whose scale is another. An effort the map does not name sends no budget.

func WithVoice

func WithVoice(name string) Option

WithVoice selects a prebuilt voice by name (provider-defined; empty = the API default).

type Transcriber

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

Transcriber turns a recording into the words spoken in it with one generateContent request: the audio as inline data, a strict instruction to transcribe verbatim in the language spoken. It holds no connection, so one Transcriber serves concurrent calls.

The key travels in the x-goog-api-key header, never in the URL, so it is not in a proxy's log or an error message.

func NewTranscriber

func NewTranscriber(apiKey, model string, opts ...TranscriberOption) *Transcriber

NewTranscriber builds a Transcriber that authenticates with apiKey and runs model (the bare id, e.g. "gemini-3.5-flash"; a "models/" prefix is accepted).

func (*Transcriber) Transcribe

func (t *Transcriber) Transcribe(ctx context.Context, audio []byte, mime string) (string, error)

Transcribe returns the words spoken in audio, of type mime ("audio/webm", "audio/ogg", …), trimmed; "" when nothing was said. A refusal by the API, a blocked prompt and an answer cut short are errors: a partial transcript would send half of what somebody said as all of it.

type TranscriberOption

type TranscriberOption func(*Transcriber)

TranscriberOption configures a Transcriber.

func WithBaseURL

func WithBaseURL(url string) TranscriberOption

WithBaseURL points the Transcriber at another REST root than Google's, such as a test server or a gateway that speaks the same API.

func WithHTTPClient

func WithHTTPClient(c *http.Client) TranscriberOption

WithHTTPClient sets the client requests are sent with (default http.DefaultClient). A nil client is ignored.

type Transport

type Transport interface {
	// Send writes one complete text frame.
	Send(ctx context.Context, frame []byte) error
	// Recv blocks for the next complete text frame. It returns an error once the peer closes.
	Recv(ctx context.Context) ([]byte, error)
	// Close releases the connection. It must be safe to call more than once.
	Close() error
}

Transport is the WebSocket this adapter speaks over, injected rather than imported. That keeps this module dependency-free (agentkit's rule: the engine and its siblings carry no third-party deps that would follow it into its own repository) and makes the whole protocol testable against a scripted fake — no network, no API key, no flakiness in `go test ./...`.

An implementation must be safe for one concurrent Send and one concurrent Recv, which is what the adapter does: a writer goroutine and a reader goroutine. It need NOT support concurrent Sends.

Jump to

Keyboard shortcuts

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