openai

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
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.

View Source
const SystemFingerprint = "fp_switchboard"

Variables

This section is empty.

Functions

func ChainHash

func ChainHash(prev string, m Message) string

ChainHash extends a running hash with one more message.

func CompletionID

func CompletionID(requestID string) string

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

func EstimateTokens(s string) int

EstimateTokens approximates a token count from text length. It is marked as an estimate everywhere it is displayed.

func FirstUserText

func FirstUserText(msgs []Message) string

FirstUserText returns the first user message's text (for titles).

func Normalize

func Normalize(m Message) string

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

func PrefixHashes(msgs []Message) []string

PrefixHashes returns chain hashes for every prefix of msgs: hashes[k] covers msgs[:k]; hashes[0] is the empty prefix.

func PromptChars

func PromptChars(msgs []Message) int

PromptChars counts the characters in all message text.

func RootHash

func RootHash(apiKeyID string, msgs []Message) string

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.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON writes v as JSON with status 200.

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

func (a Answer) AssistantMessage() Message

AssistantMessage renders the answer as the assistant message a client will append to its history, for chain hashing.

func (Answer) Normalized

func (a Answer) Normalized(newID func() string) Answer

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.

func BuildChunk

func BuildChunk(requestID, model string, created time.Time, delta Delta, finish *string, usage *Usage) ChatChunk

BuildChunk wraps a delta in the streaming envelope.

func UsageOnlyChunk

func UsageOnlyChunk(requestID, model string, created time.Time, usage Usage) ChatChunk

UsageOnlyChunk is the trailing chunk sent when stream_options.include_usage is set: no choices, just usage.

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

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

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

func LastMessage(msgs []Message) Message

LastMessage returns the final message, or a zero Message.

func (Message) Text

func (m Message) Text() string

Text flattens message content into plain text. Non-text parts are represented by a bracketed placeholder so they still influence hashing.

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 ModelList

type ModelList struct {
	Object string  `json:"object"`
	Data   []Model `json:"data"`
}

ModelList is 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.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage is the token accounting block.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL