model

package
v0.0.0-...-c0d9c37 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2025 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Overview

Package model provides multi-provider LLM integration with unified interfaces and automatic model resolution.

The model package implements the types.Model interface for various Large Language Model providers, using google.golang.org/genai as the primary abstraction layer. It provides consistent content format, streaming patterns, and provider-specific conversions while supporting both synchronous and streaming generation.

Supported Providers

The package supports multiple LLM providers:

  • Google Gemini: Direct integration with full streaming and live connection support
  • Anthropic Claude: Support for direct API, Vertex AI, and AWS Bedrock deployments
  • Registry-based extensibility for additional providers

Model Registry

Models are automatically resolved using regex pattern matching:

// Gemini models
gemini-1.5-pro
gemini-2.0-flash-exp
projects/my-project/locations/us-central1/publishers/google/models/gemini-pro

// Claude models
claude-3-5-sonnet-20241022
claude-3-haiku-20240307

Basic Usage

Creating models using the factory pattern:

factory := model.NewDefaultModelFactory("your-api-key")
model, err := factory.CreateModel(ctx, "gemini-1.5-pro")
if err != nil {
	log.Fatal(err)
}
defer model.Close()

Direct model creation:

// Google Gemini
gemini, err := model.NewGemini(ctx, apiKey, "gemini-1.5-pro")
if err != nil {
	log.Fatal(err)
}

// Anthropic Claude
claude, err := model.NewClaude(ctx, "claude-3-5-sonnet-20241022", model.ClaudeModeAnthropic)
if err != nil {
	log.Fatal(err)
}

Content Generation

Synchronous generation:

request := &types.LLMRequest{
	Contents: []*genai.Content{{
		Parts: []genai.Part{genai.Text("What is the capital of France?")},
	}},
	GenerationConfig: &genai.GenerationConfig{
		Temperature: 0.7,
		MaxOutputTokens: 1000,
	},
}

response, err := model.GenerateContent(ctx, request)
if err != nil {
	log.Fatal(err)
}

fmt.Println(response.Candidates[0].Content.Parts[0])

Streaming generation:

for event, err := range model.GenerateContentStream(ctx, request) {
	if err != nil {
		log.Printf("Stream error: %v", err)
		continue
	}

	if event.TextDelta != "" {
		fmt.Print(event.TextDelta)
	}
}

Live Connections

Some providers support stateful live connections for real-time interactions:

if liveModel, ok := model.(types.LiveModel); ok {
	conn, err := liveModel.StartLiveConnection(ctx, config)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	// Real-time bidirectional communication
	for event, err := range conn.ReceiveEvents(ctx) {
		// Handle real-time events
	}
}

Model Configuration

Models support extensive configuration options:

model, err := model.NewGemini(ctx, apiKey, "gemini-1.5-pro",
	model.WithTemperature(0.7),
	model.WithMaxOutputTokens(2048),
	model.WithTopP(0.9),
	model.WithTopK(40),
	model.WithSafetySettings(safetySettings),
	model.WithSystemInstruction("You are a helpful assistant"),
)

Claude Integration

Claude models support multiple deployment modes:

// Direct Anthropic API
claude, err := model.NewClaude(ctx, "claude-3-5-sonnet-20241022", model.ClaudeModeAnthropic)

// Vertex AI deployment
claude, err := model.NewClaude(ctx, "claude-3-5-sonnet@20241022", model.ClaudeModeVertexAI)

// AWS Bedrock deployment
claude, err := model.NewClaude(ctx, "anthropic.claude-3-5-sonnet-20241022-v2:0", model.ClaudeModeBedrock)

Custom Model Registration

Register custom model implementations:

model.RegisterLLMType(
	[]string{`my-custom-model-.*`},
	func(ctx context.Context, apiKey, modelName string) (types.Model, error) {
		return NewCustomModel(ctx, apiKey, modelName)
	},
)

// Now factory can create custom models
customModel, err := factory.CreateModel(ctx, "my-custom-model-v1")

