llm

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

README

LLM Package

Go Tests Go Reference

A unified Go library for integrating with multiple LLM providers through a single, consistent interface.

Overview

This package offers a provider-agnostic interface for interacting with LLM services:

  • OpenAI — GPT-4.1, GPT-5.x, O4 models
  • Google Gemini — Gemini 2.5 / 3.x models via the Gemini API
  • Google Vertex AI — Gemini models on Google Cloud
  • Anthropic — Claude Sonnet 4, Opus 4.x, Haiku 4.5
  • OpenRouter — Access 50+ models from OpenAI, Anthropic, Google, Mistral, Qwen, xAI, DeepSeek, and more through a single API
  • Custom — Any OpenAI-compatible endpoint
  • Mock — For testing without API calls

Installation

go get github.com/dracory/llm

Requires Go 1.25 or later.

Key Features

  • Provider Agnostic: Use any supported LLM provider through a consistent interface
  • Extensible: Add new providers by implementing LlmInterface or use RegisterCustomProvider
  • Multiple Output Formats: Text, JSON, XML, YAML, and image generation (PNG/JPEG)
  • Embedding Support: Generate text embeddings across providers
  • Configurable: Fine-tune model parameters like temperature, token limits, and provider-specific options
  • Structured Logging: Optional slog.Logger support for production observability
  • Thread-Safe: Concurrent provider registration is protected by sync.RWMutex

Quick Start

package main

import (
    "fmt"
    "os"

    "github.com/dracory/llm"
)

func main() {
    // Create a text model
    engine, err := llm.TextModel(llm.ProviderOpenAI, llm.LlmOptions{
        ApiKey: os.Getenv("OPENAI_API_KEY"),
        Model:  "gpt-4.1-nano",
    })
    if err != nil {
        panic(err)
    }

    response, err := engine.GenerateText(
        "You are a helpful assistant.",
        "What is a contract?",
    )
    if err != nil {
        panic(err)
    }

    fmt.Println(response)
}

Usage Examples

Text Generation
engine, err := llm.TextModel(llm.ProviderOpenAI, llm.LlmOptions{
    ApiKey: os.Getenv("OPENAI_API_KEY"),
    Model:  "gpt-4.1-nano",
})

response, err := engine.GenerateText(
    "You are a helpful assistant.",
    "Explain quantum computing in simple terms.",
)
JSON Generation
engine, err := llm.JSONModel(llm.ProviderGemini, llm.LlmOptions{
    ApiKey:      os.Getenv("GEMINI_API_KEY"),
    Model:       "gemini-2.5-flash",
    Temperature: llm.PtrFloat64(0.3),
})

jsonResponse, err := engine.GenerateJSON(
    "You are a data extraction assistant.",
    "Extract the name, age, and city from: John is 30 years old and lives in NYC.",
)
Image Generation
engine, err := llm.ImageModel(llm.ProviderOpenRouter, llm.LlmOptions{
    ApiKey: os.Getenv("OPENROUTER_API_KEY"),
    Model:  llm.OPENROUTER_MODEL_GPT_5_IMAGE,
})

imageBytes, err := engine.GenerateImage("A sunset over a mountain lake")
Embedding Generation
engine, err := llm.TextModel(llm.ProviderOpenRouter, llm.LlmOptions{
    ApiKey: os.Getenv("OPENROUTER_API_KEY"),
    Model:  llm.OPENROUTER_MODEL_TEXT_EMBEDDING_3_SMALL,
})

embeddings, err := engine.GenerateEmbedding("The quick brown fox")
Using OpenRouter with Pre-defined Model Constants
engine, err := llm.TextModel(llm.ProviderOpenRouter, llm.LlmOptions{
    ApiKey: os.Getenv("OPENROUTER_API_KEY"),
    Model:  llm.OPENROUTER_MODEL_CLAUDE_SONNET_4_5,
})

