Documentation
¶
Overview ¶
Package llm is the model client: one streaming completion against the configured base URL, with function tools, in one of three wire formats (OpenAI Chat Completions, OpenAI Responses and Anthropic Messages), and one decision from a decision model (OpenAI Decisions, TypeSafe Jev). The API key is a header only. It is never written to a log, and errors that would quote it are scrubbed.
Index ¶
- Constants
- Variables
- func IsChatModel(id string) bool
- type APIError
- type Answer
- type Choice
- type Client
- func (c *Client) Decide(ctx context.Context, state string, qs []Question) (map[string]Answer, error)
- func (c *Client) ListModels(ctx context.Context) ([]string, error)
- func (c *Client) Stream(ctx context.Context, req Request, emit func(Event)) (Message, error)
- func (c *Client) Test(ctx context.Context) error
- type Event
- type Message
- type Part
- type Question
- type Request
- type Tool
- type ToolCall
Constants ¶
const ( FormatChat = "chat" FormatResponses = "responses" FormatAnthropic = "anthropic" // FormatTypeSafe is TypeSafe's System One API, which decides and does // not chat. FormatTypeSafe = "typesafe" )
The wire formats a Client speaks. An empty format is FormatChat.
const ( EffortNone = "none" EffortLow = "low" EffortMedium = "medium" EffortHigh = "high" EffortXHigh = "xhigh" )
The thinking levels a Request may ask for. An empty Effort leaves the provider's default; EffortNone turns reasoning off where the format can.
const AnthropicMaxTokens = 32000
AnthropicMaxTokens is the max_tokens an Anthropic request sends when none is configured, as the format requires one.
const DefaultMaxBytes = 8 << 20
DefaultMaxBytes is the most of one response body a turn will read.
const MaxModels = 1000
MaxModels is the most model IDs ListModels returns.
Variables ¶
var ( ErrProtocol = errors.New("llm: invalid response protocol") ErrIncompleteResponse = errors.New("llm: incomplete response") // ErrTokenLimit is an explicit, structurally complete length response with // no tool calls. It remains incomplete for normal assistant turns. ErrTokenLimit = fmt.Errorf("%w: output token limit reached", ErrIncompleteResponse) )
var ErrBaseURL = errors.New("llm: base URL must be http or https with a host")
ErrBaseURL is the error for a base URL this client will not dial.
var ErrNoChat = errors.New("llm: this provider only makes decisions")
ErrNoChat is the error for a completion asked of a format that cannot chat.
var ErrTooBig = errors.New("llm: response exceeds the size cap")
ErrTooBig is the error when a response exceeds the size cap.
var Formats = []string{FormatChat, FormatResponses, FormatAnthropic, FormatTypeSafe}
Formats lists the wire formats in the order the settings show them.
Functions ¶
func IsChatModel ¶
IsChatModel reports whether id names a model that can chat, judged by the words providers use in the IDs of the others.
Types ¶
type APIError ¶
type APIError struct {
Status int
Message string
Truncated bool
// contains filtered or unexported fields
}
APIError is a non-2xx response. Error never contains the API key.
type Answer ¶
Answer is a decision model's answer to one question: the probability of yes, or the choice picked and the model's confidence in it. Refused is set when the model declined the question.
type Client ¶
type Client struct {
Format string
BaseURL string
APIKey string
Model string
Headers map[string]string
Temperature *float64
TopP *float64
MaxTokens int
// LegacyMaxTokens sends a Chat Completions limit as max_tokens rather
// than max_completion_tokens, for endpoints that know only the old name.
LegacyMaxTokens bool
// MaxBytes caps the response body of one call. Zero means DefaultMaxBytes.
MaxBytes int
// HTTP is optional. Its Transport is reused; a redirect is allowed
// only when its scheme, hostname and port all match BaseURL.
HTTP *http.Client
}
Client talks to one endpoint. A nil Temperature or TopP, or a zero MaxTokens, is omitted so the provider's default applies; an Anthropic request, which needs max_tokens, then sends AnthropicMaxTokens.
func (*Client) Decide ¶
func (c *Client) Decide(ctx context.Context, state string, qs []Question) (map[string]Answer, error)
Decide asks the client's model the questions about state, through OpenAI Decisions or, for FormatTypeSafe, TypeSafe's System One. Every question is answered or refused, or it is an error.
func (*Client) ListModels ¶
ListModels returns the chat models the endpoint offers, sorted and without duplicates: GET {base}/models, following Anthropic's pages.
func (*Client) Stream ¶
Stream posts one completion with streaming on and returns the assembled assistant message. emit, when set, receives each text delta. Cancelling ctx stops the read. A redirect is refused unless its scheme, hostname and port all match BaseURL, so the key is not sent in clear or to another service.
type Event ¶
Event is one piece of streamed text. Thinking is set when the model sends a reasoning delta.
type Message ¶
type Message struct {
Role string
Content string
Parts []Part
ToolCalls []ToolCall
ToolCallID string
Finish string
Opaque json.RawMessage
OpaqueFormat string
OpaqueModel string
}
Message is one chat message. ToolCalls is set on an assistant message that asked for tools; ToolCallID is set on a tool result. Parts are the files a user message carries besides its text.
Opaque is what the provider needs back with an assistant message, such as reasoning items or signed thinking blocks. It is replayed only to the format and model that produced it (OpaqueFormat, OpaqueModel).
type Question ¶
Question is one question a decision model answers about the state. With Choices it picks one of them; without, it says how likely the answer is yes. Yes and No, when set, say what counts as each.
type Request ¶
type Request struct {
Messages []Message
Tools []Tool
Model string
Effort string
// MaxTokens overrides the client's when not zero.
MaxTokens int
}
Request is one completion. Model and Effort override the client's model and the provider's default thinking when set.