Error Handling

The package provides detailed error information:

response, err := model.GenerateContent(ctx, request)
if err != nil {
	if rateLimitErr, ok := err.(*types.RateLimitError); ok {
		fmt.Printf("Rate limited, retry after: %v\n", rateLimitErr.RetryAfter)
	} else if quotaErr, ok := err.(*types.QuotaExceededError); ok {
		fmt.Printf("Quota exceeded: %s\n", quotaErr.Message)
	} else {
		fmt.Printf("Generation error: %v\n", err)
	}
}

Function Calling

Models support function calling for tool integration:

tools := []*genai.Tool{{
	FunctionDeclarations: []*genai.FunctionDeclaration{{
		Name: "get_weather",
		Description: "Get current weather for a location",
		Parameters: weatherSchema,
	}},
}}

request := &types.LLMRequest{
	Contents: contents,
	Tools: tools,
	ToolConfig: &genai.ToolConfig{
		FunctionCallingConfig: &genai.FunctionCallingConfig{
			Mode: genai.FunctionCallingAuto,
		},
	},
}

Content Caching

Support for content caching to optimize token usage:

request := &types.LLMRequest{
	Contents: contents,
	CachedContent: "projects/my-project/locations/us-central1/cachedContents/my-cache",
}

Thread Safety

All model implementations are safe for concurrent use across multiple goroutines. Each request is handled independently with proper context propagation.

Performance Optimization

The package provides several performance optimizations:

  • Connection pooling for HTTP requests
  • Automatic retry with exponential backoff
  • Request batching where supported
  • Efficient streaming with minimal buffering
  • Content caching for large contexts

Environment Variables

Models can be configured via environment variables:

GOOGLE_API_KEY        - Google AI API key
ANTHROPIC_API_KEY     - Anthropic API key
VERTEX_AI_PROJECT     - Google Cloud project for Vertex AI
VERTEX_AI_LOCATION    - Google Cloud location for Vertex AI

Integration with Agent System

Models integrate seamlessly with the agent framework:

agent := agent.NewLLMAgent(ctx, "assistant",
	agent.WithModel("gemini-1.5-pro"),
	agent.WithInstruction("You are a helpful assistant"),
)

The agent system automatically handles model creation, request formatting, response processing, and error handling.

Index

Constants

View Source
const (
	// GeminiLLMDefaultModel is the default model name for [Gemini].
	GeminiLLMDefaultModel = "gemini-2.5-flash"

	// EnvGoogleAPIKey is the environment variable name that specifies the API key for the Gemini API.
	// If both GOOGLE_API_KEY and GEMINI_API_KEY are set, GOOGLE_API_KEY will be used.
	EnvGoogleAPIKey = "GOOGLE_API_KEY"

	// EnvGeminiAPIKey is the environment variable name that specifies the API key for the Gemini API.
	EnvGeminiAPIKey = "GEMINI_API_KEY"
)
View Source
const (
	// EnvGoogleCloudProject is the environment variable name that specifies the GCP project ID.
	EnvGoogleCloudProject = "GOOGLE_CLOUD_PROJECT"

	// EnvGoogleCloudProject is the environment variable name that specifies the GCP location.
	EnvGoogleCloudLocation = "GOOGLE_CLOUD_LOCATION"

	// EnvGoogleCloudRegion is the environment variable name that specifies the GCP region.
	EnvGoogleCloudRegion = "GOOGLE_CLOUD_REGION"
)

Variables

This section is empty.

Functions

func NewLLM

func NewLLM(ctx context.Context, modelName string) (types.Model, error)

NewLLM is a convenience function to create a new LLM instance.

func RegisterLLM

func RegisterLLM(modelPattern string, creator ModelCreatorFunc)

RegisterLLM is a convenience function to register a model pattern.

func RegisterLLMType

func RegisterLLMType(patterns []string, creator ModelCreatorFunc)

RegisterLLMType registers multiple patterns for a single model creator.

func ToGenAIRole