response, err := engine.GenerateText(
    "You are a code reviewer.",
    "Review this function for bugs: ...",
)
Vertex AI with Credentials
engine, err := llm.TextModel(llm.ProviderVertex, llm.LlmOptions{
    ProjectID:   "my-gcp-project",
    Region:      "europe-west1",
    Model:       "gemini-2.5-flash",
    MaxTokens:   8192,
    Temperature: llm.PtrFloat64(0.7),
    ProviderOptions: map[string]any{
        "credentials_json": os.Getenv("VERTEXAI_CREDENTIALS_JSON"),
    },
})
Custom OpenAI-Compatible Endpoint
engine, err := llm.TextModel(llm.ProviderCustom, llm.LlmOptions{
    ApiKey: "your-api-key",
    Model:  "your-model",
    ProviderOptions: map[string]any{
        "url": "https://your-endpoint.com/v1/chat/completions",
    },
})
Per-Call Option Overrides
// Override options on a per-call basis
response, err := engine.GenerateText(
    "You are a helpful assistant.",
    "Summarize this document: ...",
    llm.LlmOptions{
        MaxTokens:   2000,
        Temperature: llm.PtrFloat64(0.2),
    },
)

Interface

The core interface that all LLM providers must implement:

type LlmInterface interface {
    // GenerateText generates a text response
    GenerateText(systemPrompt string, userPrompt string, options ...LlmOptions) (string, error)

    // GenerateJSON generates a JSON response
    GenerateJSON(systemPrompt string, userPrompt string, options ...LlmOptions) (string, error)

    // GenerateImage generates an image from a prompt
    GenerateImage(prompt string, options ...LlmOptions) ([]byte, error)

    // GenerateEmbedding generates embeddings for the given text
    GenerateEmbedding(text string) ([]float32, error)

    // Generate generates content (DEPRECATED: use GenerateText or GenerateJSON)
    Generate(systemPrompt string, userMessage string, options ...LlmOptions) (string, error)
}

Configuration Options

Option Type Description
Provider Provider LLM provider to use (openai, gemini, vertex, anthropic, openrouter, custom, mock)
ApiKey string API key for the provider
ProjectID string GCP project ID (Vertex AI)
Region string GCP region (Vertex AI, defaults to europe-west1)
Model string Model identifier
MaxTokens int Maximum tokens to generate (default: 4096, Vertex: 8192)
Temperature *float64 Randomness control, 0.0–1.0 (default: 0.7). Use PtrFloat64(val) to set; nil uses default.
Verbose bool Enable verbose logging
Logger *slog.Logger Structured logger for production use
OutputFormat OutputFormat Output format (text, json, xml, yaml, image/png, image/jpeg)
ProviderOptions map[string]any Provider-specific options (credentials, endpoint URLs, etc.)
MockResponse string Canned response for mock provider (excluded from JSON serialization)

Factory Functions

Function Description
TextModel(provider, options) Creates an LLM configured for text output
JSONModel(provider, options) Creates an LLM configured for JSON output
ImageModel(provider, options) Creates an LLM configured for image generation
NewLLM(options) Low-level constructor with full control

OpenRouter Model Constants

The package provides pre-defined constants for popular models available via OpenRouter:

Category Examples
OpenAI OPENROUTER_MODEL_GPT_5_2, OPENROUTER_MODEL_GPT_5_2_CODEX, OPENROUTER_MODEL_O4_MINI
Anthropic OPENROUTER_MODEL_CLAUDE_SONNET_4_5, OPENROUTER_MODEL_CLAUDE_OPUS_4_6, OPENROUTER_MODEL_CLAUDE_HAIKU_4_5
Google OPENROUTER_MODEL_GEMINI_2_5_PRO, OPENROUTER_MODEL_GEMINI_3_PRO_PREVIEW
Mistral OPENROUTER_MODEL_MISTRAL_MEDIUM_3_1, OPENROUTER_MODEL_DEVSTRAL_2512
Qwen OPENROUTER_MODEL_QWEN_3_MAX_THINKING, OPENROUTER_MODEL_QWEN_3_CODER_NEXT
xAI OPENROUTER_MODEL_GROK_3, OPENROUTER_MODEL_GROK_4
DeepSeek OPENROUTER_MODEL_DEEPSEEK_V3_1
Image OPENROUTER_MODEL_GPT_5_IMAGE, OPENROUTER_MODEL_GEMINI_2_5_FLASH_IMAGE
Embedding OPENROUTER_MODEL_TEXT_EMBEDDING_3_LARGE, OPENROUTER_MODEL_QWEN_3_EMBEDDING_0_6B

See openrouter_models.go for the full list with pricing and context window sizes.

Adding a Custom Provider

Option 1: Use RegisterCustomProvider
llm.RegisterCustomProvider("my-provider", func(options llm.LlmOptions) (llm.LlmInterface, error) {
    return NewMyProvider(options)
})

