llm

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenLimitExceeded is returned when the input would exceed the model's context limit
	ErrTokenLimitExceeded = errors.New("token limit exceeded for model context window")

	// ErrRateLimitExceeded is returned when the API rate limit would be exceeded
	ErrRateLimitExceeded = errors.New("rate limit exceeded for API requests")

	// ErrInvalidTokenCount is returned when token counting fails
	ErrInvalidTokenCount = errors.New("failed to count tokens in input")
)

Functions

func GetDefaultModel

func GetDefaultModel(provider string) string

GetDefaultModel returns the default model for a provider

func NewProvider

func NewProvider(config core.LLMConfig) (core.LLMProvider, error)

NewProvider creates a new LLM provider based on the configuration

Types

type BaseProvider

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

BaseProvider provides common functionality for LLM providers

func NewBaseProvider

func NewBaseProvider(config *Config) (*BaseProvider, error)

NewBaseProvider creates a new base provider with token management

func (*BaseProvider) CheckTokenLimit

func (p *BaseProvider) CheckTokenLimit(messages []Message) error

CheckTokenLimit checks if the input would exceed the model's context limit

func (*BaseProvider) GetTokenUsage

func (p *BaseProvider) GetTokenUsage() token.UsageMetrics

GetTokenUsage returns the current token usage metrics

func (*BaseProvider) ResetTokenUsage

func (p *BaseProvider) ResetTokenUsage()

ResetTokenUsage resets the token usage metrics

func (*BaseProvider) Stop

func (p *BaseProvider) Stop()

Stop stops the provider's background processes

func (*BaseProvider) WaitForRateLimit

func (p *BaseProvider) WaitForRateLimit()

WaitForRateLimit waits if necessary to respect the rate limit

type Config

type Config struct {
	Model            string
	Temperature      float32
	MaxTokens        int
	TopP             float32
	FrequencyPenalty float32
	PresencePenalty  float32
	Stop             []string
	APIKey           string
	BaseURL          string
	APIVersion       string
	Timeout          float32
	MaxRPM           int  // Maximum requests per minute
	EnableTokenCheck bool // Whether to enable token counting and limits
}

Config holds configuration for an LLM provider

func NewConfig

func NewConfig() *Config

NewConfig creates a new LLM configuration with default values

type Function

type Function struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters"`
}

Function represents a function that can be called by the LLM

type GeminiStudioProvider

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

GeminiStudioProvider implements the Provider interface for Google's Gemini model via AI Studio

func NewGeminiStudioProvider

func NewGeminiStudioProvider(ctx context.Context, config *core.LLMConfig) (*GeminiStudioProvider, error)

NewGeminiStudioProvider creates a new Gemini provider instance using AI Studio

func (*GeminiStudioProvider) Chat

func (p *GeminiStudioProvider) Chat(ctx context.Context, messages []types.Message) (string, error)

Chat generates a response in a chat conversation

func (*GeminiStudioProvider) Complete

func (p *GeminiStudioProvider) Complete(ctx context.Context, prompt string) (string, error)

Complete generates a completion for a prompt

func (*GeminiStudioProvider) CompleteWithFunctions

func (p *GeminiStudioProvider) CompleteWithFunctions(ctx context.Context, prompt string, functions []types.Function) (string, error)

CompleteWithFunctions generates a completion with function calling support

func (*GeminiStudioProvider) GetAPIKey

func (p *GeminiStudioProvider) GetAPIKey() string

GetAPIKey returns the API key

func (*GeminiStudioProvider) GetConfig

func (p *GeminiStudioProvider) GetConfig() *core.LLMConfig

GetConfig returns the current configuration

func (*GeminiStudioProvider) GetContextWindowSize

func (p *GeminiStudioProvider) GetContextWindowSize() int

GetContextWindowSize returns the context window size for the model

func (*GeminiStudioProvider) SupportsFunction

func (p *GeminiStudioProvider) SupportsFunction() bool

SupportsFunction returns whether function calling is supported

type GeminiVertexProvider

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

GeminiVertexProvider implements Provider interface using Google Vertex AI's Gemini model

func NewGeminiVertexProvider

func NewGeminiVertexProvider(ctx context.Context, projectID, location, credentials string, config *types.LLMConfig) (*GeminiVertexProvider, error)

NewGeminiVertexProvider creates a new Vertex AI Gemini provider instance

func (*GeminiVertexProvider) Chat

func (p *GeminiVertexProvider) Chat(ctx context.Context, messages []types.Message) (string, error)

Chat generates a response in a chat conversation

func (*GeminiVertexProvider) Complete

func (p *GeminiVertexProvider) Complete(ctx context.Context, prompt string) (string, error)

Complete generates a completion for the given prompt

func (*GeminiVertexProvider) CompleteWithFunctions

func (p *GeminiVertexProvider) CompleteWithFunctions(ctx context.Context, prompt string, functions []types.Function) (string, error)

CompleteWithFunctions generates a completion with function calling support

func (*GeminiVertexProvider) GetAPIKey

func (p *GeminiVertexProvider) GetAPIKey() string

GetAPIKey returns the API key

func (*GeminiVertexProvider) GetConfig

func (p *GeminiVertexProvider) GetConfig() *types.LLMConfig

GetConfig returns the provider's configuration

func (*GeminiVertexProvider) GetContextWindowSize

func (p *GeminiVertexProvider) GetContextWindowSize() int

GetContextWindowSize returns the context window size for the model

func (*GeminiVertexProvider) SupportsFunction

func (p *GeminiVertexProvider) SupportsFunction() bool

SupportsFunction returns whether the provider supports function calling

type Message

type Message struct {
	Role    Role
	Content string
}

Message represents a message in a chat conversation

type OpenAIProvider

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

OpenAIProvider implements Provider interface using OpenAI's API

func NewOpenAIProvider

func NewOpenAIProvider(apiKey string, model string, verbose bool) *OpenAIProvider

NewOpenAIProvider creates a new OpenAI provider instance

func (*OpenAIProvider) Complete

func (p *OpenAIProvider) Complete(ctx context.Context, messages []types.Message) (string, error)

Complete generates a completion for the given messages

func (*OpenAIProvider) CompleteWithFunctions

func (p *OpenAIProvider) CompleteWithFunctions(ctx context.Context, messages []types.Message, functions []core.FunctionDefinition) (string, error)

CompleteWithFunctions generates a completion with function calling support

func (*OpenAIProvider) GetAPIKey

func (p *OpenAIProvider) GetAPIKey() string

GetAPIKey returns the API key used by the provider

type Provider

type Provider interface {
	// Complete generates a completion for a prompt
	Complete(ctx context.Context, prompt string) (string, error)

	// CompleteWithFunctions generates a completion with function calling support
	CompleteWithFunctions(ctx context.Context, prompt string, functions []Function) (string, error)

	// Chat generates a response in a chat conversation
	Chat(ctx context.Context, messages []Message) (string, error)

	// GetAPIKey returns the API key for the provider
	GetAPIKey() string

	// GetConfig returns the current configuration
	GetConfig() *Config

	// SupportsFunction returns whether the provider supports function calling
	SupportsFunction() bool

	// GetContextWindowSize returns the context window size for the model
	GetContextWindowSize() int

	// GetTokenUsage returns the current token usage metrics
	GetTokenUsage() token.UsageMetrics

	// ResetTokenUsage resets the token usage metrics
	ResetTokenUsage()
}

Provider defines the interface for language model providers

type Role

type Role string

Role represents a message role in a chat conversation

const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleSystem    Role = "system"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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