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
- type Candidate
- type Client
- func (c *Client) CountTokens(ctx context.Context, model string, contents []*Content) (int, error)
- func (c *Client) Embed(ctx context.Context, model string, contents []*Content) ([][]float32, error)
- func (c *Client) GenerateContent(ctx context.Context, model string, contents []*Content) (*GenerateResponse, error)
- func (c *Client) ListModels(ctx context.Context, pageSize int, pageToken string) (*ModelPage, error)
- type Content
- type GenerateResponse
- type Model
- type ModelPage
- type Part
Constants ¶
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 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 ¶
New binds an API key to an HTTP client; hc carries the process proxy configuration (proxy.ProxyHttpClient) and may be nil.
func (*Client) CountTokens ¶
CountTokens reports the prompt token count the API will bill for contents.
func (*Client) Embed ¶
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.
type GenerateResponse ¶
type GenerateResponse struct {
Candidates []*Candidate `json:"candidates"`
}