engine, err := llm.NewLLM(llm.LlmOptions{
    Provider: llm.Provider("my-provider"),
    ApiKey:   "...",
})
Option 2: Implement LlmInterface
  1. Create a new file yourprovider_implementation.go
  2. Implement all methods of LlmInterface
  3. Register via RegisterProvider in an init() function
type myProvider struct {
    options llm.LlmOptions
}

func (p *myProvider) GenerateText(systemPrompt, userPrompt string, opts ...llm.LlmOptions) (string, error) {
    // Your implementation
}

func (p *myProvider) GenerateJSON(systemPrompt, userPrompt string, opts ...llm.LlmOptions) (string, error) {
    // Your implementation
}

func (p *myProvider) GenerateImage(prompt string, opts ...llm.LlmOptions) ([]byte, error) {
    // Your implementation
}

func (p *myProvider) GenerateEmbedding(text string) ([]float32, error) {
    // Your implementation
}

func (p *myProvider) Generate(systemPrompt, userMessage string, opts ...llm.LlmOptions) (string, error) {
    // Your implementation
}

Provider-Specific Notes

OpenAI
  • Requires OPENAI_API_KEY environment variable or ApiKey option
  • Image generation returns decoded PNG bytes via the DALL-E API
  • Supports model and size overrides via options for image generation
Gemini
  • Requires GEMINI_API_KEY environment variable or ApiKey option
  • Uses the google.golang.org/genai SDK with system instruction support
  • Defaults to gemini-2.5-flash if no model is specified
Vertex AI
  • Requires GCP project ID and region
  • Credentials can be supplied in several ways:
    1. ProviderOptions["credentials_json"] — raw service-account JSON string or []byte
    2. ProviderOptions["credentials_file"] — path to a service-account JSON file
    3. Environment variables: VERTEXAI_CREDENTIALS_JSON, VERTEXAI_CREDENTIALS_FILE, or GOOGLE_APPLICATION_CREDENTIALS
    4. Application Default Credentials as fallback
Anthropic
  • Requires ANTHROPIC_API_KEY environment variable or ApiKey option
  • Supports custom TLS configuration via provider options:
    • anthropic_root_ca_file / ANTHROPIC_ROOT_CA_FILE — custom root CA file
    • anthropic_root_ca_pem / ANTHROPIC_ROOT_CA_PEM — custom root CA PEM
    • anthropic_spki_hash / ANTHROPIC_EXPECTED_SPKI_HASH — certificate SPKI pin
OpenRouter
  • Requires OPENROUTER_API_KEY environment variable or ApiKey option
  • Provides access to models from multiple providers through a single API
  • Image generation uses the chat completions endpoint with modalities: ["image", "text"]
  • Supports structured logging via Logger option
Custom
  • Requires an endpoint URL via ProviderOptions["url"], ProviderOptions["endpoint_url"], or ProviderOptions["base_url"]
  • Sends OpenAI-compatible chat completion requests
  • Falls back to plain-text response parsing if JSON parsing fails

Testing

The package includes a mock implementation for testing:

// Create a mock LLM with a default response
mockLLM, _ := llm.NewLLM(llm.LlmOptions{
    Provider:     llm.ProviderMock,
    MockResponse: "This is a mock response",
})

// Or provide per-call mock responses
response, _ := mockLLM.GenerateText(
    "system prompt",
    "user message",
    llm.LlmOptions{
        MockResponse: "Specific response for this test case",
    },
)

The mock returns the first non-empty MockResponse it finds, checking in order:

  1. Options passed to the specific method call
  2. Options used when creating the LLM instance
Running Tests
go test ./...

Integration tests are skipped automatically when API keys are not set.

Utility Functions

  • CountTokens(text string) int — Approximate token count (words + punctuation)
  • EstimateMaxTokens(promptTokens, contextWindowSize int) int — Estimate remaining tokens in context window

Best Practices

  1. Error Handling: Always check for errors when calling LLM methods
  2. Environment Variables: Store API keys in environment variables, not in code
  3. Prompt Engineering: Craft clear system prompts for better results
  4. Token Management: Be mindful of token limits for large inputs
  5. Structured Logging: Use the Logger option for production observability
  6. Provider Fallback: Implement fallbacks to handle provider outages

Similar Projects

Documentation

Index

Constants

