openai

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package openai adapts the OpenAI HTTP protocol to canonical inference values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeChat

func DecodeChat(reader io.Reader) (inference.ChatRequest, error)

DecodeChat decodes one strict OpenAI request into canonical inference.

func DecodeEmbedding

func DecodeEmbedding(reader io.Reader) (inference.EmbeddingRequest, error)

DecodeEmbedding decodes one strict OpenAI embeddings request.

func WriteError

func WriteError(w http.ResponseWriter, status int, errorType, message string, param *string)

WriteError writes one OpenAI error response.

func WriteJSON

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

WriteJSON writes one OpenAI JSON value.

Types

type ChatRequest

type ChatRequest struct {
	Model               string            `json:"model"`
	Messages            []Message         `json:"messages"`
	Temperature         *float32          `json:"temperature,omitempty"`
	TopP                *float32          `json:"top_p,omitempty"`
	N                   *int              `json:"n,omitempty"`
	Stream              bool              `json:"stream,omitempty"`
	StreamOptions       *StreamOptions    `json:"stream_options,omitempty"`
	Stop                json.RawMessage   `json:"stop,omitempty"`
	MaxTokens           *int              `json:"max_tokens,omitempty"`
	MaxCompletionTokens *int              `json:"max_completion_tokens,omitempty"`
	PresencePenalty     *float32          `json:"presence_penalty,omitempty"`
	FrequencyPenalty    *float32          `json:"frequency_penalty,omitempty"`
	LogitBias           map[string]int    `json:"logit_bias,omitempty"`
	LogProbs            *bool             `json:"logprobs,omitempty"`
	TopLogProbs         *int              `json:"top_logprobs,omitempty"`
	User                string            `json:"user,omitempty"`
	Seed                *int              `json:"seed,omitempty"`
	Tools               []Tool            `json:"tools,omitempty"`
	ToolChoice          json.RawMessage   `json:"tool_choice,omitempty"`
	ResponseFormat      *ResponseFormat   `json:"response_format,omitempty"`
	ReasoningEffort     string            `json:"reasoning_effort,omitempty"`
	ParallelToolCalls   *bool             `json:"parallel_tool_calls,omitempty"`
	Store               *bool             `json:"store,omitempty"`
	Metadata            map[string]string `json:"metadata,omitempty"`
	ServiceTier         string            `json:"service_tier,omitempty"`
}

ChatRequest is the OpenAI chat-completions wire request.

type ChatResponse

type ChatResponse struct {
	ID                string   `json:"id"`
	Object            string   `json:"object"`
	Created           int64    `json:"created"`
	Model             string   `json:"model"`
	Choices           []Choice `json:"choices"`
	Usage             Usage    `json:"usage"`
	SystemFingerprint string   `json:"system_fingerprint,omitempty"`
	ServiceTier       string   `json:"service_tier,omitempty"`
}

ChatResponse is the OpenAI chat-completions wire response.

func EncodeChat

func EncodeChat(response inference.ChatResponse) ChatResponse

EncodeChat converts one canonical chat result to OpenAI wire values.

type Choice

type Choice struct {
	Index        int             `json:"index"`
	Message      ResponseMessage `json:"message"`
	FinishReason string          `json:"finish_reason"`
	LogProbs     *LogProbs       `json:"logprobs,omitempty"`
}

Choice is one OpenAI chat-completions result.

type CompletionTokenDetails

type CompletionTokenDetails struct {
	ReasoningTokens int `json:"reasoning_tokens"`
}

CompletionTokenDetails contains OpenAI reasoning-token accounting.

type ContentPart

type ContentPart struct {
	Type     string    `json:"type"`
	Text     string    `json:"text,omitempty"`
	ImageURL *ImageURL `json:"image_url,omitempty"`
}

ContentPart is one OpenAI multipart message input.

type Embedding

type Embedding struct {
	Object    string    `json:"object"`
	Index     int       `json:"index"`
	Embedding []float32 `json:"embedding"`
}

Embedding is one OpenAI embedding vector.

type EmbeddingRequest

type EmbeddingRequest struct {
	Model          string          `json:"model"`
	Input          json.RawMessage `json:"input"`
	EncodingFormat string          `json:"encoding_format,omitempty"`
	Dimensions     *int            `json:"dimensions,omitempty"`
	User           string          `json:"user,omitempty"`
}

EmbeddingRequest is the OpenAI embeddings wire request.

type EmbeddingResponse

