llm

package
v0.17.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	DefaultAnthropicModel          = "claude-opus-4-8"
	DefaultAnthropicRequestTimeout = 120 * time.Second
	DefaultAnthropicMaxTokens      = 4096
)

Variables

View Source
var (
	ErrDisabled      = errors.New("llm: disabled by configuration")
	ErrAuth          = errors.New("llm: authentication failed")
	ErrRateLimited   = errors.New("llm: rate limited")
	ErrOverloaded    = errors.New("llm: provider overloaded")
	ErrInvalidOutput = errors.New("llm: output did not match schema")
)

Functions

This section is empty.

Types

type AnthropicClient

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

func NewAnthropicClient

func NewAnthropicClient(cfg AnthropicConfig) *AnthropicClient

func (*AnthropicClient) CompleteStructured

func (c *AnthropicClient) CompleteStructured(ctx context.Context, req StructuredRequest) (*StructuredResponse, error)

type AnthropicConfig

type AnthropicConfig struct {
	Enabled        bool
	APIKey         string
	Model          string
	BaseURL        string
	RequestTimeout time.Duration
}

type CacheTTL

type CacheTTL string

CacheTTL controls prompt-cache breakpoints. The empty value disables caching for that block; "5m" and "1h" map to Anthropic's ephemeral cache lifetimes.

const (
	CacheTTLNone CacheTTL = ""
	CacheTTL5m   CacheTTL = "5m"
	CacheTTL1h   CacheTTL = "1h"
)

type Client

type Client interface {
	// CompleteStructured sends system+user prompts and a JSON schema the
	// response must conform to. Returns raw schema-constrained JSON.
	CompleteStructured(ctx context.Context, req StructuredRequest) (*StructuredResponse, error)
}

type FakeClient

type FakeClient struct {
	Raw          json.RawMessage
	Model        string
	InputTokens  int
	OutputTokens int
	Err          error
	Requests     []StructuredRequest
	Responses    []*StructuredResponse
	Errors       []error
	// contains filtered or unexported fields
}

func (*FakeClient) CompleteStructured

func (f *FakeClient) CompleteStructured(ctx context.Context, req StructuredRequest) (*StructuredResponse, error)

type OutputError

type OutputError struct {
	Reason       string
	StopReason   string
	OutputTokens int
	// Text is the raw provider text content (possibly truncated JSON or prose).
	Text string
}

OutputError carries diagnostic detail about an invalid or truncated structured response so callers can log what the provider actually returned (e.g. the truncated JSON) instead of just the generic schema-mismatch message. It unwraps to ErrInvalidOutput so existing errors.Is checks keep working.

func (*OutputError) Error

func (e *OutputError) Error() string

func (*OutputError) Unwrap

func (e *OutputError) Unwrap() error

type RateLimitError

type RateLimitError struct {
	RetryAfter time.Duration
	Err        error
}

RateLimitError wraps a provider 429 and carries the Retry-After hint (zero when the provider did not supply one). It unwraps to ErrRateLimited so existing errors.Is(err, ErrRateLimited) checks keep working.

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

func (*RateLimitError) Unwrap

func (e *RateLimitError) Unwrap() error

type StructuredRequest

type StructuredRequest struct {
	System    string
	Prompt    string
	Schema    map[string]any // JSON Schema for the output object
	MaxTokens int

	// Prompt caching (additive; zero values preserve the uncached behaviour).
	// SystemCacheTTL sets a cache breakpoint at the end of the system block.
	SystemCacheTTL CacheTTL
	// CachedUserPrefix, when non-empty, is emitted as a user text block before
	// Prompt with its own cache breakpoint. Use it for the large, stable portion
	// of the user content so the volatile tail in Prompt stays uncached.
	CachedUserPrefix    string
	CachedUserPrefixTTL CacheTTL
}

type StructuredResponse

type StructuredResponse struct {
	Raw json.RawMessage
	// EmptyOutput is true when the provider returned no structured JSON output
	// (e.g. a plain-text "nothing applies" message) rather than a schema-shaped
	// payload. Raw is nil in that case. Callers should treat this as a valid
	// "no result" outcome, not a failure. It is distinct from a truncated
	// response, which surfaces as ErrInvalidOutput so it can be retried.
	EmptyOutput  bool
	Model        string
	InputTokens  int
	OutputTokens int
	// Prompt-cache accounting; cache reads do not count against ITPM rate limits
	// for most models, so these are useful for verifying caching is effective.
	CacheReadInputTokens     int
	CacheCreationInputTokens int
}

Jump to

Keyboard shortcuts

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