openai

package
v1.1.0 Latest Latest
Warning

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

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

Documentation

Overview

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

Index

Constants

View Source
const (
	// StoredFileObject is the literal every file object carries.
	StoredFileObject = "file"
	// ListObject is the literal every list envelope carries. A stored file
	// page, an embedding set, a video job page, and a rerank answer all state it.
	ListObject = "list"
)
View Source
const (
	// StoredFileStatusUploaded names a file whose bytes have not committed.
	StoredFileStatusUploaded = "uploaded"
	// StoredFileStatusProcessed names a readable file.
	StoredFileStatusProcessed = "processed"
)

The status field. Upstream marks it deprecated and Starport still serves it, because a strict SDK decode reads it and Starport holds a real two-state record behind it. A record whose bytes have not finished landing reads as uploaded, and a readable one reads as processed.

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 DecodeImages added in v1.1.0

func DecodeImages(reader io.Reader) (inference.ImagesRequest, error)

DecodeImages decodes one strict OpenAI image generation request.

func DecodeImagesForm added in v1.1.0

func DecodeImagesForm(form *multipart.Form) (inference.ImagesRequest, error)

DecodeImagesForm decodes one OpenAI image edit request from multipart form data.

func DecodeSpeech added in v1.1.0

func DecodeSpeech(reader io.Reader) (inference.SpeechRequest, error)

DecodeSpeech decodes one strict OpenAI text-to-speech request.

func DecodeTranscriptionForm added in v1.1.0

func DecodeTranscriptionForm(form *multipart.Form, translate bool) (inference.TranscriptionRequest, error)

DecodeTranscriptionForm decodes one OpenAI speech-to-text request from multipart form data. translate states whether the caller reached the translation path.

func DecodeVideoJob added in v1.1.0

func DecodeVideoJob(reader io.Reader) (inference.VideoJobRequest, error)

DecodeVideoJob decodes one strict OpenAI video generation 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 AudioConfig added in v1.1.0

type AudioConfig struct {
	Voice  string `json:"voice,omitempty"`
	Format string `json:"format,omitempty"`
}

AudioConfig selects the voice and container for a spoken answer. A provider that serves audio requires both, and neither carries a gateway default.

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"`

	// Modalities names what the caller will accept back. Absent means text,
	// which is what every model served before this field existed.
	Modalities []string `json:"modalities,omitempty"`
	// Audio configures the spoken answer that Modalities asks for.
	Audio *AudioConfig `json:"audio,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"`
	// AudioTokens counts the audio share of CompletionTokens.
	AudioTokens int `json:"audio_tokens,omitempty"`
}

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"`
	InputAudio *InputAudio `json:"input_audio,omitempty"`
	File       *File       `json:"file,omitempty"`
	VideoURL   *VideoURL   `json:"video_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 File added in v1.1.0

type File struct {
	Filename string `json:"filename,omitempty"`
	FileData string `json:"file_data,omitempty"`
	FileID   string `json:"file_id,omitempty"`
}

File is a document input. FileData holds a data URL, and FileID names a document this gateway stores for the requesting account.

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 GeneratedAudio added in v1.1.0

type GeneratedAudio struct {
	Data       string `json:"data,omitempty"`
	Transcript string `json:"transcript,omitempty"`
	Format     string `json:"format,omitempty"`
}

GeneratedAudio is a spoken answer. Data is raw base64 with no data URL prefix, matching the audio input shape on the same wire.

type GeneratedImage added in v1.1.0

type GeneratedImage struct {
	Type     string    `json:"type"`
	ImageURL *ImageURL `json:"image_url,omitempty"`
	Index    int       `json:"index,omitempty"`
}

GeneratedImage is one picture a model produced. It repeats the input part shape, so a caller sends back what it received without translating.

type ImageDatum added in v1.1.0

type ImageDatum struct {
	B64JSON       string `json:"b64_json,omitempty"`
	URL           string `json:"url,omitempty"`
	RevisedPrompt string `json:"revised_prompt,omitempty"`
}

ImageDatum is one generated image, inline or by reference.

type ImageURL

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

ImageURL is an OpenAI image input.

type ImagesRequest added in v1.1.0

type ImagesRequest struct {
	Model          string `json:"model"`
	Prompt         string `json:"prompt"`
	N              int    `json:"n,omitempty"`
	Size           string `json:"size,omitempty"`
	Quality        string `json:"quality,omitempty"`
	Style          string `json:"style,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

ImagesRequest is the OpenAI image generation wire request.

