gemini

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultModel is the Gemini model used when no WithModel option is given.
	// gemini-3.5-flash is chosen so the default ThinkingLevelLow configuration
	// is accepted; Gemini 2.x callers should pass WithModel + WithThinkingBudget.
	DefaultModel          = "gemini-3.5-flash"
	DefaultEmbeddingModel = "text-embedding-004"
)

Variables

This section is empty.

Functions

func NewHistory

func NewHistory(contents []*genai.Content) (*gollem.History, error)

NewHistory creates gollem.History from Gemini contents

func ToContents

func ToContents(h *gollem.History) ([]*genai.Content, error)

ToContents converts gollem.History to Gemini contents

Types

type Client

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

Client is a client for the Gemini API. It provides methods to interact with Google's Gemini models.

func New

func New(ctx context.Context, projectID, location string, options ...Option) (*Client, error)

New creates a new client for the Gemini API. It requires a project ID and location, and can be configured with additional options.

The default thinking configuration is ThinkingLevelLow, which works with Gemini 3.x models. Callers using Gemini 2.x models that do not support thinking_level should override this via WithThinkingBudget.

func (*Client) GenerateEmbedding

func (c *Client) GenerateEmbedding(ctx context.Context, dimension int, input []string) ([][]float64, error)

GenerateEmbedding generates embeddings for the given input texts.

func (*Client) NewSession

func (c *Client) NewSession(ctx context.Context, options ...gollem.SessionOption) (gollem.Session, error)

NewSession creates a new session for the Gemini API. It converts the provided tools to Gemini's tool format and initializes a new chat session.

type Option

type Option func(*Client)

Option is a configuration option for the Gemini client.

func WithContentType

func WithContentType(contentType gollem.ContentType) Option

WithContentType sets the content type for text generation. This determines the format of the generated content.

func WithEmbeddingModel

func WithEmbeddingModel(model string) Option

WithEmbeddingModel sets the model to use for embeddings. Default: "text-embedding-004"

func WithGoogleCloudOptions

func WithGoogleCloudOptions(opts ...option.ClientOption) Option

WithGoogleCloudOptions sets additional Google Cloud options. These can include authentication credentials, endpoint overrides, etc.

func WithMaxTokens

func WithMaxTokens(maxTokens int32) Option

WithMaxTokens sets the maximum number of tokens to generate.

func WithModel

func WithModel(model string) Option

WithModel sets the model to use for text generation. Default: "gemini-3.5-flash"

func WithStopSequences

func WithStopSequences(stopSequences []string) Option

WithStopSequences sets the stop sequences for text generation.

func WithSystemPrompt

func WithSystemPrompt(prompt string) Option

WithSystemPrompt sets the system prompt to use for chat completions.

func WithTemperature

func WithTemperature(temp float32) Option

WithTemperature sets the temperature parameter for text generation. Controls randomness in output generation. Range: 0.0 to 2.0 Default: 1.0

func WithThinkingBudget

func WithThinkingBudget(budget int32) Option

WithThinkingBudget sets the thinking budget for text generation. A value of -1 enables automatic thinking budget allocation.

Gemini 3.x deprecates thinking_budget in favor of thinking_level; prefer WithThinkingLevel for those models. The two options are mutually exclusive at the API layer, so calling this clears any thinking level previously set.

func WithThinkingLevel

func WithThinkingLevel(level genai.ThinkingLevel) Option

WithThinkingLevel sets the thinking level for text generation. Introduced in Gemini 3.x as the replacement for WithThinkingBudget.

Valid values: genai.ThinkingLevelMinimal, ThinkingLevelLow, ThinkingLevelMedium, ThinkingLevelHigh.

Vertex AI rejects requests that carry both thinking_budget and thinking_level (HTTP 400), so calling this clears any thinking budget previously set, including the zero-value default established by gemini.New.

func WithTopK

func WithTopK(topK float32) Option

WithTopK sets the top_k parameter for text generation. Controls diversity via top-k sampling. Range: 1 to 40

func WithTopP

func WithTopP(topP float32) Option

WithTopP sets the top_p parameter for text generation. Controls diversity via nucleus sampling. Range: 0.0 to 1.0 Default: 1.0

type Session

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

Session is a session for the Gemini chat. It maintains the conversation state and handles message generation.

func (*Session) AppendHistory

func (s *Session) AppendHistory(h *gollem.History) error

func (*Session) CountToken

func (s *Session) CountToken(ctx context.Context, input ...gollem.Input) (int, error)

CountToken calculates the total number of tokens for the given inputs, including system prompt, history messages, and new inputs. This is useful for estimating API costs and checking token limits before making actual API calls.

func (*Session) Generate

func (s *Session) Generate(ctx context.Context, input []gollem.Input, opts ...gollem.GenerateOption) (*gollem.Response, error)

Generate generates content based on the input with optional per-call overrides.

func (*Session) GenerateContent deprecated

func (s *Session) GenerateContent(ctx context.Context, input ...gollem.Input) (*gollem.Response, error)

Deprecated: GenerateContent is deprecated. Use Generate instead.

func (*Session) GenerateStream deprecated

func (s *Session) GenerateStream(ctx context.Context, input ...gollem.Input) (<-chan *gollem.Response, error)

Deprecated: GenerateStream is deprecated. Use Stream instead.

func (*Session) History

func (s *Session) History() (*gollem.History, error)

func (*Session) Stream

func (s *Session) Stream(ctx context.Context, input []gollem.Input, opts ...gollem.GenerateOption) (<-chan *gollem.Response, error)

Stream generates content based on the input and returns a stream of responses with optional per-call overrides.

type StreamResponse

type StreamResponse struct {
	Resp *genai.GenerateContentResponse
	Err  error
}

StreamResponse wraps the response and error from streaming

Jump to

Keyboard shortcuts

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