provider

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package provider defines RiskKernel's LLM provider abstraction. A Provider is the only place in the codebase (besides internal/otel) permitted to make outbound network calls. Each Chat call returns token Usage so the deterministic governor and cost ledger can attribute spend to a run.

v0.1 implements Anthropic natively; OpenAI, Bedrock, and Ollama are stubs that return ErrNotImplemented. The interface is intentionally provider-neutral so the gateway and governor never special-case a vendor.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotImplemented = errors.New("provider: not implemented in v0.1")

ErrNotImplemented is returned by stub providers that are not yet wired up.

Functions

This section is empty.

Types

type Anthropic

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

Anthropic implements Provider against the Anthropic Messages API. It is the native v0.1 provider (the founder builds on Claude).

func NewAnthropic

func NewAnthropic(apiKey string) *Anthropic

NewAnthropic constructs an Anthropic provider. apiKey must be non-empty.

func (*Anthropic) Chat

func (a *Anthropic) Chat(ctx context.Context, req Request) (*Response, error)

Chat performs one chat completion against the Anthropic Messages API.

func (*Anthropic) Name

func (a *Anthropic) Name() string

Name returns the stable provider identifier.

type Bedrock

type Bedrock struct{}

Bedrock is a stub. Native implementation is planned post-v0.1.

func NewBedrock

func NewBedrock() *Bedrock

func (*Bedrock) Chat

func (b *Bedrock) Chat(context.Context, Request) (*Response, error)

func (*Bedrock) Name

func (b *Bedrock) Name() string

type Message

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

Message is a single turn in a conversation.

type Ollama

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

Ollama is a stub for local models. Native implementation is planned post-v0.1.

func NewOllama

func NewOllama(baseURL string) *Ollama

func (*Ollama) Chat

func (o *Ollama) Chat(context.Context, Request) (*Response, error)

func (*Ollama) Name

func (o *Ollama) Name() string

type OpenAI

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

OpenAI implements Provider against the OpenAI Chat Completions API.

func NewOpenAI

func NewOpenAI(apiKey string) *OpenAI

NewOpenAI constructs an OpenAI provider.

func (*OpenAI) Chat

func (o *OpenAI) Chat(ctx context.Context, req Request) (*Response, error)

Chat performs one chat completion against the OpenAI Chat Completions API.

func (*OpenAI) Name

func (o *OpenAI) Name() string

Name returns the stable provider identifier.

type Provider

type Provider interface {
	// Name is the stable provider identifier used in config, routing, and the
	// OTel gen_ai.system attribute (e.g. "anthropic").
	Name() string
	// Chat performs one chat completion. It must honor ctx for cancellation and
	// deadlines — this is how the governor's kill switch and time budget reach an
	// in-flight call. It must always populate Usage on success.
	Chat(ctx context.Context, req Request) (*Response, error)
}

Provider is an LLM backend. Implementations must be safe for concurrent use.

type Registry

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

Registry holds the configured providers, keyed by Name(). It is read-only after construction and safe for concurrent use.

func NewRegistry

func NewRegistry(defaultName string, ps ...Provider) (*Registry, error)

NewRegistry builds a registry from the given providers. defaultName selects the provider used when a request does not specify one; it must be present.

func (*Registry) Default

func (r *Registry) Default() Provider

Default returns the default provider.

func (*Registry) Get

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

Get returns the provider with the given name. An empty name resolves to the default provider.

func (*Registry) Names

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

Names returns the registered provider names (for diagnostics).

type Request

type Request struct {
	// Model is the provider-specific model identifier (e.g. "claude-sonnet-4-5").
	Model string `json:"model"`
	// System is an optional system prompt, kept separate from Messages because
	// some providers (Anthropic) take it as a distinct field.
	System string `json:"system,omitempty"`
	// Messages is the ordered conversation, excluding the system prompt.
	Messages []Message `json:"messages"`
	// MaxTokens caps the completion length. Required by some providers
	// (Anthropic); a provider may supply a sane default when zero.
	MaxTokens int `json:"max_tokens,omitempty"`
	// Temperature is optional; nil means "use the provider default".
	Temperature *float64 `json:"temperature,omitempty"`
}

Request is a provider-neutral chat completion request.

type Response

type Response struct {
	// ID is the provider's response identifier, for the OTel span and audit log.
	ID string `json:"id"`
	// Model is the model that actually served the request.
	Model string `json:"model"`
	// Content is the assistant's text output (concatenated across content blocks).
	Content string `json:"content"`
	// FinishReason is the provider's stop reason, normalized loosely
	// ("stop", "length", "tool_use", ...).
	FinishReason string `json:"finish_reason"`
	// Usage is the token accounting for this call.
	Usage Usage `json:"usage"`
}

Response is a provider-neutral chat completion result.

type Role

type Role string

Role identifies the author of a message.

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

type Usage

type Usage struct {
	PromptTokens     int64 `json:"prompt_tokens"`
	CompletionTokens int64 `json:"completion_tokens"`
}

Usage reports tokens consumed by a single call. This is the unit the governor meters and the cost ledger prices.

func (Usage) Total

func (u Usage) Total() int64

Total returns the sum of prompt and completion tokens.

Jump to

Keyboard shortcuts

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