type ImagesResponse added in v1.1.0

type ImagesResponse struct {
	Created int64        `json:"created"`
	Data    []ImageDatum `json:"data"`
	Usage   *Usage       `json:"usage,omitempty"`
}

ImagesResponse is the OpenAI image wire response.

func EncodeImages added in v1.1.0

func EncodeImages(response inference.ImagesResponse) ImagesResponse

EncodeImages converts one canonical image result to OpenAI wire values.

type InputAudio added in v1.1.0

type InputAudio struct {
	Data   string `json:"data"`
	Format string `json:"format,omitempty"`
}

InputAudio is an audio input. Data is raw base64 with no data URL prefix, which is why Format names the container beside it.

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"`
	// Images arrive whole in one delta, because a provider sends a finished
	// picture rather than a growing one.
	Images []GeneratedImage `json:"images,omitempty"`
	// Audio arrives in pieces, so each chunk holds its own base64 run and
	// the transcript text that goes with it.
	Audio *GeneratedAudio `json:"audio,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 PromptTokenDetails added in v1.1.0

type PromptTokenDetails struct {
	AudioTokens int `json:"audio_tokens,omitempty"`
}

PromptTokenDetails breaks the prompt total down. A caller that asked for a spoken turn reconciles its bill from the audio share, because a provider charges audio at a rate the plain input rate does not describe.

type RerankDecoding added in v1.1.0

type RerankDecoding struct {
	Request         inference.RerankRequest
	ReturnDocuments bool
}

RerankDecoding is one decoded rerank request with the wire-only options that change how its answer is written. Holding them apart is what keeps the canonical request free of a presentation flag that no router, cache, or usage record has any use for.

func DecodeRerank added in v1.1.0

func DecodeRerank(reader io.Reader) (RerankDecoding, error)

DecodeRerank decodes one strict rerank request. An unknown field fails the same way it fails on the chat route, because a caller that misspells a cost control has to hear about it rather than pay the default.

type RerankRequest added in v1.1.0

type RerankRequest struct {
	Model     string   `json:"model"`
	Query     string   `json:"query"`
	Documents []string `json:"documents"`
	// TopN asks for fewer results. It is not a page: a provider scores every
	// document either way, and it bills for every document either way.
	TopN *int `json:"top_n,omitempty"`
	// ReturnDocuments asks for the ranked text echoed on every result.
	ReturnDocuments bool `json:"return_documents,omitempty"`
	// MaxTokensPerDoc caps how much of each document the provider reads. Not
	// every provider has a wire name for it, and the transport refuses the
	// field rather than dropping it when it cannot say it.
	MaxTokensPerDoc *int `json:"max_tokens_per_doc,omitempty"`
}

RerankRequest is the rerank wire request served at POST /v1/rerank.

type RerankResponse added in v1.1.0

type RerankResponse struct {
	Object  string         `json:"object"`
	Model   string         `json:"model"`
	Results []RerankResult `json:"results"`
	Usage   RerankUsage    `json:"usage"`
}

RerankResponse is the rerank wire response.

func EncodeRerank added in v1.1.0

func EncodeRerank(
	response inference.RerankResponse,
	decoding RerankDecoding,
) (RerankResponse, error)

EncodeRerank writes one canonical rerank answer. It takes the decoding rather than the response alone: an echoed document comes from the request, which is the only copy of the text the gateway holds.

type RerankResult added in v1.1.0

type RerankResult struct {
	Index          int     `json:"index"`
	RelevanceScore float64 `json:"relevance_score"`
	Document       *string `json:"document,omitempty"`
}

RerankResult is one scored document on the wire. The document field is present only when the caller asked for it.

type RerankUsage added in v1.1.0

type RerankUsage struct {
	TotalTokens int `json:"total_tokens,omitempty"`
	SearchUnits int `json:"search_units,omitempty"`
}

RerankUsage reports what a rerank turn consumed. Providers split on the unit: one bills a search unit and another bills tokens, so the answer states whichever one the provider reported and omits the other.

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"`
	// Images carries generated pictures. The field name comes from the
	// shape the OpenAI-compatible providers that generate images already
	// serve, so a client that reads one of them reads this gateway.
	Images []GeneratedImage `json:"images,omitempty"`
	// Audio carries a spoken answer. Content keeps the transcript beside
	// it rather than going null, because a caller that cannot play audio
	// still has the answer.
	Audio *GeneratedAudio `json:"audio,omitempty"`
}

ResponseMessage is one OpenAI assistant message.

type SpeechRequest added in v1.1.0

