gemini

package
v1.832.2 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package gemini is the ONE client for Google's Gemini API, covering exactly the four calls this module makes: countTokens, generateContent, batchEmbedContents and models.list.

It exists instead of google.golang.org/genai because that SDK reaches cloud.google.com/go/auth for credentials, which drags s2a-go, gRPC and protobuf — 100+ packages — into every binary that links it. Gemini is an API-key REST service; none of that machinery buys anything here.

Contract (base = https://generativelanguage.googleapis.com/v1beta, auth = "x-goog-api-key: {apiKey}"):

count    POST {base}/models/{model}:countTokens
         {"contents":[{"role":"user","parts":[{"text":"…"}]}]}
         → {"totalTokens":12}
generate POST {base}/models/{model}:generateContent
         {"contents":[…]}
         → {"candidates":[{"content":{"parts":[{"text":"…"}]},"tokenCount":34}]}
embed    POST {base}/models/{model}:batchEmbedContents
         {"requests":[{"model":"models/…","content":{"parts":[{"text":"…"}]}}]}
         → {"embeddings":[{"values":[0.1,…]}]}
list     GET  {base}/models?pageSize=50&pageToken=…
         → {"models":[{"name":"models/…"}],"nextPageToken":"…"}

Index

Constants

View Source
const RoleUser = "user"

RoleUser authors a caller turn; replies come back with role "model".

Variables

This section is empty.

Functions

This section is empty.

Types

type Candidate

type Candidate struct {
	Content    *Content `json:"content"`
	TokenCount int      `json:"tokenCount"`
}

type Client

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

Client is an API-key Gemini caller. It is stateless beyond the key and the HTTP client, so it is safe to build per request and share across goroutines.

func New

func New(apiKey string, hc *http.Client) *Client

New binds an API key to an HTTP client; hc carries the process proxy configuration (proxy.ProxyHttpClient) and may be nil.

func (*Client) CountTokens

func (c *Client) CountTokens(ctx context.Context, model string, contents []*Content) (int, error)

CountTokens reports the prompt token count the API will bill for contents.

func (*Client) Embed

func (c *Client) Embed(ctx context.Context, model string, contents []*Content) ([][]float32, error)

Embed returns one vector per content, in order. batchEmbedContents is the only embed endpoint that accepts more than one content, so it serves the single-content case too rather than needing a second code path.

func (*Client) GenerateContent

func (c *Client) GenerateContent(ctx context.Context, model string, contents []*Content) (*GenerateResponse, error)

GenerateContent runs one non-streaming completion. A response with no usable candidate — what a safety block returns — is an error, never a nil deref.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context, pageSize int, pageToken string) (*ModelPage, error)

ListModels walks the model catalogue one page at a time; an empty NextPageToken ends the walk.

type Content

type Content struct {
	Role  string  `json:"role,omitempty"`
	Parts []*Part `json:"parts,omitempty"`
}

func Text

func Text(text, role string) *Content

Text builds the single-part content that every call site here needs.

type GenerateResponse

type GenerateResponse struct {
	Candidates []*Candidate `json:"candidates"`
}

type Model

type Model struct {
	Name string `json:"name"`
}

type ModelPage

type ModelPage struct {
	Models        []*Model `json:"models"`
	NextPageToken string   `json:"nextPageToken"`
}

type Part

type Part struct {
	Text string `json:"text,omitempty"`
}

Jump to

Keyboard shortcuts

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