Documentation
¶
Overview ¶
Package decisions holds the wire types for decision models ("System One" models): models that answer typed questions about a piece of evidence with calibrated probabilities instead of generating text.
Two wire formats are understood:
- OpenAI Decisions (POST /v1/decisions): the canonical client contract the gateway exposes on its unified /v1 ingress. Questions are an ordered array; answers come back as an array in the same order.
- System One (POST /v1/systemone): TypeSafe Jev and wire-compatible self-hosts (llama.cpp serving Kev, OpenJev, Laya, ...). Questions and answers are maps keyed by question name.
The package is a dependency-free leaf so the proxy, the vendors, the plugin SDK and the microgateway can all share one parser and one translator.
Index ¶
- Constants
- Variables
- func InputText(input json.RawMessage) (string, int, error)
- func IsDecisionPath(path string) bool
- func KindForPath(path string) string
- func RenderAnswers(resp *Response) string
- func StateText(raw json.RawMessage) string
- func Validate(req *Request, target Format) error
- type Answer
- type AnswerProbability
- type Choice
- type Format
- type InputTokensDetails
- type Level
- type NamedQuestion
- type OrderedQuestions
- type Question
- type Request
- type Response
- type Summary
- type SystemOneAnswer
- type SystemOneQuestion
- type SystemOneRequest
- type SystemOneResponse
- type Usage
- type ValidationError
Constants ¶
const ( MaxSystemOneChoices = 255 MaxSystemOneLevels = 10 )
System One limits (TypeSafe API reference).
const ( OpenAIEndpoint = "/v1/decisions" SystemOneEndpoint = "/v1/systemone" )
Upstream endpoints, relative to a vendor's API base.
const ( TypePredicate = "predicate" TypeChoice = "choice" TypeScore = "score" // TypeRefusal marks an answer the model declined to give. TypeRefusal = "refusal" )
Question and answer types of the OpenAI (canonical) format.
const ( SystemOneTypeNoul = "noul" SystemOneTypeChoice = "choice" SystemOneTypeScore = "score" )
Question types of the System One format. Choice and score share their names with the OpenAI format; noul is System One's predicate.
const KindDecision = "decision"
KindDecision is the request kind (models.RequestKind, the plugin context's request_kind) of decision-model traffic. Generative traffic has kind "".
Variables ¶
var ErrImagesUnsupported = errors.New("image input is not supported by this decision model; send text input only")
ErrImagesUnsupported is returned when a request carrying image input is translated for an upstream that only accepts text state.
Functions ¶
func InputText ¶
func InputText(input json.RawMessage) (string, int, error)
InputText flattens an OpenAI-format input into plain text and counts its image parts. A string input is returned as is; message inputs have their text parts joined by blank lines.
func IsDecisionPath ¶
IsDecisionPath reports whether an upstream request path targets a decision endpoint in either format. The path may carry any prefix (/llm/rest/{slug}, an Azure deployment path, ...); only the trailing endpoint is matched.
func KindForPath ¶
KindForPath is the request kind of an upstream request path: KindDecision for a decision endpoint, "" otherwise.
func RenderAnswers ¶
RenderAnswers renders a response as compact text, one answer per line, for text-based response filters and logs.
func StateText ¶
func StateText(raw json.RawMessage) string
StateText renders System One state (or instructions) as text: a string as is, an object or array as compact JSON.
Types ¶
type Answer ¶
type Answer struct {
Type string `json:"type"`
Name string `json:"name"`
// Probability is the predicate's probability that the condition holds.
Probability *float64 `json:"probability,omitempty"`
// Choice is the selected option of a choice question.
Choice string `json:"choice,omitempty"`
// Score is the probability-weighted average level index of a score question.
Score *float64 `json:"score,omitempty"`
Probabilities []AnswerProbability `json:"probabilities,omitempty"`
Confidence *float64 `json:"confidence,omitempty"`
}
Answer is one OpenAI-format answer. Which fields are set depends on Type.
type AnswerProbability ¶
type AnswerProbability struct {
Value any `json:"value"`
Label string `json:"label,omitempty"`
Probability float64 `json:"probability"`
}
AnswerProbability is one entry of a choice or score distribution. For a choice Value is the option string; for a score it is the level index and Label carries the level's label.
type Choice ¶
type Choice struct {
Value string `json:"value"`
Description string `json:"description,omitempty"`
}
Choice is one option of a choice question.
type Format ¶
type Format string
Format names the wire format of a decision request or response body.
func DetectResponseFormat ¶
DetectResponseFormat reports the wire format of a decision response body from the shape of its answers field (an array for OpenAI, an object for System One).
func FormatForPath ¶
FormatForPath reports which decision wire format an upstream path speaks.
type InputTokensDetails ¶
type InputTokensDetails struct {
CachedTokens int `json:"cached_tokens"`
CacheWriteTokens int `json:"cache_write_tokens"`
}
InputTokensDetails is the cache share of a call's input tokens.
type NamedQuestion ¶
type NamedQuestion struct {
Name string
Question SystemOneQuestion
}
NamedQuestion is a System One question with its map key.
type OrderedQuestions ¶
type OrderedQuestions []NamedQuestion
OrderedQuestions is the System One questions map in wire order.
type Question ¶
type Question struct {
Type string `json:"type"`
Name string `json:"name"`
Instructions string `json:"instructions,omitempty"`
Choices []Choice `json:"choices,omitempty"`
// Levels are ordered lowest to highest.
Levels []Level `json:"levels,omitempty"`
}
Question is one OpenAI-format question.
type Request ¶
type Request struct {
Model string `json:"model"`
// Input is the shared evidence: a JSON string, or an array of user
// messages whose content is a string or input_text/input_image parts.
Input json.RawMessage `json:"input,omitempty"`
Questions []Question `json:"questions"`
}
Request is an OpenAI-format decision request.
func ParseRequest ¶
ParseRequest parses a request body in the given format into the OpenAI shape.
func ToOpenAIRequest ¶
func ToOpenAIRequest(so *SystemOneRequest) *Request
ToOpenAIRequest converts a System One request into the OpenAI shape, so plugins and filters can reason about one schema. State becomes a string input (objects and arrays are carried as compact JSON text).
type Response ¶
type Response struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Model string `json:"model,omitempty"`
Answers []Answer `json:"answers"`
Usage *Usage `json:"usage,omitempty"`
}
Response is an OpenAI-format decision response.
func FromSystemOne ¶
func FromSystemOne(so *SystemOneResponse, req *Request) *Response
FromSystemOne translates a System One response into an OpenAI-format response, returning answers in the order of req's questions. A question with no answer, or an answer of an unexpected type, becomes a refusal.
type Summary ¶
type Summary struct {
Questions int
Answers int
Refusals int
// ByType counts answered questions per OpenAI answer type.
ByType map[string]int
}
Summary counts what a decision exchange asked and answered.
type SystemOneAnswer ¶
type SystemOneAnswer struct {
Type string `json:"type"`
Noul *float64 `json:"noul,omitempty"`
Choice string `json:"choice,omitempty"`
Score *float64 `json:"score,omitempty"`
Legend json.RawMessage `json:"legend,omitempty"`
Probabilities json.RawMessage `json:"probabilities,omitempty"`
Confidence *float64 `json:"confidence,omitempty"`
}
SystemOneAnswer is one System One answer. Probabilities is an option->probability object for choice, and an array or index-keyed object for score.
type SystemOneQuestion ¶
type SystemOneQuestion struct {
Type string `json:"type"`
Instructions json.RawMessage `json:"instructions,omitempty"`
Criteria json.RawMessage `json:"criteria,omitempty"`
}
SystemOneQuestion is one System One question. Instructions may be a string, an object or an array; Criteria depends on Type (an option->description object for choice, an array of levels for score, optional for noul).
type SystemOneRequest ¶
type SystemOneRequest struct {
Model string
State json.RawMessage
Questions OrderedQuestions
}
SystemOneRequest is a System One decision request. Questions keep their insertion order through OrderedQuestions: System One models are sensitive to option order, and a Go map would sort it.
func ToSystemOne ¶
func ToSystemOne(req *Request) (*SystemOneRequest, error)
ToSystemOne translates an OpenAI-format request into a System One request. Question order and option order are preserved.
func (SystemOneRequest) MarshalJSON ¶
func (r SystemOneRequest) MarshalJSON() ([]byte, error)
MarshalJSON writes the request with questions as an ordered object.
func (*SystemOneRequest) UnmarshalJSON ¶
func (r *SystemOneRequest) UnmarshalJSON(data []byte) error
UnmarshalJSON reads a System One request, keeping question order.
type SystemOneResponse ¶
type SystemOneResponse struct {
Model string `json:"model,omitempty"`
Answers map[string]SystemOneAnswer `json:"answers"`
Usage *Usage `json:"usage,omitempty"`
}
SystemOneResponse is a System One decision response.
type Usage ¶
type Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
TotalTokens int `json:"total_tokens,omitempty"`
// InputTokensDetails is OpenAI's prompt-cache breakdown (seen on the live
// API: cached_tokens and cache_write_tokens, both included in
// InputTokens). System One reports none.
InputTokensDetails *InputTokensDetails `json:"input_tokens_details,omitempty"`
}
Usage is the token usage of a decision call. It accepts the input_tokens/output_tokens spelling (System One, OpenAI Responses-style) and the prompt_tokens/completion_tokens spelling (OpenAI chat-style).
func UsageFromBody ¶
UsageFromBody reads the usage block of a decision response in either format.
func (*Usage) UnmarshalJSON ¶
UnmarshalJSON accepts either usage spelling.
type ValidationError ¶
ValidationError is a client error in a decision request. Param names the offending field in the OpenAI error envelope.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string