View Source
const GEMINI_MODEL_1_5_FLASH = "gemini-1.5-flash" // supported but now old
View Source
const GEMINI_MODEL_1_5_PRO = "gemini-1.5-pro" // supported but now old
View Source
const GEMINI_MODEL_2_0_FLASH_EXP_IMAGE_GENERATION = "gemini-2.0-flash-exp-image-generation"

const GEMINI_MODEL_2_0_FLASH = "gemini-2.0-flash-001" const GEMINI_MODEL_2_0_FLASH_LITE = "gemini-2.0-flash-lite-001"

View Source
const GEMINI_MODEL_2_5_FLASH = "gemini-2.5-flash"
View Source
const GEMINI_MODEL_2_5_FLASH_LITE = "gemini-2.5-flash-lite"
View Source
const GEMINI_MODEL_2_5_PRO = "gemini-2.5-pro"
View Source
const GEMINI_MODEL_3_0_IMAGEN = "imagen-3.0-generate-002" // not supported
View Source
const OPENROUTER_MODEL_CLAUDE_HAIKU_4_5 = "anthropic/claude-haiku-4.5"

Anthropic Claude Haiku 4.5 Input $0.80/M Output $4.00/M Context 200,000

View Source
const OPENROUTER_MODEL_CLAUDE_OPUS_4_5 = "anthropic/claude-opus-4.5"

Anthropic Claude Opus 4.5 Input $5.00/M Output $25.00/M Context 200,000

View Source
const OPENROUTER_MODEL_CLAUDE_OPUS_4_6 = "anthropic/claude-opus-4.6"

Anthropic Claude Opus 4.6 Input $5.00/M Output $25.00/M Context 1,000,000

View Source
const OPENROUTER_MODEL_CLAUDE_SONNET_4 = "anthropic/claude-sonnet-4"

Anthropic Claude Sonnet 4 Input $3.00/M Output $15.00/M Context 1,000,000

View Source
const OPENROUTER_MODEL_CLAUDE_SONNET_4_5 = "anthropic/claude-sonnet-4.5"

Anthropic Claude Sonnet 4.5 Input $3.00/M Output $15.00/M Context 1,000,000

View Source
const OPENROUTER_MODEL_CODESTRAL_EMBED_2505 = "mistralai/codestral-embed-2505"

Mistral Codestral Embedding 2505 Input $0.15/M Output $0.00/M

View Source
const OPENROUTER_MODEL_DEEPSEEK_V3_1 = "deepseek/deepseek-chat-v3.1"

DeepSeek DeepSeek V3.1 Input $0.20/M Output $0.80/M Context 163,840

View Source
const OPENROUTER_MODEL_DEVSTRAL_2512 = "mistralai/devstral-2512"

Mistral Devstral 2512 Input $0.05/M Output $0.22/M Context 262,144

View Source
const OPENROUTER_MODEL_GEMINI_2_5_FLASH = "google/gemini-2.5-flash"

Google Gemini 2.5 Flash Input $0.30/M Output $2.50/M Context 1,048,576

View Source
const OPENROUTER_MODEL_GEMINI_2_5_FLASH_IMAGE = "google/gemini-2.5-flash-image"

Google Gemini 2.5 Flash Image Input $0.30/M Output $2.50/M Context 1,048,576

View Source
const OPENROUTER_MODEL_GEMINI_2_5_FLASH_LITE = "google/gemini-2.5-flash-lite"

Google Gemini 2.5 Flash Lite Input $0.10/M Output $0.40/M Context 1,048,576

View Source
const OPENROUTER_MODEL_GEMINI_2_5_PRO = "google/gemini-2.5-pro"

Google Gemini 2.5 Pro Input $1.25/M Output $10/M Context 1,048,576

View Source
const OPENROUTER_MODEL_GEMINI_3_FLASH_PREVIEW = "google/gemini-3-flash-preview"

Google Gemini 3 Flash Preview Input $0.50/M Output $3.00/M Context 1,048,576

View Source
const OPENROUTER_MODEL_GEMINI_3_PRO_PREVIEW = "google/gemini-3-pro-preview"

Google Gemini 3 Pro Preview Input $2/M Output $12/M Context 1,048,576

View Source
const OPENROUTER_MODEL_GEMINI_EMBED_001 = "google/gemini-embedding-001"

Google Gemini Embedding 001 Input $0.15/M Output $0.00/M