type SpeechRequest struct {
	Model          string   `json:"model"`
	Input          string   `json:"input"`
	Voice          string   `json:"voice,omitempty"`
	ResponseFormat string   `json:"response_format,omitempty"`
	Speed          *float64 `json:"speed,omitempty"`
}

SpeechRequest is the OpenAI text-to-speech wire request.

type StoredFile added in v1.1.0

type StoredFile struct {
	ID        string `json:"id"`
	Object    string `json:"object"`
	Bytes     int64  `json:"bytes"`
	CreatedAt int64  `json:"created_at"`
	Filename  string `json:"filename"`
	Purpose   string `json:"purpose"`
	ExpiresAt int64  `json:"expires_at,omitempty"`
	Status    string `json:"status"`
}

StoredFile is one file object on the wire.

The status_details field is absent. Upstream fills it from fine-tune validation, Starport validates no fine-tune file, and a field carrying an invented value is worse than an absent one.

type StoredFileDeletion added in v1.1.0

type StoredFileDeletion struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Deleted bool   `json:"deleted"`
}

StoredFileDeletion is the answer to a delete.

func NewStoredFileDeletion added in v1.1.0

func NewStoredFileDeletion(id string) StoredFileDeletion

NewStoredFileDeletion names one deleted file.

type StoredFileList added in v1.1.0

type StoredFileList struct {
	Object  string       `json:"object"`
	Data    []StoredFile `json:"data"`
	FirstID string       `json:"first_id,omitempty"`
	LastID  string       `json:"last_id,omitempty"`
	HasMore bool         `json:"has_more"`
}

StoredFileList is the list envelope.

HasMore has no omitempty. A client reads the field to decide whether to page again, and an absent false would read the same as an absent envelope.

func NewStoredFileList added in v1.1.0

func NewStoredFileList(page []StoredFile, hasMore bool) StoredFileList

NewStoredFileList wraps one page and names its edges.

The cursor fields come from the page rather than from the caller, so a client that pages with last_id reads the same order the server returned.

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 TranscriptionResponse added in v1.1.0

type TranscriptionResponse struct {
	Text     string  `json:"text"`
	Language string  `json:"language,omitempty"`
	Duration float64 `json:"duration,omitempty"`
}

TranscriptionResponse is the OpenAI speech-to-text wire response.

func EncodeTranscription added in v1.1.0

func EncodeTranscription(response inference.TranscriptionResponse) TranscriptionResponse

EncodeTranscription converts one canonical transcript to OpenAI wire values.

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"`
	PromptTokenDetails     *PromptTokenDetails     `json:"prompt_tokens_details,omitempty"`
}

Usage is OpenAI token accounting.

type VideoJob added in v1.1.0

type VideoJob struct {
	ID          string         `json:"id"`
	Object      string         `json:"object"`
	Model       string         `json:"model"`
	Status      string         `json:"status"`
	CreatedAt   int64          `json:"created_at"`
	CompletedAt int64          `json:"completed_at,omitempty"`
	ExpiresAt   int64          `json:"expires_at,omitempty"`
	Error       *VideoJobError `json:"error,omitempty"`
}

VideoJob is the OpenAI video wire object.

func EncodeVideoJob added in v1.1.0

func EncodeVideoJob(job inference.VideoJob) VideoJob

EncodeVideoJob converts one canonical job to OpenAI wire values.

type VideoJobError added in v1.1.0

type VideoJobError struct {
	Message string `json:"message"`
}

VideoJobError states why a failed job produced no video.

type VideoJobList added in v1.1.0

type VideoJobList struct {
	Object string     `json:"object"`
	Data   []VideoJob `json:"data"`
}

VideoJobList is the OpenAI listing of one caller's video jobs.

func EncodeVideoJobs added in v1.1.0

func EncodeVideoJobs(records []inference.VideoJob) VideoJobList

EncodeVideoJobs converts one listing to OpenAI wire values.

type VideoJobRequest added in v1.1.0

type VideoJobRequest struct {
	Model          string `json:"model"`
	Prompt         string `json:"prompt"`
	NegativePrompt string `json:"negative_prompt,omitempty"`
	Size           string `json:"size,omitempty"`
	Seconds        string `json:"seconds,omitempty"`
	Seed           *int64 `json:"seed,omitempty"`
}

VideoJobRequest is the OpenAI video wire request.

type VideoURL added in v1.1.0

type VideoURL struct {
	URL string `json:"url"`
}

VideoURL is a video input.

Jump to

Keyboard shortcuts

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