gemini

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package gemini provides HTTP client functionality for the Gemini API.

Index

Constants

View Source
const (
	// EmbeddingDimensions is the expected embedding vector size.
	EmbeddingDimensions = 768
	// EmbeddingModel is the Gemini embedding model to use.
	EmbeddingModel = "gemini-embedding-2-preview"
	// EmbeddingBatchSize is the maximum chunks per batch request.
	EmbeddingBatchSize = 20
)
View Source
const (
	// LLMModel is the default Gemini LLM model to use.
	// gemini-2.5-flash is the recommended stable model as of March 2026.
	LLMModel = "gemini-2.5-flash"
	// LLMModelFallback is used when the primary model returns 404.
	LLMModelFallback = "gemini-3.0-flash"
)

Variables

This section is empty.

Functions

func BytesToFloat32Slice

func BytesToFloat32Slice(b []byte) []float32

BytesToFloat32Slice converts little-endian bytes back to float32 slice.

func CallLLM

func CallLLM(ctx context.Context, apiKey string, req LLMRequest) (LLMResult, *LLMError)

CallLLM makes a non-streaming generateContent request. Returns LLMResult on success, LLMError on failure — never panics. Falls back to LLMModelFallback if the primary model returns 404.

func EmbedQuery

func EmbedQuery(ctx context.Context, apiKey string, query string) []float32

EmbedQuery embeds a single query string with taskType RETRIEVAL_QUERY. Returns nil on any error — never panics.

func Float32SliceToBytes

func Float32SliceToBytes(v []float32) []byte

Float32SliceToBytes converts a float32 slice to little-endian bytes for sqlite-vec storage.

func ResetBaseURL

func ResetBaseURL()

ResetBaseURL resets the API base URL to the default.

func SetBaseURL

func SetBaseURL(url string)

SetBaseURL sets the API base URL (for testing).

func StreamLLM

func StreamLLM(ctx context.Context, apiKey string, req LLMRequest, onChunk func(string)) (LLMResult, *LLMError)

StreamLLM makes a streaming generateContent request. Calls onChunk for each text chunk as it arrives via SSE. Returns LLMResult with assembled text on completion. Returns LLMError on failure — never panics. Falls back to LLMModelFallback if the primary model returns 404.

Types

type APIKeyValidationResult

type APIKeyValidationResult struct {
	Valid bool
	Error string
}

APIKeyValidationResult contains the result of validating a Gemini API key.

func ValidateAPIKey

func ValidateAPIKey(ctx context.Context, apiKey string) APIKeyValidationResult

ValidateAPIKey makes a lightweight embedContent call to verify the key.

type BatchEmbedResult

type BatchEmbedResult struct {
	Results []EmbedResult
	Errors  []EmbedError
}

BatchEmbedResult contains results from a batch embedding operation.

func EmbedChunks

func EmbedChunks(ctx context.Context, apiKey string, chunks []EmbedRequest) BatchEmbedResult

EmbedChunks embeds up to 20 chunks in a single batchEmbedContents request. Uses taskType RETRIEVAL_DOCUMENT. Returns all chunks as errors if the API call fails entirely — never panics.

type EmbedError

type EmbedError struct {
	ID    int64
	Error string
}

EmbedError represents a failed embedding attempt.

type EmbedRequest

type EmbedRequest struct {
	ID      int64
	Content string
}

EmbedRequest represents a single chunk to embed.

type EmbedResult

type EmbedResult struct {
	ID        int64
	Embedding []float32 // always 768 dimensions
}

EmbedResult represents a successful embedding result.

type LLMError

type LLMError struct {
	Error      string
	DurationMs int64
}

LLMError contains details of a failed LLM call.

type LLMRequest

type LLMRequest struct {
	Prompt       string
	SystemPrompt string
	MaxTokens    int     // default 1024
	Temperature  float32 // default 0.3
}

LLMRequest contains parameters for an LLM call.

type LLMResult

type LLMResult struct {
	Text         string
	InputTokens  int
	OutputTokens int
	DurationMs   int64
}

LLMResult contains the result of a successful LLM call.

Jump to

Keyboard shortcuts

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