View Source
const OPENROUTER_MODEL_GEMMA_3_12B_IT = "google/gemma-3-12b-it"

Google Gemma 3 12B Input $0.048/M Output $0.193/M Context 96,000

View Source
const OPENROUTER_MODEL_GEMMA_3_27B_IT = "google/gemma-3-27b-it"

Google Gemma 3 27B Input $0.067/M Output $0.267/M Context 96,000

View Source
const OPENROUTER_MODEL_GLM_4_7 = "z-ai/glm-4.7"

Z.AI GLM 4.7 Input $0.40/M Output $1.50/M Context 202,752

View Source
const OPENROUTER_MODEL_GLM_4_7_FLASH = "z-ai/glm-4.7-flash"

Z.AI GLM 4.7 Flash Input $0.06/M Output $0.40/M Context 202,752

View Source
const OPENROUTER_MODEL_GPT_4_1_NANO = "openai/gpt-4.1-nano"

OpenAI GPT-4.1 Nano Input $0.10/M Output $0.40/M Context 1,047,576

View Source
const OPENROUTER_MODEL_GPT_5_1 = "openai/gpt-5.1"

OpenAI GPT-5.1 Input $1.25/M Output $10.00/M Context 400,000 Web Search: $10/K

View Source
const OPENROUTER_MODEL_GPT_5_2 = "openai/gpt-5.2"

OpenAI GPT-5.2 Input $1.75/M Output $14.00/M Context 400,000 Web Search: $10/K

View Source
const OPENROUTER_MODEL_GPT_5_2_CHAT = "openai/gpt-5.2-chat"

OpenAI GPT-5.2 Chat (Instant) Input $1.75/M Output $14.00/M Context 128,000 Web Search: $10/K

View Source
const OPENROUTER_MODEL_GPT_5_2_CODEX = "openai/gpt-5.2-codex"

OpenAI GPT-5.2 Codex Input $1.75/M Output $14.00/M Context 400,000 Web Search: $10/K

View Source
const OPENROUTER_MODEL_GPT_5_2_PRO = "openai/gpt-5.2-pro"

OpenAI GPT-5.2 Pro Input $21.00/M Output $168.00/M Context 400,000 Web Search: $10/K

View Source
const OPENROUTER_MODEL_GPT_5_IMAGE = "openai/gpt-5-image"

OpenAI GPT-5 Image Input $10.00/M Output $10/M Context 400,000

View Source
const OPENROUTER_MODEL_GPT_5_IMAGE_MINI = "openai/gpt-5-image-mini"

OpenAI GPT-5 Image Mini Input $2.50/M Output $2/M Context 1,048,576

View Source
const OPENROUTER_MODEL_GPT_5_NANO = "openai/gpt-5-nano"

OpenAI GPT-5 Nano Input $0.05/M Output $0.40/M Context 400,000

View Source
const OPENROUTER_MODEL_GPT_OSS_120B = "openai/gpt-oss-120b"

OpenAI GPT-OSS-120B Input $0.072/M Output $0.28/M Context 131,072

View Source
const OPENROUTER_MODEL_GPT_OSS_20B = "openai/gpt-oss-20b"

OpenAI GPT-OSS-20B Input $0.04/M Output $0.15/M Context 131,072

View Source
const OPENROUTER_MODEL_GROK_3 = "x-ai/grok-3"

xAI Grok 3 Input $3.00/M Output $15.00/M Context 131,072

View Source
const OPENROUTER_MODEL_GROK_3_MINI = "x-ai/grok-3-mini"

xAI Grok 3 Mini Input $0.30/M Output $0.50/M Context 131,072

View Source
const OPENROUTER_MODEL_GROK_4 = "x-ai/grok-4"

xAI Grok 4 Input $3.00/M Output $15.00/M Context 256,000

View Source
const OPENROUTER_MODEL_KIMI_K2_5 = "moonshotai/kimi-k2.5"

MoonshotAI Kimi K2.5 Input $0.45/M Output $2.25/M Context 262,144

View Source
const OPENROUTER_MODEL_MIMO_V2_FLASH = "xiaomi/mimo-v2-flash"

Xiaomi MiMo-V2-Flash Input $0.09/M Output $0.29/M Context 262,144

View Source
const OPENROUTER_MODEL_MINIMAX_M2_1 = "minimax/minimax-m2.1"

MiniMax M2.1 Input $0.27/M Output $0.95/M Context 196,608

