googleai

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package googleai provides a provider implementation for Google AI's Gemini API.

Google AI uses a different API format than OpenAI, so this package implements custom parsing, enrichment, and extraction logic.

Key differences from OpenAI:

  • Endpoint: /v1beta/models/{model}:generateContent (model in path)
  • Auth: x-goog-api-key header (not Bearer token)
  • Request uses contents array with parts instead of messages
  • System prompt is in systemInstruction field
  • Response uses candidates instead of choices
  • Token fields: promptTokenCount/candidatesTokenCount

Basic usage:

provider, _ := googleai.New("your-api-key")
proxy := llmproxy.NewProxy(provider)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Candidate

type Candidate struct {
	Content       *Content       `json:"content,omitempty"`
	FinishReason  string         `json:"finishReason,omitempty"`
	SafetyRatings []SafetyRating `json:"safetyRatings,omitempty"`
}

Candidate represents a single completion candidate.

type Content

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

Content represents a content block with role and parts.

type Enricher

type Enricher struct {
	// APIKey is the Google AI API key.
	APIKey string
}

Enricher implements llmproxy.RequestEnricher for Google AI's API. It sets the required x-goog-api-key header.

func NewEnricher

func NewEnricher(apiKey string) *Enricher

NewEnricher creates a new Google AI enricher with the given API key.

func (*Enricher) Enrich

func (e *Enricher) Enrich(req *http.Request, meta llmproxy.BodyMetadata, rawBody []byte) error

Enrich adds Google AI-specific headers to the request. Sets:

  • x-goog-api-key: <APIKey>
  • Content-Type: application/json

type Extractor

type Extractor struct{}

Extractor implements llmproxy.ResponseExtractor for Google AI responses.

func NewExtractor

func NewExtractor() *Extractor

NewExtractor creates a new Google AI response extractor.

func (*Extractor) Extract

func (e *Extractor) Extract(resp *http.Response) (llmproxy.ResponseMetadata, []byte, error)

Extract parses a Google AI response and returns unified metadata.

Google AI responses use:

  • candidates array instead of choices
  • usageMetadata with promptTokenCount/candidatesTokenCount

Returns metadata, raw body bytes, and any error.

type GenerationConfig

type GenerationConfig struct {
	Temperature     float64  `json:"temperature,omitempty"`
	TopP            float64  `json:"topP,omitempty"`
	TopK            int      `json:"topK,omitempty"`
	MaxOutputTokens int      `json:"maxOutputTokens,omitempty"`
	StopSequences   []string `json:"stopSequences,omitempty"`
}

GenerationConfig contains generation parameters.

type InlineData

type InlineData struct {
	MimeType string `json:"mimeType"`
	Data     string `json:"data"`
}

InlineData represents binary data in a part.

type Parser

type Parser struct{}

Parser implements llmproxy.BodyParser for Google AI's request format.

func (*Parser) Parse

func (p *Parser) Parse(body io.ReadCloser) (llmproxy.BodyMetadata, []byte, error)

Parse reads a Google AI request body and extracts metadata. It handles Google AI-specific fields like contents with parts.

type Part

type Part struct {
	Text       string                 `json:"text,omitempty"`
	InlineData *InlineData            `json:"inlineData,omitempty"`
	Custom     map[string]interface{} `json:"-"`
}

Part represents a single part of content (text, inline_data, etc.).

func (*Part) UnmarshalJSON

func (p *Part) UnmarshalJSON(data []byte) error

UnmarshalJSON handles flexible part content.

type PromptFeedback

type PromptFeedback struct {
	BlockReason   string         `json:"blockReason,omitempty"`
	SafetyRatings []SafetyRating `json:"safetyRatings,omitempty"`
}

PromptFeedback contains feedback about the prompt.

type Provider

type Provider struct {
	*llmproxy.BaseProvider
}

Provider is a Google AI provider implementation.

func New

func New(apiKey string) (*Provider, error)

New creates a new Google AI provider with the given API key. The provider is configured to use Google AI's API endpoint (https://generativelanguage.googleapis.com).

Example:

provider, _ := googleai.New("your-google-ai-api-key")

type Request

type Request struct {
	Model             string                 `json:"model,omitempty"`
	Contents          []Content              `json:"contents,omitempty"`
	SystemInstruction *Content               `json:"systemInstruction,omitempty"`
	GenerationConfig  GenerationConfig       `json:"generationConfig,omitempty"`
	SafetySettings    []SafetySetting        `json:"safetySettings,omitempty"`
	Custom            map[string]interface{} `json:"-"`
}

Request represents a Google AI generateContent request.

func (*Request) UnmarshalJSON

func (r *Request) UnmarshalJSON(data []byte) error

UnmarshalJSON captures unknown fields into Custom.

type Resolver

type Resolver struct {
	// BaseURL is the Google AI API base URL.
	BaseURL *url.URL
}

Resolver implements llmproxy.URLResolver for Google AI's API. It constructs the generateContent endpoint URL with the model in the path.

func NewResolver

func NewResolver(baseURL string) (*Resolver, error)

NewResolver creates a new resolver with the given base URL.

func (*Resolver) Resolve

func (r *Resolver) Resolve(meta llmproxy.BodyMetadata) (*url.URL, error)

Resolve returns the full URL for the generateContent or streamGenerateContent endpoint. When meta.Stream is true, the URL uses streamGenerateContent with alt=sse for SSE format. Otherwise, the URL uses generateContent.

If meta.Model is empty, defaults to "gemini-pro".

type Response

type Response struct {
	Candidates     []Candidate     `json:"candidates,omitempty"`
	PromptFeedback *PromptFeedback `json:"promptFeedback,omitempty"`
	UsageMetadata  UsageMetadata   `json:"usageMetadata,omitempty"`
	ModelName      string          `json:"model,omitempty"`
}

Response represents a Google AI generateContent response.

type SafetyRating

type SafetyRating struct {
	Category    string `json:"category"`
	Probability string `json:"probability"`
}

SafetyRating represents safety assessment for a candidate.

type SafetySetting

type SafetySetting struct {
	Category  string `json:"category"`
	Threshold string `json:"threshold"`
}

SafetySetting represents a safety configuration.

type StreamingExtractor added in v0.0.6

type StreamingExtractor struct {
	*Extractor
}

func NewStreamingExtractor added in v0.0.6

func NewStreamingExtractor() *StreamingExtractor

func (*StreamingExtractor) ExtractStreamingWithController added in v0.0.6

func (e *StreamingExtractor) ExtractStreamingWithController(resp *http.Response, w http.ResponseWriter, rc *http.ResponseController) (llmproxy.ResponseMetadata, error)

func (*StreamingExtractor) IsStreamingResponse added in v0.0.6

func (e *StreamingExtractor) IsStreamingResponse(resp *http.Response) bool

type UsageMetadata

type UsageMetadata struct {
	PromptTokenCount     int `json:"promptTokenCount"`
	CandidatesTokenCount int `json:"candidatesTokenCount"`
	TotalTokenCount      int `json:"totalTokenCount"`
}

UsageMetadata tracks token usage in a Google AI response.

Jump to

Keyboard shortcuts

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