model

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 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

Overview

Package model provides the provider abstraction (§40) and the centralized structured-output pipeline (§39). Providers are config-driven; the fake provider is the deterministic CI path, while the HTTP adapters are exercised by the tagged real-provider smoke test.

Index

Constants

This section is empty.

Variables

View Source
var DefaultModels = map[string][]string{
	"openai":     {"gpt-4o", "gpt-4o-mini", "gpt-4.1", "gpt-4.1-mini", "o3-mini"},
	"anthropic":  {"claude-sonnet-4-5", "claude-3-7-sonnet-latest", "claude-3-5-haiku-latest", "claude-opus-4-1"},
	"gemini":     {"gemini-2.5-flash", "gemini-2.5-pro", "gemini-2.0-flash", "gemini-1.5-pro"},
	"deepseek":   {"deepseek-chat", "deepseek-reasoner"},
	"openrouter": {"openai/gpt-4o", "anthropic/claude-3.7-sonnet", "google/gemini-2.5-flash", "meta-llama/llama-4-maverick"},
	"ollama":     {"llama3.1", "llama3.2", "qwen2.5", "mistral", "deepseek-r1"},
}

DefaultModels is a curated catalog of common models per provider. It seeds the UI/CLI pickers and, since audit 4, backs config.UnknownModelProblems.

Still advisory in the sense that matters: any model name is accepted, and a name absent from this list produces a *warning*, never a rejection, because a valid model can postdate the catalog. But it is no longer only cosmetic -- deleting an entry here makes a correct id start warning, so keep it current.

View Source
var ErrTruncated = errors.New("model: output truncated at the token budget")

ErrTruncated reports that the provider stopped because it hit the output budget rather than because the model finished.

This is the safety net under the stage-budget cap. Capping a request to the provider's ceiling turns a guaranteed 400 into a request that succeeds -- but a structured reply cut mid-object is NOT reliably caught downstream: repairJSON closes unterminated strings and containers, and the review schema has no minItems, so a findings list truncated from 15 entries to 1 validates clean. The result would be a silently shorter adversarial review.

So truncation is detected where it is actually visible -- the provider's own finish reason -- and raised loudly. The engine's retry loop resends the same request, so a truncation surfaces as a real failure with a message naming the cause instead of a quietly diminished artifact.

Functions

func CostUSD

func CostUSD(model string, inputTokens, outputTokens int64) float64

CostUSD returns the cost of input/out tokens (counts) for a model name, using the canonical pricing table and longest-prefix normalization.

func DefaultModelFor

func DefaultModelFor(provider string) string

DefaultModelFor returns a sensible default model name for a provider, or "model" if the provider is unknown.

Types

type AnthropicProvider

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

AnthropicProvider calls the Anthropic messages API natively (§41).

func NewAnthropic

func NewAnthropic(apiKey, baseURL string) *AnthropicProvider

NewAnthropic builds an Anthropic provider.

func (*AnthropicProvider) Capabilities

func (p *AnthropicProvider) Capabilities() Capabilities

func (*AnthropicProvider) Complete

func (p *AnthropicProvider) Complete(ctx context.Context, req Request) (Response, error)

Complete implements Provider.

func (*AnthropicProvider) ListModels

func (p *AnthropicProvider) ListModels(ctx context.Context) ([]string, error)

ListModels lists models for Anthropic via GET /models.

func (*AnthropicProvider) Name

func (p *AnthropicProvider) Name() string

type Capabilities

type Capabilities struct {
	StructuredOutput  bool
	Tools             bool
	Streaming         bool
	ReasoningControls bool
	ContextLimit      int
	MaxOutput         int
}

Capabilities describes what a provider/model supports (§40).

type FakeProvider

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

FakeProvider is a deterministic provider for hermetic CI and tests. When no scripted response is registered it generates input-aware (but clearly synthetic) output from the user's idea, so the no-provider demo responds to what the user actually typed instead of returning hardcoded content.

func NewFakeProvider

func NewFakeProvider() *FakeProvider

NewFakeProvider returns a fake provider with full capabilities.

func (*FakeProvider) Calls

func (f *FakeProvider) Calls() []Request

Calls returns the requests seen so far (for assertions).

func (*FakeProvider) Capabilities

func (f *FakeProvider) Capabilities() Capabilities

Capabilities implements Provider.

func (*FakeProvider) Complete

func (f *FakeProvider) Complete(_ context.Context, req Request) (Response, error)

Complete implements Provider.

func (*FakeProvider) EnqueueResponse

func (f *FakeProvider) EnqueueResponse(schemaName, rawJSON string)

EnqueueResponse appends a response to the FIFO queue for a schema name; the queue is consumed before the default response.

func (*FakeProvider) Name

func (f *FakeProvider) Name() string

Name implements Provider.

func (*FakeProvider) SetResponse

func (f *FakeProvider) SetResponse(schemaName, rawJSON string)

SetResponse registers the raw JSON to return for a schema name.

type GeminiProvider

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

GeminiProvider calls the Google generative-language API natively (§41).

func NewGemini

func NewGemini(apiKey, baseURL string) *GeminiProvider

NewGemini builds a Google Gemini provider. baseURL defaults to the v1beta generative-language endpoint.

func (*GeminiProvider) Capabilities

func (p *GeminiProvider) Capabilities() Capabilities

func (*GeminiProvider) Complete

func (p *GeminiProvider) Complete(ctx context.Context, req Request) (Response, error)

Complete implements Provider.

func (*GeminiProvider) ListModels

func (p *GeminiProvider) ListModels(ctx context.Context) ([]string, error)

ListModels lists models for Google Gemini via GET /models.