View Source
const OPENROUTER_MODEL_MISTRAL_EMBED_2312 = "mistralai/mistral-embed-2312"

Mistral Mistral Embedding 2312 Input $0.10/M Output $0.00/M

View Source
const OPENROUTER_MODEL_MISTRAL_MEDIUM_3_1 = "mistralai/mistral-medium-3.1"

Mistral Mistral Medium 3.1 Input $0.40/M Output $2/M Context 131,072

View Source
const OPENROUTER_MODEL_MISTRAL_NEMO = "mistralai/mistral-nemo"

Mistral Mistral Nemo Input $0.02/M Output $0.04/M Context 131,072

View Source
const OPENROUTER_MODEL_O4_MINI = "openai/o4-mini"

OpenAI O4 Mini Input $1.10/M Output $4.40/M Context 200,000

View Source
const OPENROUTER_MODEL_QWEN_3_235B_A22B_INSTRUCT_2507 = "qwen/qwen3-235b-a22b-2507"

Qwen Qwen3 235B A22B Instruct 2507 Input $0.078/M Output $0.312/M Context 262,144

View Source
const OPENROUTER_MODEL_QWEN_3_30B_A3B = "qwen/qwen3-30b-a3b"

Qwen Qwen3 30B A3B Input $0.02/M Output $0.08/M Context 40,960

View Source
const OPENROUTER_MODEL_QWEN_3_CODER_NEXT = "qwen/qwen3-coder-next"

Qwen Qwen3 Coder Next Input $0.07/M Output $0.30/M Context 262,144

View Source
const OPENROUTER_MODEL_QWEN_3_EMBEDDING_0_6B = "qwen/qwen3-embedding-0.6b"

Qwen Qwen3 Embedding 0.6B Input $0.01/M Output $0.00/M

View Source
const OPENROUTER_MODEL_QWEN_3_MAX_THINKING = "qwen/qwen3-max-thinking"

Qwen Qwen3 Max Thinking Input $1.20/M Output $6.00/M Context 262,144

View Source
const OPENROUTER_MODEL_SEED_1_6 = "bytedance-seed/seed-1.6"

ByteDance Seed 1.6 Input $0.25/M Output $2.00/M Context 262,144

View Source
const OPENROUTER_MODEL_SEED_1_6_FLASH = "bytedance-seed/seed-1.6-flash"

ByteDance Seed 1.6 Flash Input $0.075/M Output $0.30/M Context 262,144

View Source
const OPENROUTER_MODEL_STEP_3_5_FLASH = "stepfun/step-3.5-flash"

StepFun Step 3.5 Flash Input $0.10/M Output $0.30/M Context 256,000

View Source
const OPENROUTER_MODEL_TEXT_EMBEDDING_3_LARGE = "openai/text-embedding-3-large"

OpenAI Text Embedding 3 Large Input $0.13/M Output $0.00/M

View Source
const OPENROUTER_MODEL_TEXT_EMBEDDING_3_SMALL = "openai/text-embedding-3-small"

OpenAI Text Embedding 3 Small Input $0.02/M Output $0.00/M

View Source
const OPENROUTER_MODEL_TEXT_EMBEDDING_ADA_002 = "openai/text-embedding-ada-002"

OpenAI Text Embedding Ada 002 Input $0.10/M Output $0.00/M

Variables

This section is empty.

Functions

func CountTokens

func CountTokens(text string) int

CountTokens provides a simple approximation of token counting Note: This is a basic implementation and not accurate for all models Production code should use model-specific tokenizers

func EstimateMaxTokens

func EstimateMaxTokens(promptTokens, contextWindowSize int) int

EstimateMaxTokens estimates the maximum number of tokens that could be generated given the model's context window size and the prompt length

func PtrFloat64 added in v1.3.0

func PtrFloat64(v float64) *float64

PtrFloat64 returns a pointer to the given float64 value. This is a convenience helper for setting Temperature in LlmOptions.

func RegisterCustomProvider added in v0.2.0

func RegisterCustomProvider(name string, factory LlmFactory)

RegisterCustomProvider registers a custom LLM provider

func RegisterProvider added in v0.2.0

func RegisterProvider(provider Provider, factory LlmFactory)

RegisterProvider registers a new LLM provider factory

Types

type AgentInterface added in v0.2.0

