Documentation
¶
Index ¶
- func ParseSSEStream(reader io.Reader) <-chan StreamChunk
- func ResponseFromWire(resp *ChatCompletionResponse) *core.Response
- func StreamChunkToEvent(chunk StreamChunk) core.StreamEvent
- type ChatCompletionRequest
- type ChatCompletionResponse
- type Choice
- type Client
- type CompletionTokensDetails
- type ContentPart
- type Delta
- type ErrorDetail
- type ErrorResponse
- type Function
- type ImageURL
- type Message
- type PromptTokensDetails
- type StreamChoice
- type StreamChunk
- type Tool
- type ToolCall
- type Usage
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"`
StreamOptions map[string]interface{} `json:"stream_options,omitempty"`
ReasoningEffort string `json:"reasoning_effort,omitempty"` // for OpenAI o1/o3
Tools []Tool `json:"tools,omitempty"`
TopK *int `json:"top_k,omitempty"`
PresencePenalty *float64 `json:"presence_penalty,omitempty"`
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
ResponseFormat interface{} `json:"response_format,omitempty"`
// ExtraBody for provider-specific parameters (e.g., Qwen)
ExtraBody map[string]interface{} `json:"extra_body,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 Client ¶
type Client struct {
*core.BaseClient
}
Client is an OpenAI LLM client.
It implements the core.Client interface and provides access to OpenAI's chat completion API, including GPT-3.5, GPT-4, and other models.
The client handles authentication, retries, error handling, and streaming automatically. It supports all OpenAI features including tool calling, multimodal inputs, and extended thinking (for o1/o3 models).
IMPORTANT FOR ALIBABA CLOUD QWEN USERS: This client uses OpenAI compatibility mode by default (dashscope.aliyuncs.com/compatible-mode/v1). Compatibility mode has limitations: - Does NOT support enable_search (internet search) - Does NOT support enable_thinking (deep thinking mode) - Does NOT support incremental_output - Only supports standard OpenAI parameters
For full Qwen feature support, use client/dashscope instead.
type CompletionTokensDetails ¶ added in v0.2.4
type CompletionTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens,omitempty"`
AudioTokens int `json:"audio_tokens,omitempty"`
AcceptedPredictionTokens int `json:"accepted_prediction_tokens,omitempty"`
RejectedPredictionTokens int `json:"rejected_prediction_tokens,omitempty"`
}
CompletionTokensDetails contains details about completion tokens
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
type PromptTokensDetails ¶ added in v0.2.4
type PromptTokensDetails struct {
CachedTokens int `json:"cached_tokens,omitempty"`
AudioTokens int `json:"audio_tokens,omitempty"`
}
PromptTokensDetails contains details about prompt tokens
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"`
Usage *Usage `json:"usage,omitempty"`
}
StreamChunk represents a chunk in the streaming response
type Tool ¶
Tool represents a tool definition
func ToolsToWire ¶
ToolsToWire converts core.Tool slice to OpenAI wire format
type ToolCall ¶
type ToolCall struct {
Index *int `json:"index,omitempty"` // streaming delta index (nil in non-streaming)
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"` // "function"
Function struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
} `json:"function,omitempty"`
}
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"`
PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details,omitempty"`
CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details,omitempty"`
}
Usage represents token usage