func (*GeminiProvider) Name

func (p *GeminiProvider) Name() string

type Image

type Image struct {
	Data      []byte
	MediaType string // e.g. "image/png", "image/jpeg"
}

Image is one image attached to a request (for vision models).

type Message

type Message struct {
	Role    string `json:"role"` // system | user | assistant
	Content string `json:"content"`
}

Message is a single conversation turn.

type ModelLister

type ModelLister interface {
	ListModels(ctx context.Context) ([]string, error)
}

ModelLister is implemented by providers that can list their available models. The catalog (DefaultModels) is only a fallback when a provider cannot (or does not need to) enumerate models live.

type OllamaProvider

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

OllamaProvider is an OpenAI-compatible local provider that additionally enumerates installed models via GET /api/tags (at the server root, not /v1).

func NewOllama

func NewOllama(baseURL string) *OllamaProvider

NewOllama builds an Ollama provider for the given OpenAI-compatible base URL (e.g. http://localhost:11434/v1).

func (*OllamaProvider) ListModels

func (p *OllamaProvider) ListModels(ctx context.Context) ([]string, error)

ListModels lists installed Ollama models via GET /api/tags.

type OpenAIProvider

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

OpenAIProvider calls an OpenAI-compatible chat-completions API (§41). It is used for OpenAI, OpenRouter, Ollama, and any custom OpenAI-compatible endpoint (Groq, Together, LM Studio, …).

func NewOpenAI

func NewOpenAI(apiKey, baseURL string) *OpenAIProvider

NewOpenAI builds an OpenAI provider.

func NewOpenAICompat

func NewOpenAICompat(name, apiKey, baseURL string) *OpenAIProvider

NewOpenAICompat builds an OpenAI-compatible provider with a custom name.

func (*OpenAIProvider) Capabilities

func (p *OpenAIProvider) Capabilities() Capabilities

func (*OpenAIProvider) Complete

func (p *OpenAIProvider) Complete(ctx context.Context, req Request) (Response, error)

Complete implements Provider.

func (*OpenAIProvider) ListModels

func (p *OpenAIProvider) ListModels(ctx context.Context) ([]string, error)

ListModels lists models for an OpenAI-compatible provider (OpenAI, OpenRouter, Groq, Together, …) via GET /models.

func (*OpenAIProvider) Name

func (p *OpenAIProvider) Name() string

type Pricing

type Pricing struct {
	InputUSD  float64
	OutputUSD float64
}

Pricing is the USD cost per 1,000,000 tokens for a model family.

type Provider

type Provider interface {
	Name() string
	Capabilities() Capabilities
	// Complete runs one request and returns a response. It must set
	// ActualModel to the model actually used (no silent fallback, §36).
	Complete(ctx context.Context, req Request) (Response, error)
}

Provider is a model provider adapter.

type Registry

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

Registry holds multiple named providers and routes requests by model spec ("provider/model"). It supports OpenAI, Anthropic, Gemini, OpenRouter, Ollama, and arbitrary OpenAI-compatible endpoints (§40).

func NewRegistry

func NewRegistry(defaultProvider string) *Registry

NewRegistry builds an empty registry with a default provider name.

func (*Registry) Add

func (r *Registry) Add(name string, p Provider)

Add registers a provider under a name.

func (*Registry) Complete

func (r *Registry) Complete(ctx context.Context, spec string, req Request) (Response, error)

Complete resolves a spec and runs it on the matching provider.

func (*Registry) Default

func (r *Registry) Default() string

Default returns the default provider name.

func (*Registry) Get

func (r *Registry) Get(name string) (Provider, bool)

Get returns a provider by name.

func (*Registry) Len

func (r *Registry) Len() int

Len returns the number of registered providers.

func (*Registry) ListModels

func (r *Registry) ListModels(ctx context.Context, name string) ([]string, error)

ListModels returns live models for a provider, falling back to the catalog.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns the registered provider names, sorted.

func (*Registry) Resolve

func (r *Registry) Resolve(spec string) (Provider, string, error)

Resolve maps a model spec to a provider and model name. A spec of "provider/model" selects that provider; a bare model name uses the default provider (§40). Splitting is on the first slash.

func (*Registry) SetDefault

func (r *Registry) SetDefault(name string)

SetDefault sets the provider used when a model spec has no "/" prefix.

type Request

type Request struct {
	Model           string
	System          string
	Messages        []Message
	Images          []Image
	ReasoningEffort string
	MaxOutput       int
	JSONSchema      []byte // if set, structured output is requested
	JSONSchemaName  string
}

Request is a normalized model request. Images, when present, are attached to the (single) user message as vision content by providers that support it.

type Response

type Response struct {
	Text        string
	RawJSON     string
	Usage       Usage
	ActualModel string
}

Response is a normalized model response. RawJSON is the provider's raw (post-redaction) output; Text is the plain-text form when not structured.

type StructuredResult

type StructuredResult struct {
	Raw         []byte         // raw model output (post-redaction)
	Repaired    []byte         // repaired content (post-redaction)
	Valid       map[string]any // validated object
	WasRepaired bool
}

StructuredResult is the outcome of the structured-output pipeline (§39).

func DecodeStructured

func DecodeStructured(raw []byte, schema []byte) (*StructuredResult, error)

DecodeStructured validates raw JSON against schema, applying bounded repair on failure (single pass). It returns the validated object and whether repair was used. Raw and repaired content are returned for digest storage.

type Usage

type Usage struct {
	InputTokens  int64
	OutputTokens int64
	CostUSD      float64
}

Usage records token/cost usage (§36).

Jump to

Keyboard shortcuts

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