type AgentInterface interface {
	// SetRole sets the role of the agent
	// i.e. "You are a helpful assistant"
	SetRole(role string)

	// GetRole returns the role of the agent
	GetRole() string

	// SetTask sets the task for the agent
	// i.e. "Your task is to write a book about self-improvement"
	SetTask(task string)

	// GetTask returns the task of the agent
	GetTask() string

	// Execute runs the agent and returns the response
	Execute() (response string, err error)
}

AgentInterface defines the core interface that all agents must implement

type LlmFactory added in v0.2.0

type LlmFactory func(options LlmOptions) (LlmInterface, error)

LlmFactory is a function type that creates a new LLM instance Now returns (LlmInterface, error)

type LlmInterface added in v0.2.0

type LlmInterface interface {
	// GenerateText generates a text response from the LLM based on the given prompt
	GenerateText(systemPrompt string, userPrompt string, options ...LlmOptions) (string, error)

	// GenerateJSON generates a JSON response from the LLM based on the given prompt
	GenerateJSON(systemPrompt string, userPrompt string, options ...LlmOptions) (string, error)

	// GenerateImage generates an image from the LLM based on the given prompt
	GenerateImage(prompt string, options ...LlmOptions) ([]byte, error)

	// DEPRECATED: Generate generates a response from the LLM based on the given prompt and options
	Generate(systemPrompt string, userMessage string, options ...LlmOptions) (string, error)

	// GenerateEmbedding generates embeddings for the given text
	GenerateEmbedding(text string) ([]float32, error)
}

LlmInterface is an interface for making LLM API calls

func ImageModel

func ImageModel(provider Provider, options LlmOptions) (LlmInterface, error)

ImageModel creates an LLM model for image output

func JSONModel

func JSONModel(provider Provider, options LlmOptions) (LlmInterface, error)

JSONModel creates an LLM model for JSON output

func NewLLM added in v0.2.0

func NewLLM(options LlmOptions) (LlmInterface, error)

NewLLM creates a new LLM instance based on the provider specified in options

func TextModel

func TextModel(provider Provider, options LlmOptions) (LlmInterface, error)

TextModel creates an LLM model for text output

type LlmOptions added in v0.2.0

type LlmOptions struct {
	// Provider specifies which LLM provider to use
	Provider Provider

	// MockResponse, if not empty, will be returned by the mock implementation
	// instead of making an actual API call. This is useful for testing.
	MockResponse string `json:"-"`

	// ApiKey specifies the API key for the LLM provider
	ApiKey string

	// ProjectID specifies the project ID for the LLM (used by Vertex AI)
	ProjectID string

	// Region specifies the region for the LLM (used by Vertex AI)
	Region string

	// Model specifies the LLM model to use
	Model string

	// MaxTokens specifies the maximum number of tokens to generate
	MaxTokens int

	// Temperature controls the randomness of the response.
	// A higher temperature (e.g., 0.8) makes the output more random and creative,
	// while a lower temperature (e.g., 0.2) makes the output more focused and deterministic.
	// Use PtrFloat64(0.7) to set, or leave nil to use the provider default.
	Temperature *float64

	// Verbose controls whether to log detailed information
	Verbose bool

	// Logger specifies a logger to use for error logging
	Logger *slog.Logger

	// OutputFormat specifies the output format from the LLM
	OutputFormat OutputFormat

	// Additional options specific to the LLM provider
	ProviderOptions map[string]any
}

type OutputFormat

type OutputFormat string

OutputFormat specifies the desired output format from the LLM

const (
	OutputFormatText     OutputFormat = "text"
	OutputFormatJSON     OutputFormat = "json"
	OutputFormatXML      OutputFormat = "xml"
	OutputFormatYAML     OutputFormat = "yaml"
	OutputFormatEnum     OutputFormat = "enum"
	OutputFormatImagePNG OutputFormat = "image/png"
	OutputFormatImageJPG OutputFormat = "image/jpeg"
)

type Provider

type Provider string

Provider represents an LLM provider type

const (
	ProviderOpenAI     Provider = "openai"
	ProviderGemini     Provider = "gemini"
	ProviderVertex     Provider = "vertex"
	ProviderMock       Provider = "mock"
	ProviderAnthropic  Provider = "anthropic"
	ProviderOpenRouter Provider = "openrouter"
	ProviderCustom     Provider = "custom"
)

Supported LLM providers

Jump to

Keyboard shortcuts

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