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 ¶
NewAnthropic constructs an Anthropic provider. apiKey must be non-empty.
func (*Anthropic) ChatStream ¶ added in v0.7.0
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) WithBaseURL ¶ added in v0.5.0
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 ¶
NewBedrock constructs a Bedrock provider for the given region and credentials.
func (*Bedrock) WithBaseURL ¶ added in v0.8.0
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
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 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 (*Ollama) WithBaseURL ¶ added in v0.7.0
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 (*OpenAI) ChatStream ¶ added in v0.7.0
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) WithBaseURL ¶ added in v0.5.0
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 ¶
NewRegistry builds a registry from the given providers. defaultName selects the provider used when a request does not specify one; it must be present.
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 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.