Documentation
¶
Index ¶
- func BearerTokenHeaders(r *http.Request, apiKey string)
- func ChooseCapabilityModel(reqModel, defaultModel string) string
- func ChooseModel(req *llm.ChatRequest, defaultModel, fallbackModel string) string
- func ConvertResponseFormat(rf *llm.ResponseFormat) any
- func CreateEmbeddingOpenAICompat(ctx context.Context, client *http.Client, ...) (*llm.EmbeddingResponse, error)
- func GenerateAudioOpenAICompat(ctx context.Context, client *http.Client, ...) (*llm.AudioGenerationResponse, error)
- func GenerateImageOpenAICompat(ctx context.Context, client *http.Client, ...) (*llm.ImageGenerationResponse, error)
- func GenerateVideoOpenAICompat(ctx context.Context, client *http.Client, ...) (*llm.VideoGenerationResponse, error)
- func ListModelsOpenAICompat(ctx context.Context, client *http.Client, ...) ([]llm.Model, error)
- func MapHTTPError(status int, msg string, provider string) *types.Error
- func NotSupportedError(providerName, feature string) *types.Error
- func ReadErrorMessage(body io.Reader) string
- func SafeCloseBody(body io.ReadCloser)
- func ToLLMChatResponse(oa OpenAICompatResponse, provider string) *llm.ChatResponse
- type BaseCapabilityProvider
- func (p *BaseCapabilityProvider) DoRaw(ctx context.Context, method, endpoint string, body any) ([]byte, error)
- func (p *BaseCapabilityProvider) GetJSON(ctx context.Context, endpoint string) ([]byte, error)
- func (p *BaseCapabilityProvider) GetJSONDecode(ctx context.Context, endpoint string, result any) error
- func (p *BaseCapabilityProvider) PostJSON(ctx context.Context, endpoint string, body any) ([]byte, error)
- func (p *BaseCapabilityProvider) PostJSONDecode(ctx context.Context, endpoint string, body any, result any) error
- type CapabilityConfig
- type CompletionTokensDetails
- type HeaderFunc
- type OpenAIAnnotation
- type OpenAICompatChoice
- type OpenAICompatErrorResp
- type OpenAICompatFunction
- type OpenAICompatMessage
- type OpenAICompatRequest
- type OpenAICompatResponse
- type OpenAICompatTool
- type OpenAICompatToolCall
- type OpenAICompatUsage
- type PromptTokensDetails
- type StreamOptions
- type Thinking
- type URLCitationDetail
- type WebSearchApproxLocation
- type WebSearchOptions
- type WebSearchUserLocation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BearerTokenHeaders ¶
BearerTokenHeaders 是标准的 Bearer token 认证 header 构建函数。 用于 multimodal helper 函数的 buildHeadersFunc 参数,避免各 provider 重复定义匿名函数。
func ChooseCapabilityModel ¶
ChooseCapabilityModel 根据请求模型和默认模型选择最终使用的模型。
func ChooseModel ¶
func ChooseModel(req *llm.ChatRequest, defaultModel, fallbackModel string) string
ChooseModel 根据请求和默认值选择模型
func ConvertResponseFormat ¶
func ConvertResponseFormat(rf *llm.ResponseFormat) any
ConvertResponseFormat 将 llm.ResponseFormat 转换为 OpenAI 兼容的 response_format 参数。 返回 nil 表示不设置 response_format。
func CreateEmbeddingOpenAICompat ¶
func CreateEmbeddingOpenAICompat(ctx context.Context, client *http.Client, baseURL, apiKey, providerName, endpoint string, req *llm.EmbeddingRequest, buildHeadersFunc func(*http.Request, string)) (*llm.EmbeddingResponse, error)
CreateEmbeddingOpenAICompat 通用的 OpenAI 兼容 Embedding 函数
func GenerateAudioOpenAICompat ¶
func GenerateAudioOpenAICompat(ctx context.Context, client *http.Client, baseURL, apiKey, providerName, endpoint string, req *llm.AudioGenerationRequest, buildHeadersFunc func(*http.Request, string)) (*llm.AudioGenerationResponse, error)
GenerateAudioOpenAICompat 通用的 OpenAI 兼容音频生成函数
func GenerateImageOpenAICompat ¶
func GenerateImageOpenAICompat(ctx context.Context, client *http.Client, baseURL, apiKey, providerName, endpoint string, req *llm.ImageGenerationRequest, buildHeadersFunc func(*http.Request, string)) (*llm.ImageGenerationResponse, error)
GenerateImageOpenAICompat 通用的 OpenAI 兼容图像生成函数
func GenerateVideoOpenAICompat ¶
func GenerateVideoOpenAICompat(ctx context.Context, client *http.Client, baseURL, apiKey, providerName, endpoint string, req *llm.VideoGenerationRequest, buildHeadersFunc func(*http.Request, string)) (*llm.VideoGenerationResponse, error)
GenerateVideoOpenAICompat 通用的 OpenAI 兼容视频生成函数
func ListModelsOpenAICompat ¶
func ListModelsOpenAICompat(ctx context.Context, client *http.Client, baseURL, apiKey, providerName, modelsEndpoint string, buildHeadersFunc func(*http.Request, string)) ([]llm.Model, error)
ListModelsOpenAICompat 通用的 OpenAI 兼容 Provider 模型列表获取函数
func MapHTTPError ¶
MapHTTPError 将 HTTP 状态码映射为带有合适重试标记的 types.Error 这是所有提供者使用的通用错误映射函数
func NotSupportedError ¶
NotSupportedError 返回不支持的错误
func ReadErrorMessage ¶
ReadErrorMessage 读取响应体中的错误消息 尝试解析 JSON 错误响应,失败则回退到原始文本
func ToLLMChatResponse ¶
func ToLLMChatResponse(oa OpenAICompatResponse, provider string) *llm.ChatResponse
ToLLMChatResponse 将 OpenAI 兼容的响应转换为 llm.ChatResponse.
Types ¶
type BaseCapabilityProvider ¶
type BaseCapabilityProvider struct {
ProviderName string
Client *http.Client
BaseURL string
APIKey string
Model string
BuildHeaders HeaderFunc
}
BaseCapabilityProvider 为多模态能力模块(image/video/speech/embedding/music/threed/rerank/moderation) 提供通用的 HTTP 客户端、认证和错误处理基础设施。
func NewBaseCapabilityProvider ¶
func NewBaseCapabilityProvider(cfg CapabilityConfig) *BaseCapabilityProvider
NewBaseCapabilityProvider 创建通用能力 provider 基类。
func (*BaseCapabilityProvider) DoRaw ¶
func (p *BaseCapabilityProvider) DoRaw(ctx context.Context, method, endpoint string, body any) ([]byte, error)
DoRaw 执行底层 HTTP 请求,支持 JSON 和非 JSON 场景。 body 为 nil 时不发送请求体;body 为 io.Reader 时直接使用;否则 JSON marshal。
func (*BaseCapabilityProvider) GetJSONDecode ¶
func (p *BaseCapabilityProvider) GetJSONDecode(ctx context.Context, endpoint string, result any) error
GetJSONDecode 发送 GET 请求并将响应解码到 result。
func (*BaseCapabilityProvider) PostJSON ¶
func (p *BaseCapabilityProvider) PostJSON(ctx context.Context, endpoint string, body any) ([]byte, error)
PostJSON 发送 JSON POST 请求,返回原始响应体。 自动处理 marshal、设置 headers、状态码检查和结构化错误映射。
func (*BaseCapabilityProvider) PostJSONDecode ¶
func (p *BaseCapabilityProvider) PostJSONDecode(ctx context.Context, endpoint string, body any, result any) error
PostJSONDecode 发送 JSON POST 请求并将响应解码到 result。
type CapabilityConfig ¶
type CapabilityConfig struct {
Name string
BaseURL string
APIKey string
Model string
Timeout time.Duration
BuildHeaders HeaderFunc
}
CapabilityConfig 构造 BaseCapabilityProvider 所需的配置。
type CompletionTokensDetails ¶
type CompletionTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens"`
AudioTokens int `json:"audio_tokens,omitempty"`
AcceptedPredictionTokens int `json:"accepted_prediction_tokens,omitempty"`
RejectedPredictionTokens int `json:"rejected_prediction_tokens,omitempty"`
}
CompletionTokensDetails 补全 token 详细统计。
type OpenAIAnnotation ¶
type OpenAIAnnotation struct {
Type string `json:"type"` // "url_citation"
URLCitation *URLCitationDetail `json:"url_citation,omitempty"`
}
OpenAIAnnotation represents a URL citation annotation in a response.
type OpenAICompatChoice ¶
type OpenAICompatChoice struct {
Index int `json:"index"`
FinishReason string `json:"finish_reason"`
Message OpenAICompatMessage `json:"message"`
Delta *OpenAICompatMessage `json:"delta,omitempty"`
}
OpenAICompatChoice 表示 OpenAI 兼容响应中的单个选项.
type OpenAICompatErrorResp ¶
type OpenAICompatErrorResp struct {
Error struct {
Message string `json:"message"`
Type string `json:"type"`
Code any `json:"code"`
Param string `json:"param"`
} `json:"error"`
}
OpenAICompatErrorResp 表示 OpenAI 兼容的错误响应.
type OpenAICompatFunction ¶
type OpenAICompatFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
Arguments json.RawMessage `json:"arguments,omitempty"`
}
OpenAICompatFunction 表示 OpenAI 兼容的函数定义.
type OpenAICompatMessage ¶
type OpenAICompatMessage struct {
Role string `json:"role"`
Content string `json:"content,omitempty"`
ReasoningContent *string `json:"reasoning_content,omitempty"` // 推理内容
Refusal *string `json:"refusal,omitempty"` // 模型拒绝内容
MultiContent []map[string]any `json:"multi_content,omitempty"` // multimodal content parts
Name string `json:"name,omitempty"`
ToolCalls []OpenAICompatToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Annotations []OpenAIAnnotation `json:"annotations,omitempty"` // URL 引用
}
OpenAICompatMessage 表示 OpenAI 兼容的消息格式.
func ConvertMessagesToOpenAI ¶
func ConvertMessagesToOpenAI(msgs []types.Message) []OpenAICompatMessage
ConvertMessagesToOpenAI 将 types.Message 切片转换为 OpenAI 兼容格式.
func (OpenAICompatMessage) MarshalJSON ¶
func (m OpenAICompatMessage) MarshalJSON() ([]byte, error)
MarshalJSON 自定义序列化:当 MultiContent 非空时,将其序列化为 "content" 字段。
type OpenAICompatRequest ¶
type OpenAICompatRequest struct {
Model string `json:"model"`
Messages []OpenAICompatMessage `json:"messages"`
Tools []OpenAICompatTool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
ResponseFormat any `json:"response_format,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
Stop []string `json:"stop,omitempty"`
Stream bool `json:"stream,omitempty"`
FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"`
PresencePenalty *float32 `json:"presence_penalty,omitempty"`
RepetitionPenalty *float32 `json:"repetition_penalty,omitempty"`
N *int `json:"n,omitempty"`
LogProbs *bool `json:"logprobs,omitempty"`
TopLogProbs *int `json:"top_logprobs,omitempty"`
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
ServiceTier *string `json:"service_tier,omitempty"`
StreamOptions *StreamOptions `json:"stream_options,omitempty"`
User string `json:"user,omitempty"`
Thinking *Thinking `json:"thinking,omitempty"`
MaxCompletionTokens *int `json:"max_completion_tokens,omitempty"`
ReasoningEffort *string `json:"reasoning_effort,omitempty"`
// 新增 OpenAI 扩展字段
Store *bool `json:"store,omitempty"` // 是否存储用于蒸馏/评估
Modalities []string `json:"modalities,omitempty"` // ["text", "audio"]
WebSearchOptions *WebSearchOptions `json:"web_search_options,omitempty"` // 内置 web 搜索
Metadata map[string]string `json:"metadata,omitempty"` // OpenAI 级别元数据
}
OpenAICompatRequest 表示 OpenAI 兼容的聊天完成请求.
type OpenAICompatResponse ¶
type OpenAICompatResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Choices []OpenAICompatChoice `json:"choices"`
Usage *OpenAICompatUsage `json:"usage,omitempty"`
Created int64 `json:"created,omitempty"`
ServiceTier string `json:"service_tier,omitempty"`
}
OpenAICompatResponse 表示 OpenAI 兼容的聊天完成响应.
type OpenAICompatTool ¶
type OpenAICompatTool struct {
Type string `json:"type"`
Function OpenAICompatFunction `json:"function"`
}
OpenAICompatTool 表示 OpenAI 兼容的工具定义.
func ConvertToolsToOpenAI ¶
func ConvertToolsToOpenAI(tools []types.ToolSchema) []OpenAICompatTool
ConvertToolsToOpenAI 将 types.ToolSchema 切片转换为 OpenAI 兼容格式.
type OpenAICompatToolCall ¶
type OpenAICompatToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function OpenAICompatFunction `json:"function"`
}
OpenAICompatToolCall 表示 OpenAI 兼容的工具调用.
type OpenAICompatUsage ¶
type OpenAICompatUsage 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"`
}
OpenAICompatUsage 表示 OpenAI 兼容响应中的 token 用量.
type PromptTokensDetails ¶
type PromptTokensDetails struct {
CachedTokens int `json:"cached_tokens"`
AudioTokens int `json:"audio_tokens,omitempty"`
}
PromptTokensDetails 提示 token 详细统计。
type StreamOptions ¶
type StreamOptions struct {
IncludeUsage bool `json:"include_usage,omitempty"`
ChunkIncludeUsage bool `json:"chunk_include_usage,omitempty"`
}
StreamOptions 控制流式响应中的额外信息。
type Thinking ¶
type Thinking struct {
Type string `json:"type"` // "enabled", "disabled", "auto"
}
Thinking 控制推理/思考模式。
type URLCitationDetail ¶
type URLCitationDetail struct {
StartIndex int `json:"start_index"`
EndIndex int `json:"end_index"`
URL string `json:"url"`
Title string `json:"title"`
}
URLCitationDetail holds the details of a URL citation.
type WebSearchApproxLocation ¶
type WebSearchApproxLocation struct {
Country string `json:"country,omitempty"`
Region string `json:"region,omitempty"`
City string `json:"city,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
WebSearchApproxLocation holds approximate location details.
type WebSearchOptions ¶
type WebSearchOptions struct {
UserLocation *WebSearchUserLocation `json:"user_location,omitempty"`
SearchContextSize string `json:"search_context_size,omitempty"` // low/medium/high
}
WebSearchOptions configures the built-in web search for Chat Completions.
type WebSearchUserLocation ¶
type WebSearchUserLocation struct {
Type string `json:"type"` // "approximate"
Approximate *WebSearchApproxLocation `json:"approximate,omitempty"`
}
WebSearchUserLocation represents approximate user location.