llm

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultProvider added in v0.6.0

func DefaultProvider() (provider, model string)

DefaultProvider returns the instance-wide default provider and model (llm.provider, llm.model), for presets such as agent templates.

func SetDefaultConfig

func SetDefaultConfig(cfg Config)

SetDefaultConfig sets the instance-wide LLM settings (the `llm` block of the configuration). Fields an agent leaves empty fall back to them.

func WithUsageRecorder added in v0.5.0

func WithUsageRecorder(ctx context.Context, r *UsageRecorder) context.Context

WithUsageRecorder returns a context whose LLM calls are recorded by r.

Types

type Client

type Client interface {
	Chat(ctx context.Context, messages []Message, opts ...Option) (*Response, error)
}

Client is the interface that all LLM providers must implement.

func NewAdapter

func NewAdapter(cfg Config) (Client, error)

NewAdapter creates a new `Client` by wrapping the specified LLM provider. Empty fields are filled from SetDefaultConfig.

type Config

type Config = config.LLMConfig

Config represents the LLM configuration, likely from your agent model.

type Message

type Message struct {
	Role       Role           `json:"role"`
	Content    string         `json:"content"`
	ToolCallID string         `json:"tool_call_id,omitempty"`
	ToolCall   *llms.ToolCall `json:"tool_call,omitempty"`
}

Message represents a single message in a conversation.

type Option

type Option func(*options)

Option is a functional option for the Chat method.

func WithToolChoice

func WithToolChoice(choice any) Option

WithToolChoice sets the tool choice option for the LLM call.

func WithTools

func WithTools(tools []llms.Tool) Option

type Provider

type Provider string

Provider represents the LLM provider.

const (
	ProviderOpenAI    Provider = "openai"
	ProviderAnthropic Provider = "anthropic"
	ProviderOllama    Provider = "ollama"
)

type Response

type Response struct {
	Type     ResponseType
	Content  string
	ToolCall *llms.ToolCall
	Usage    Usage // tokens of this call, and its estimated cost
}

Response represents the output from an LLM chat call.

type ResponseType

type ResponseType string

ResponseType indicates the type of the LLM's response.

const (
	ResponseTypeText     ResponseType = "text"
	ResponseTypeToolCall ResponseType = "tool_call"
)

type Role

type Role string

Role represents the role of a message sender.

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

type Usage added in v0.5.0

type Usage struct {
	InputTokens  int     `json:"inputTokens"`
	OutputTokens int     `json:"outputTokens"`
	CostUSD      float64 `json:"costUsd"` // 0 when the model has no configured price
	Calls        int     `json:"calls"`
}

Usage counts the tokens of one or more LLM calls and their estimated cost.

func (Usage) Plus added in v0.5.0

func (u Usage) Plus(o Usage) Usage

Plus returns the sum of two usages.

func (Usage) Total added in v0.5.0

func (u Usage) Total() int

Total is input plus output tokens.

type UsageRecorder added in v0.5.0

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

UsageRecorder accumulates the usage of every LLM call made with a context that carries it, including calls made inside agent functions.

func RecorderFrom added in v0.5.0

func RecorderFrom(ctx context.Context) *UsageRecorder

RecorderFrom returns the recorder carried by ctx, or nil.

func (*UsageRecorder) Add added in v0.5.0

func (r *UsageRecorder) Add(u Usage)

Add records a call.

func (*UsageRecorder) Total added in v0.5.0

func (r *UsageRecorder) Total() Usage

Total returns everything recorded so far.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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