type EmbeddingResponse struct {
	Object string      `json:"object"`
	Data   []Embedding `json:"data"`
	Model  string      `json:"model"`
	Usage  Usage       `json:"usage"`
}

EmbeddingResponse is the OpenAI embeddings wire response.

func EncodeEmbedding

func EncodeEmbedding(response inference.EmbeddingResponse) EmbeddingResponse

EncodeEmbedding converts one canonical embedding result to OpenAI wire values.

type ErrorDetail

type ErrorDetail struct {
	Message string  `json:"message"`
	Type    string  `json:"type"`
	Param   *string `json:"param,omitempty"`
	Code    string  `json:"code,omitempty"`
}

ErrorDetail is one OpenAI API error.

type ErrorResponse

type ErrorResponse struct {
	Error ErrorDetail `json:"error"`
}

ErrorResponse is the OpenAI error envelope.

type Function

type Function struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Parameters  json.RawMessage `json:"parameters"`
}

Function defines an OpenAI function tool.

type FunctionCall

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

FunctionCall is one requested function invocation.

type ImageURL

type ImageURL struct {
	URL    string `json:"url"`
	Detail string `json:"detail,omitempty"`
}

ImageURL is an OpenAI image input.

type JSONSchema

type JSONSchema struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Schema      json.RawMessage `json:"schema"`
	Strict      bool            `json:"strict,omitempty"`
}

JSONSchema describes one structured output.

type LogProb

type LogProb struct {
	Token       string       `json:"token"`
	LogProb     float64      `json:"logprob"`
	Bytes       []int        `json:"bytes,omitempty"`
	TopLogProbs []TopLogProb `json:"top_logprobs"`
}

LogProb is one output-token probability.

type LogProbs

type LogProbs struct {
	Content []LogProb `json:"content"`
}

LogProbs contains output-token log probabilities.

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 one OpenAI chat message.

type MessageDelta

type MessageDelta struct {
	Role      string     `json:"role,omitempty"`
	Content   string     `json:"content,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}

MessageDelta is one streamed OpenAI message update.

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 OpenAI model resource.

type ModelList

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

ModelList is the OpenAI model-list response.

type ResponseFormat

type ResponseFormat struct {
	Type       string      `json:"type"`
	JSONSchema *JSONSchema `json:"json_schema,omitempty"`
}

ResponseFormat is the OpenAI response-format contract.

type ResponseMessage

type ResponseMessage struct {
	Role      string     `json:"role"`
	Content   string     `json:"content"`
	Refusal   *string    `json:"refusal,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}

ResponseMessage is one OpenAI assistant message.

type StreamChoice

type StreamChoice struct {
	Index        int          `json:"index"`
	Delta        MessageDelta `json:"delta"`
	FinishReason *string      `json:"finish_reason"`
	LogProbs     *LogProbs    `json:"logprobs,omitempty"`
}

StreamChoice is one streamed OpenAI choice.

type StreamChunk

type StreamChunk struct {
	ID                string         `json:"id"`
	Object            string         `json:"object"`
	Created           int64          `json:"created"`
	Model             string         `json:"model"`
	Choices           []StreamChoice `json:"choices"`
	Usage             *Usage         `json:"usage,omitempty"`
	SystemFingerprint string         `json:"system_fingerprint,omitempty"`
}

StreamChunk is one OpenAI SSE data value.

func EncodeStream

func EncodeStream(event inference.StreamEvent) StreamChunk

EncodeStream converts one canonical stream event to an OpenAI chunk.

type StreamOptions

type StreamOptions struct {
	IncludeUsage bool `json:"include_usage,omitempty"`
}

StreamOptions controls OpenAI stream usage events.

type Tool

type Tool struct {
	Type     string   `json:"type"`
	Function Function `json:"function"`
}

Tool is an OpenAI function tool.

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function FunctionCall `json:"function"`
}

ToolCall is an OpenAI function call.

type TopLogProb

type TopLogProb struct {
	Token   string  `json:"token"`
	LogProb float64 `json:"logprob"`
	Bytes   []int   `json:"bytes,omitempty"`
}

TopLogProb is one alternate output token.

type Usage

type Usage struct {
	PromptTokens           int                     `json:"prompt_tokens"`
	CompletionTokens       int                     `json:"completion_tokens"`
	TotalTokens            int                     `json:"total_tokens"`
	CompletionTokenDetails *CompletionTokenDetails `json:"completion_tokens_details,omitempty"`
}

Usage is OpenAI token accounting.

Jump to

Keyboard shortcuts

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