Help us shape the future of Go! Take the 2026 Go Developer Survey

llm

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOff    = errors.New("LLM analysis is off (llm.provider = none)")
	ErrBudget = errors.New("LLM budget for this change is used up")
)

Functions

func EstimateTokens

func EstimateTokens(req Request) int64

EstimateTokens uses about 4 characters per token, which is close enough for a budget guard.

Types

type Budget

type Budget struct {
	MaxUSD    float64
	InPerMTok float64
	OutPerMT  float64
	SpentUSD  float64
	Calls     int
	CacheHits int
	// contains filtered or unexported fields
}

func NewBudget

func NewBudget(cfg config.LLM) *Budget

func (*Budget) Add

func (b *Budget) Add(r Response)

func (*Budget) Reserve

func (b *Budget) Reserve(req Request) error

type Cache

type Cache struct{ Dir string }

func (Cache) Get

func (c Cache) Get(p Provider, req Request) (Response, bool)

func (Cache) Put

func (c Cache) Put(p Provider, req Request, r Response)

type Client

type Client struct {
	Provider Provider
	Cache    Cache
	Budget   *Budget
}

Client adds the cache, the budget guard and the JSON validation to a provider.

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, req Request) (Response, error)

func (*Client) CompleteJSON

func (c *Client) CompleteJSON(ctx context.Context, req Request, out any) (Response, error)

CompleteJSON decodes the response into out. It retries once when the response is not valid JSON for out.

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type Mock

type Mock struct {
	Requests []Request
	Respond  func(Request) (string, error)
	// contains filtered or unexported fields
}

Mock is a deterministic provider for tests. Respond decides the reply; it records every request so tests can check what would leave the machine.

func NewMock

func NewMock() *Mock

func (*Mock) All

func (m *Mock) All() []Request

func (*Mock) Complete

func (m *Mock) Complete(_ context.Context, req Request) (Response, error)

func (*Mock) Model

func (m *Mock) Model() string

func (*Mock) Name

func (m *Mock) Name() string

type Provider

type Provider interface {
	Name() string
	Model() string
	Complete(ctx context.Context, req Request) (Response, error)
}

func New

func New(cfg config.LLM) (Provider, error)

type Request

type Request struct {
	Task          string         `json:"task"`
	PromptVersion string         `json:"prompt_version"`
	System        string         `json:"system"`
	Messages      []Message      `json:"messages"`
	Schema        map[string]any `json:"schema,omitempty"`
	MaxTokens     int            `json:"max_tokens"`
	Temperature   float64        `json:"temperature"`
}

type Response

type Response struct {
	Text         string
	Model        string
	InputTokens  int64
	OutputTokens int64
	Cached       bool
}

Jump to

Keyboard shortcuts

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