openaicompat

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseSSEStream

func ParseSSEStream(reader io.Reader) <-chan StreamChunk

ParseSSEStream parses Server-Sent Events stream and returns a channel of StreamChunks

func ResponseFromWire

func ResponseFromWire(resp *ChatCompletionResponse) *core.Response

ResponseFromWire converts OpenAI wire response to core.Response

func StreamChunkToEvent

func StreamChunkToEvent(chunk StreamChunk) core.StreamEvent

StreamChunkToEvent converts a StreamChunk to core.StreamEvent

Types

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model           string    `json:"model"`
	Messages        []Message `json:"messages"`
	Temperature     float64   `json:"temperature,omitempty"`
	MaxTokens       int       `json:"max_tokens,omitempty"`
	TopP            float64   `json:"top_p,omitempty"`
	Stop            []string  `json:"stop,omitempty"`
	Stream          bool      `json:"stream,omitempty"`
	ReasoningEffort string    `json:"reasoning_effort,omitempty"` // for OpenAI o1/o3
	EnableSearch    bool      `json:"enable_search,omitempty"`
	Tools           []Tool    `json:"tools,omitempty"`
}

ChatCompletionRequest represents the OpenAI-compatible chat completion request

type ChatCompletionResponse

type ChatCompletionResponse 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"`
}

ChatCompletionResponse represents the OpenAI-compatible chat completion response

type Choice

type Choice struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	FinishReason string  `json:"finish_reason"`
}

Choice represents a choice in the response

type ContentPart

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

ContentPart represents a part of multimodal content

type Delta

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

Delta represents incremental content in streaming

type ErrorDetail

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

ErrorDetail represents error details

type ErrorResponse

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

ErrorResponse represents an error response

type Function

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

Function represents a function definition

type ImageURL

type ImageURL struct {
	URL string `json:"url"` // Can be URL or data:image/png;base64,...
}

ImageURL represents an image URL or data

type Message

type Message struct {
	Role             string      `json:"role"`
	Content          interface{} `json:"content"` // string or []ContentPart
	ReasoningContent string      `json:"reasoning_content,omitempty"`
	ToolCalls        []ToolCall  `json:"tool_calls,omitempty"`
	ToolCallID       string      `json:"tool_call_id,omitempty"`
}

Message represents a message in the OpenAI wire format

func MessagesToWire

func MessagesToWire(messages []core.Message, systemPrompt string) []Message

MessagesToWire converts core.Message slice to OpenAI wire format

type StreamChoice

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

StreamChoice represents a choice in the streaming response

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

StreamChunk represents a chunk in the streaming response

type Tool

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

Tool represents a tool definition

func ToolsToWire

func ToolsToWire(tools []core.Tool) []Tool

ToolsToWire converts core.Tool slice to OpenAI wire format

type ToolCall

type ToolCall struct {
	ID       string `json:"id"`
	Type     string `json:"type"` // "function"
	Function struct {
		Name      string `json:"name"`
		Arguments string `json:"arguments"`
	} `json:"function"`
}

ToolCall represents a tool call in the response

type Usage

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

Usage represents token usage

Jump to

Keyboard shortcuts

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