func ToGenAIRole[T ~string](role T) genai.Role

ToGenAIRole converts a custom role type to a genai.Role type.

Types

type Claude

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

Claude represents an integration with Claude models served from Vertex AI.

func NewClaude

func NewClaude(ctx context.Context, modelName string, mode ClaudeMode, opts ...Option[Claude]) (*Claude, error)

NewClaude creates a new Claude LLM instance.

func (*Claude) Connect

Connect creates a live connection to the Claude LLM.

Connect implements types.Model.

TODO(zchee): implements.

func (*Claude) GenerateContent

func (m *Claude) GenerateContent(ctx context.Context, request *types.LLMRequest) (*types.LLMResponse, error)

GenerateContent generates content from the model.

GenerateContent implements types.Model.

func (*Claude) Name

func (m *Claude) Name() string

Name returns the name of the Claude model.

Name implements types.Model.

func (*Claude) StreamGenerateContent

func (m *Claude) StreamGenerateContent(ctx context.Context, request *types.LLMRequest) iter.Seq2[*types.LLMResponse, error]

StreamGenerateContent streams generated content from the model.

StreamGenerateContent implements types.Model.

func (*Claude) SupportedModels

func (m *Claude) SupportedModels() []string

SupportedModels returns a list of supported models in the Claude.

See https://docs.anthropic.com/en/docs/about-claude/models/all-models.

SupportedModels implements types.Model.

type ClaudeMode

type ClaudeMode int

ClaudeMode represents a mode of the Claude model.

const (
	// ClaudeModeAnthropic is the mode for Anthropic's official models.
	ClaudeModeAnthropic ClaudeMode = iota

	// ClaudeModeVertexAI is the mode for Google Cloud Platform (GCP) Vertex AI models.
	ClaudeModeVertexAI

	// ClaudeModeBedrock is the mode for Amazon Web Services (AWS) Bedrock models.
	ClaudeModeBedrock
)

type DefaultModelFactory

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

DefaultModelFactory is the default implementation of ModelFactory.

func (*DefaultModelFactory) CreateModel

func (f *DefaultModelFactory) CreateModel(ctx context.Context, modelName string) (types.Model, error)

CreateModel creates a model with the specified name.

type Gemini

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

Gemini represents a Google Gemini Large Language Model.

func NewGemini

func NewGemini(ctx context.Context, modelName string, opts ...Option[Gemini]) (*Gemini, error)

NewGemini creates a new Gemini instance.

func (*Gemini) Connect

func (m *Gemini) Connect(ctx context.Context, request *types.LLMRequest) (types.ModelConnection, error)

Connect creates a live connection to the Gemini LLM.

Connect implements types.Model.

func (*Gemini) GenerateContent

func (m *Gemini) GenerateContent(ctx context.Context, request *types.LLMRequest) (*types.LLMResponse, error)

GenerateContent generates content from the model.

GenerateContent implements types.Model.

func (*Gemini) Name

func (m *Gemini) Name() string

Name returns the name of the Gemini model.

Name implements types.Model.

func (*Gemini) StreamGenerateContent

func (m *Gemini) StreamGenerateContent(ctx context.Context, request *types.LLMRequest) iter.Seq2[*types.LLMResponse, error]

StreamGenerateContent streams generated content from the model.

StreamGenerateContent implements types.Model.

func (*Gemini) SupportedModels

func (m *Gemini) SupportedModels() []string

SupportedModels returns a list of supported models in the Gemini.

See https://cloud.google.com/vertex-ai/generative-ai/docs/models and https://ai.google.dev/gemini-api/docs/models.

SupportedModels implements types.Model.

type GeminiConnection

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

GeminiConnection implements types.ModelConnection for Google Gemini models.

func (*GeminiConnection) Close

func (c *GeminiConnection) Close() error

Close terminates the connection to the model.

func (*GeminiConnection) Receive

Receive returns a channel that yields model responses.

func (*GeminiConnection) SendContent

