provider

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: Apache-2.0 Imports: 14 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.

Anthropic, OpenAI, Ollama, and AWS Bedrock are implemented natively. The interface is intentionally provider-neutral so the gateway and governor never special-case a vendor; the long tail is fronted via LiteLLM (see docs).

Index

Constants

This section is empty.

Variables

This section is empty.

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) ChatStream added in v0.7.0

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

ChatStream implements the Streamer interface: a streaming completion that yields Anthropic's raw SSE events verbatim (so the client receives authentic Anthropic SSE) while accumulating token usage for metering. Usage is assembled from the stream's own accounting: message_start carries input_tokens (and the model), message_delta carries the final cumulative output_tokens.

func (*Anthropic) Name

func (a *Anthropic) Name() string

Name returns the stable provider identifier.

func (*Anthropic) WithBaseURL added in v0.5.0

func (a *Anthropic) WithBaseURL(url string) *Anthropic

WithBaseURL overrides the API base — point it at a proxy that fronts Anthropic or a local mock (e.g. for benchmarking). Empty keeps the default. Returns the provider for chaining.

type Bedrock

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

Bedrock implements Provider against the AWS Bedrock Runtime **Converse** API, signed with hand-rolled SigV4 (no AWS SDK dependency — see sigv4.go). The unified Converse API works across Bedrock's hosted models and returns token usage, which the governor meters and the ledger prices. BYO AWS credentials, read from the standard AWS env vars and never stored.

func NewBedrock

func NewBedrock(accessKey, secretKey, sessionToken, region string) *Bedrock

NewBedrock constructs a Bedrock provider for the given region and credentials.

func (*Bedrock) Chat

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

Chat performs one chat completion against the Bedrock Converse API.

func (*Bedrock) Name

func (b *Bedrock) Name() string

Name returns the stable provider identifier.

func (*Bedrock) WithBaseURL added in v0.8.0

func (b *Bedrock) WithBaseURL(u string) *Bedrock

WithBaseURL overrides the runtime endpoint — for a VPC/PrivateLink endpoint or a test mock. Empty keeps the regional default. Returns the provider for chaining.

type ChatStream added in v0.7.0

type ChatStream interface {
	Recv() ([]byte, error)
	Usage() Usage
	Model() string
	Close() error
}

ChatStream is an open streaming response. Recv returns the next raw SSE chunk to forward to the client (io.EOF when the stream ends). After io.EOF, Usage and Model report the call's final accounting, parsed from the stream.

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 implements Provider against a local (or self-hosted) Ollama server's /api/chat endpoint — local models, no API key, your machine. Token usage comes from Ollama's prompt_eval_count / eval_count so the governor and cost ledger meter local runs the same way as hosted ones (a local model is typically priced at $0, which the pricing table handles).

func NewOllama

func NewOllama(baseURL string) *Ollama

NewOllama constructs an Ollama provider. An empty baseURL uses the local default.

func (*Ollama) Chat

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

Chat performs one chat completion against Ollama's /api/chat (non-streaming).

func (*Ollama) Name

func (o *Ollama) Name() string

Name returns the stable provider identifier.

func (*Ollama) WithBaseURL added in v0.7.0

func (o *Ollama) WithBaseURL(url string) *Ollama

WithBaseURL overrides the server URL (empty keeps the current). Returns the provider for chaining, matching the other providers.

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) ChatStream added in v0.7.0

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

ChatStream implements the Streamer interface: a streaming chat completion that yields OpenAI's raw SSE chunks verbatim while accumulating usage. It asks for a final usage chunk (stream_options.include_usage) so the call can be metered.

func (*OpenAI) Name

func (o *OpenAI) Name() string

Name returns the stable provider identifier.

func (*OpenAI) WithBaseURL added in v0.5.0

func (o *OpenAI) WithBaseURL(url string) *OpenAI

WithBaseURL overrides the API base — point it at an OpenAI-compatible gateway, a corporate proxy, or a local mock (e.g. for benchmarking). Empty keeps the default. Returns the provider for chaining.

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 Streamer added in v0.7.0

type Streamer interface {
	// ChatStream starts a streaming completion. The returned ChatStream yields the
	// provider's raw SSE chunks verbatim (so the client receives authentic,
	// untranslated SSE) while it accumulates token usage. Honor ctx so the
	// governor's kill switch / time budget interrupt an in-flight stream.
	ChatStream(ctx context.Context, req Request) (ChatStream, error)
}

Streamer is the optional interface a provider implements to support streaming chat. The gateway type-asserts for it; a provider that doesn't implement it (Ollama and Bedrock, for now) makes a streaming request fall back to a clear "unsupported" error rather than silently buffering.

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