Documentation
¶
Overview ¶
Package gemini implements ai.Provider for Google's Gemini models over raw HTTP, either through the Gemini API in Google AI Studio (api_key, https://generativelanguage.googleapis.com/v1beta) or through Vertex AI (project_id + location with Application Default Credentials; location "global" uses https://aiplatform.googleapis.com). Vertex AI express mode (platform "vertex" with an api_key and no project) is supported as well.
Chat uses generateContent / streamGenerateContent?alt=sse with systemInstruction, inline image data and generationConfig (maxOutputTokens, temperature, responseMimeType "application/json" for JSON mode). Embeddings use batchEmbedContents (AI Studio) or the :predict endpoint of Vertex text-embedding models. Images use Imagen's :predict endpoint for "imagen-*" models and generateContent with responseModalities ["IMAGE"] for Gemini image models.
Index ¶
- type Options
- type Provider
- func (p *Provider) Chat(ctx context.Context, req ai.ChatRequest) (*ai.ChatResponse, error)
- func (p *Provider) ChatStream(ctx context.Context, req ai.ChatRequest, onDelta func(string) error) (*ai.ChatResponse, error)
- func (p *Provider) Embed(ctx context.Context, req ai.EmbedRequest) (*ai.EmbedResponse, error)
- func (p *Provider) Image(ctx context.Context, req ai.ImageRequest) (*ai.ImageResponse, error)
- func (p *Provider) Name() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// HTTPClient performs upstream requests (default: httpx.DefaultClient()).
HTTPClient *http.Client
// Logger receives debug logs (default: slog.Default()).
Logger *slog.Logger
// TokenSource overrides Application Default Credentials for Vertex AI.
TokenSource oauth2.TokenSource
// FetchImage downloads URL images (Gemini needs inline bytes). Default:
// an SSRF-guarded fetcher.
FetchImage imagefetch.Func
}
Options are optional dependencies.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is an ai.Provider for Gemini. It is safe for concurrent use.
func New ¶
New creates a Gemini provider. Vertex AI is used when project_id is set (or platform is "vertex"); otherwise AI Studio with api_key (falling back to the GEMINI_API_KEY / GOOGLE_API_KEY environment variables).
func (*Provider) Chat ¶
func (p *Provider) Chat(ctx context.Context, req ai.ChatRequest) (*ai.ChatResponse, error)
Chat implements ai.Provider.
func (*Provider) ChatStream ¶
func (p *Provider) ChatStream(ctx context.Context, req ai.ChatRequest, onDelta func(string) error) (*ai.ChatResponse, error)
ChatStream implements ai.Provider via streamGenerateContent?alt=sse.
func (*Provider) Embed ¶
func (p *Provider) Embed(ctx context.Context, req ai.EmbedRequest) (*ai.EmbedResponse, error)
Embed implements ai.Provider.
func (*Provider) Image ¶
func (p *Provider) Image(ctx context.Context, req ai.ImageRequest) (*ai.ImageResponse, error)
Image implements ai.Provider. "imagen-*" models use :predict; other models are treated as Gemini image models (generateContent with responseModalities IMAGE).