func (c *GeminiConnection) SendContent(ctx context.Context, content *genai.Content) error

SendContent sends a user content to the model.

func (*GeminiConnection) SendHistory

func (c *GeminiConnection) SendHistory(ctx context.Context, history []*genai.Content) error

SendHistory sends the conversation history to the model.

func (*GeminiConnection) SendRealtime

func (c *GeminiConnection) SendRealtime(ctx context.Context, blob []byte, mimeType string) error

SendRealtime sends a chunk of audio or a frame of video to the model in realtime. Note: Gemini API may not directly support realtime streaming for all content types. This method provides a placeholder implementation.

type LLMRegistry

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

LLMRegistry provides a registry for LLM models. It allows registering and resolving model implementations based on regex patterns.

func GetRegistry

func GetRegistry() *LLMRegistry

GetRegistry returns the singleton registry instance.

func NewLLMRegistry

func NewLLMRegistry(cacheSize int) *LLMRegistry

NewLLMRegistry creates a new LLM registry with the specified cache size.

func (*LLMRegistry) NewLLM

func (r *LLMRegistry) NewLLM(ctx context.Context, modelName string) (types.Model, error)

NewLLM creates a new LLM instance for the given model name. It resolves the appropriate model implementation and creates an instance.

func (*LLMRegistry) RegisterLLM

func (r *LLMRegistry) RegisterLLM(modelPattern string, creator ModelCreatorFunc)

RegisterLLM registers a model pattern with a creator function. If the pattern already exists, it will be updated with the new creator.

func (*LLMRegistry) ResolveLLM

func (r *LLMRegistry) ResolveLLM(modelName string) (ModelCreatorFunc, error)

ResolveLLM finds the appropriate model creator for the given model name. Uses regex pattern matching and caching for performance.

type ModelCreatorFunc

type ModelCreatorFunc func(ctx context.Context, apiKey, modelName string) (types.Model, error)

ModelCreatorFunc is a function type that creates a model instance.

type ModelFactory

type ModelFactory interface {
	// CreateModel creates a model with the specified name.
	CreateModel(ctx context.Context, modelName string) (types.Model, error)
}

ModelFactory creates models.

func NewModelFactory

func NewModelFactory(apiKey string) ModelFactory

NewModelFactory creates a new model factory.

type ModelType

type ModelType = string

ModelType represents a type of model.

const (
	// ModelTypeGemini represents Gemini models.
	ModelTypeGemini ModelType = "gemini"

	// ModelTypeClaude represents Claude models.
	ModelTypeClaude ModelType = "claude"

	// ModelTypeClaudeVertex represents Claude Vertex AI models.
	ModelTypeClaudeVertexAI ModelType = "claude-vertex-ai"
)

type Option

type Option[T Gemini | Claude] func(*T)

Option is a function that modifies the Gemini or Claude model.

func WithHTTPClient

func WithHTTPClient[T Gemini | Claude](hc *http.Client) Option[T]

WithHTTPClient sets the *http.Client for the Gemini model.

func WithMaxTokens

func WithMaxTokens(maxToken int64) Option[Claude]

func WithRetry

func WithRetry(retry *backoff.ExponentialBackOff) Option[Gemini]

WithRetry sets the *backoff.ExponentialBackOff for the Gemini model.

type Role

type Role = string

Role represents a role of a participant in a conversation.

const (
	// RoleSystem is the role of the system.
	RoleSystem Role = "system"

	// RoleUser is the role of the user.
	//
	// This value Same as the [genai.RoleUser]
	RoleUser Role = "user"

	// RoleModel is the role of the model.
	//
	// This value Same as the [genai.RoleModel]
	RoleModel Role = "model"

	// RoleAssistant is the role of the assistant.
	//
	// This value Same as the [anthropic.MessageParamRoleAssistant]
	RoleAssistant Role = "assistant"
)

func NewRole

func NewRole[T ~string](role T) Role

NewRole creates a new role from a string type.

Jump to

Keyboard shortcuts

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