137 lines
4.1 KiB
Go
137 lines
4.1 KiB
Go
// Package chatcmpl describes the OpenAI-compatible Chat Completions
|
|
// protocol spoken by upstream providers.
|
|
package chatcmpl
|
|
|
|
import "encoding/json"
|
|
|
|
// Request is a Chat Completions request body.
|
|
type Request struct {
|
|
Model string `json:"model"`
|
|
Messages []Message `json:"messages"`
|
|
MaxTokens int `json:"max_tokens,omitempty"`
|
|
Temperature *float64 `json:"temperature,omitempty"`
|
|
TopP *float64 `json:"top_p,omitempty"`
|
|
Stop []string `json:"stop,omitempty"`
|
|
Stream bool `json:"stream,omitempty"`
|
|
StreamOptions *StreamOptions `json:"stream_options,omitempty"`
|
|
Tools []Tool `json:"tools,omitempty"`
|
|
ToolChoice any `json:"tool_choice,omitempty"`
|
|
}
|
|
|
|
// StreamOptions toggles the usage chunk at the end of a stream.
|
|
type StreamOptions struct {
|
|
IncludeUsage bool `json:"include_usage"`
|
|
}
|
|
|
|
// Message is one Chat Completions message. Content is either a string or
|
|
// a []ContentPart.
|
|
type Message struct {
|
|
Role string `json:"role"`
|
|
Content any `json:"content"`
|
|
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
|
ToolCallID string `json:"tool_call_id,omitempty"`
|
|
}
|
|
|
|
// ContentPart is one part of a multi-modal message.
|
|
type ContentPart struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text,omitempty"`
|
|
ImageURL *ImageURL `json:"image_url,omitempty"`
|
|
}
|
|
|
|
// ImageURL references an image, usually as a data URL.
|
|
type ImageURL struct {
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// Tool is a function tool definition.
|
|
type Tool struct {
|
|
Type string `json:"type"`
|
|
Function ToolFunction `json:"function"`
|
|
}
|
|
|
|
// ToolFunction is the payload of a function tool.
|
|
type ToolFunction struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description,omitempty"`
|
|
Parameters json.RawMessage `json:"parameters,omitempty"`
|
|
}
|
|
|
|
// ToolCall is a function call requested by the model.
|
|
type ToolCall struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Function ToolCallFunction `json:"function"`
|
|
Index int `json:"index,omitempty"`
|
|
}
|
|
|
|
// ToolCallFunction holds the name and the accumulated JSON arguments.
|
|
type ToolCallFunction struct {
|
|
Name string `json:"name"`
|
|
Arguments string `json:"arguments"`
|
|
}
|
|
|
|
// Response is a non-streaming Chat Completions response body.
|
|
type Response struct {
|
|
ID string `json:"id"`
|
|
Object string `json:"object"`
|
|
Model string `json:"model"`
|
|
Choices []Choice `json:"choices"`
|
|
Usage *Usage `json:"usage,omitempty"`
|
|
Error *Error `json:"error,omitempty"`
|
|
}
|
|
|
|
// Choice is one completion choice.
|
|
type Choice struct {
|
|
Index int `json:"index"`
|
|
Message Message `json:"message"`
|
|
FinishReason string `json:"finish_reason"`
|
|
}
|
|
|
|
// Usage reports token counts.
|
|
type Usage struct {
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
}
|
|
|
|
// Error is an upstream error payload.
|
|
type Error struct {
|
|
Message string `json:"message"`
|
|
Type string `json:"type"`
|
|
Code any `json:"code,omitempty"`
|
|
}
|
|
|
|
// ModelInfo is one entry of an upstream model catalog.
|
|
type ModelInfo struct {
|
|
ID string `json:"id"`
|
|
Object string `json:"object,omitempty"`
|
|
OwnedBy string `json:"owned_by,omitempty"`
|
|
}
|
|
|
|
// ModelList is the upstream /models response body.
|
|
type ModelList struct {
|
|
Data []ModelInfo `json:"data"`
|
|
}
|
|
|
|
// StreamChunk is one SSE data payload of a streaming response.
|
|
type StreamChunk struct {
|
|
ID string `json:"id"`
|
|
Choices []ChunkChoice `json:"choices"`
|
|
Usage *Usage `json:"usage,omitempty"`
|
|
}
|
|
|
|
// ChunkChoice is one choice inside a stream chunk.
|
|
type ChunkChoice struct {
|
|
Index int `json:"index"`
|
|
Delta Delta `json:"delta"`
|
|
FinishReason *string `json:"finish_reason"`
|
|
}
|
|
|
|
// Delta is the incremental payload of a stream chunk.
|
|
type Delta struct {
|
|
Role string `json:"role,omitempty"`
|
|
Content *string `json:"content,omitempty"`
|
|
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
|
}
|