Documentation
¶
Overview ¶
Package openai holds the OpenAI-compatible wire types, parsing and response builders, plus the message normalization used to reconstruct conversations across requests.
Index ¶
- Constants
- func ChainHash(prev string, m Message) string
- func CompletionID(requestID string) string
- func EstimatePromptTokens(r *ChatRequest) int
- func EstimateTokens(s string) int
- func FirstUserText(msgs []Message) string
- func Normalize(m Message) string
- func PrefixHashes(msgs []Message) []string
- func PromptChars(msgs []Message) int
- func RootHash(apiKeyID string, msgs []Message) string
- func WriteError(w http.ResponseWriter, status int, typ, code, msg string)
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type Answer
- type ChatChoice
- type ChatChunk
- type ChatCompletion
- type ChatRequest
- type ChunkChoice
- type CompletionRequest
- type Delta
- type ErrorBody
- type ErrorResponse
- type FunctionCall
- type FunctionCallDelta
- type Message
- type Model
- type ModelList
- type ResponseMessage
- type StreamOptions
- type TextChoice
- type TextCompletion
- type ToolCall
- type ToolCallDelta
- type Usage
Constants ¶
const ( ErrTypeInvalidRequest = "invalid_request_error" ErrTypeAuthentication = "authentication_error" ErrTypePermission = "permission_error" ErrTypeRateLimit = "rate_limit_error" ErrTypeServer = "server_error" ErrTypeNotFound = "not_found_error" )
Error type strings used by OpenAI clients to classify failures.
const SystemFingerprint = "fp_switchboard"
Variables ¶
This section is empty.
Functions ¶
func CompletionID ¶
CompletionID derives the public chatcmpl id from an internal request id.
func EstimatePromptTokens ¶
func EstimatePromptTokens(r *ChatRequest) int
EstimatePromptTokens approximates the prompt size of a chat request.
func EstimateTokens ¶
EstimateTokens approximates a token count from text length. It is marked as an estimate everywhere it is displayed.
func FirstUserText ¶
FirstUserText returns the first user message's text (for titles).
func Normalize ¶
Normalize renders a message into a canonical string: role, trimmed text, tool call names+arguments. IDs are excluded so client-side re-serialization does not break chain matching.
func PrefixHashes ¶
PrefixHashes returns chain hashes for every prefix of msgs: hashes[k] covers msgs[:k]; hashes[0] is the empty prefix.
func PromptChars ¶
PromptChars counts the characters in all message text.
func RootHash ¶
RootHash identifies the start of a conversation: the leading system/ developer messages plus the first user message, scoped to an API key.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, typ, code, msg string)
WriteError writes an OpenAI-format error with the given HTTP status.
Types ¶
type Answer ¶
type Answer struct {
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
Refusal string `json:"refusal,omitempty"`
Usage *Usage `json:"usage,omitempty"`
}
Answer is the agent's response to a request before it is wrapped in the OpenAI envelope.
func (Answer) AssistantMessage ¶
AssistantMessage renders the answer as the assistant message a client will append to its history, for chain hashing.
func (Answer) Normalized ¶
Normalized fills defaults: tool call ids/types and finish reason.
type ChatChoice ¶
type ChatChoice struct {
Index int `json:"index"`
Message ResponseMessage `json:"message"`
Logprobs any `json:"logprobs"`
FinishReason string `json:"finish_reason"`
}
ChatChoice is one completion choice.
type ChatChunk ¶
type ChatChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChunkChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
SystemFingerprint string `json:"system_fingerprint"`
}
ChatChunk is a streaming response chunk.
type ChatCompletion ¶
type ChatCompletion struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChatChoice `json:"choices"`
Usage Usage `json:"usage"`
SystemFingerprint string `json:"system_fingerprint"`
}
ChatCompletion is the non-streaming response envelope.
func BuildChatCompletion ¶
func BuildChatCompletion(requestID, model string, created time.Time, a Answer, usage Usage) ChatCompletion
BuildChatCompletion wraps an answer in the non-streaming envelope.
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream,omitempty"`
StreamOptions *StreamOptions `json:"stream_options,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
MaxCompletionTokens *int `json:"max_completion_tokens,omitempty"`
N *int `json:"n,omitempty"`
Stop json.RawMessage `json:"stop,omitempty"`
Tools json.RawMessage `json:"tools,omitempty"`
ToolChoice json.RawMessage `json:"tool_choice,omitempty"`
ResponseFormat json.RawMessage `json:"response_format,omitempty"`
User string `json:"user,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Raw map[string]json.RawMessage `json:"-"`
}
ChatRequest is POST /v1/chat/completions. Unknown fields are preserved in Raw so they can be forwarded to the serving agent untouched.
func ParseChatRequest ¶
func ParseChatRequest(body []byte) (*ChatRequest, error)
ParseChatRequest decodes and validates a chat completion body.
func (*ChatRequest) Params ¶
func (r *ChatRequest) Params() map[string]json.RawMessage
Params returns the generation parameters (everything except the routing and transport fields) for forwarding to the serving agent.
type ChunkChoice ¶
type ChunkChoice struct {
Index int `json:"index"`
Delta Delta `json:"delta"`
Logprobs any `json:"logprobs"`
FinishReason *string `json:"finish_reason"`
}
ChunkChoice is one choice in a streaming chunk.
type CompletionRequest ¶
type CompletionRequest struct {
Model string `json:"model"`
Prompt json.RawMessage `json:"prompt"`
Suffix string `json:"suffix,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
Stop json.RawMessage `json:"stop,omitempty"`
User string `json:"user,omitempty"`
Raw map[string]json.RawMessage `json:"-"`
}
CompletionRequest is the legacy POST /v1/completions body.
func ParseCompletionRequest ¶
func ParseCompletionRequest(body []byte) (*CompletionRequest, error)
ParseCompletionRequest decodes a legacy completion body.
func (*CompletionRequest) PromptText ¶
func (r *CompletionRequest) PromptText() (string, error)
PromptText flattens prompt (string or array of strings) into text.
func (*CompletionRequest) ToChat ¶
func (r *CompletionRequest) ToChat() *ChatRequest
ToChat converts a legacy completion into a chat request.
type Delta ¶
type Delta struct {
Role string `json:"role,omitempty"`
Content *string `json:"content,omitempty"`
ToolCalls []ToolCallDelta `json:"tool_calls,omitempty"`
Refusal *string `json:"refusal,omitempty"`
}
Delta is the incremental payload in a streaming chunk.
type ErrorBody ¶
type ErrorBody struct {
Message string `json:"message"`
Type string `json:"type"`
Param *string `json:"param"`
Code *string `json:"code"`
}
ErrorBody is the inner error object.
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorBody `json:"error"`
}
ErrorResponse is the OpenAI error envelope.
type FunctionCall ¶
FunctionCall names a function and carries its JSON-encoded arguments.
type FunctionCallDelta ¶
type FunctionCallDelta struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments"`
}
FunctionCallDelta is a streamed function fragment.
type Message ¶
type Message struct {
Role string `json:"role"`
Content json.RawMessage `json:"content,omitempty"`
Name string `json:"name,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Refusal *string `json:"refusal,omitempty"`
}
Message is a chat message as sent by callers. Content is kept raw because it may be a string or an array of content parts.
func LastMessage ¶
LastMessage returns the final message, or a zero Message.
type Model ¶
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
Model is one entry in GET /v1/models.
type ResponseMessage ¶
type ResponseMessage struct {
Role string `json:"role"`
Content *string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
Refusal *string `json:"refusal"`
}
ResponseMessage is the assistant message inside a completion.
type StreamOptions ¶
type StreamOptions struct {
IncludeUsage bool `json:"include_usage,omitempty"`
}
StreamOptions mirrors OpenAI's stream_options object.
type TextChoice ¶
type TextChoice struct {
Text string `json:"text"`
Index int `json:"index"`
Logprobs any `json:"logprobs"`
FinishReason string `json:"finish_reason"`
}
TextChoice is a legacy completion choice.
type TextCompletion ¶
type TextCompletion struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []TextChoice `json:"choices"`
Usage Usage `json:"usage"`
}
TextCompletion is the legacy /v1/completions envelope.
func BuildTextCompletion ¶
func BuildTextCompletion(requestID, model string, created time.Time, a Answer, usage Usage) TextCompletion
BuildTextCompletion wraps an answer in the legacy envelope.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function FunctionCall `json:"function"`
}
ToolCall is a function tool invocation emitted by the assistant.
type ToolCallDelta ¶
type ToolCallDelta struct {
Index int `json:"index"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Function *FunctionCallDelta `json:"function,omitempty"`
}
ToolCallDelta is a streamed tool call fragment.
func ToolCallsAsDeltas ¶
func ToolCallsAsDeltas(calls []ToolCall, startIndex int) []ToolCallDelta
ToolCallsAsDeltas converts complete tool calls into streaming fragments.