Documentation
¶
Index ¶
- func AppendToolJSONDelta(existing json.RawMessage, delta string) json.RawMessage
- func BearerTokenHeaders(r *http.Request, apiKey string)
- func BuildAnthropicToolResultBlock(writeback ToolOutputWriteback) map[string]any
- func BuildGeminiFunctionResponse(writeback ToolOutputWriteback) map[string]any
- func BuildOpenAIResponsesToolOutputItem(writeback ToolOutputWriteback, idMapper func(string) string) map[string]any
- func BuildToolCallTypeIndex(msgs []types.Message) map[string]string
- func ChooseCapabilityModel(reqModel, defaultModel string) string
- func ChooseModel(req *llm.ChatRequest, defaultModel, fallbackModel string) string
- func ConvertCustomToolFormat(format *types.ToolFormat) map[string]any
- func ConvertResponseFormat(rf *llm.ResponseFormat) any
- func CountOpenAICompatPromptTokens(model string, body OpenAICompatRequest) int
- 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 IsSearchToolPlaceholder(name string) bool
- func ListModelsOpenAICompat(ctx context.Context, client *http.Client, ...) ([]llm.Model, error)
- func MapHTTPError(status int, msg string, provider string) *types.Error
- func NewCustomToolCall(id, name, input string) types.ToolCall
- func NewFunctionToolCall(id, name string, arguments json.RawMessage) types.ToolCall
- func NormalizeOpenAIPromptCacheRetention(retention string, provider string) (string, *types.Error)
- func NormalizeStringSliceAny(raw any) []string
- func NormalizeToolType(toolType string) string
- func NormalizeUniqueStrings(values []string) []string
- 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
- func ToolCallChunk(call types.ToolCall) []types.ToolCall
- func ToolOutputResponseMap(content string) map[string]any
- func ToolParametersSchemaMap(parameters json.RawMessage) map[string]any
- func UnwrapStringifiedJSON(raw json.RawMessage) json.RawMessage
- 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 NormalizedToolChoice
- type OpenAIAnnotation
- type OpenAICompatChoice
- type OpenAICompatCustomCall
- type OpenAICompatCustomTool
- type OpenAICompatErrorResp
- type OpenAICompatFunction
- type OpenAICompatMessage
- type OpenAICompatRequest
- type OpenAICompatResponse
- type OpenAICompatTool
- type OpenAICompatToolCall
- type OpenAICompatUsage
- type PromptTokensDetails
- type StreamOptions
- type Thinking
- type ToolCallDeltaAccumulator
- func (a *ToolCallDeltaAccumulator) Append(itemID, delta string)
- func (a *ToolCallDeltaAccumulator) CompleteCustom(itemID string) (types.ToolCall, bool)
- func (a *ToolCallDeltaAccumulator) CompleteFunction(itemID string) (types.ToolCall, bool)
- func (a *ToolCallDeltaAccumulator) Register(itemID, toolType, name, callID string)
- type ToolOutputWriteback
- type URLCitationDetail
- type WebSearchApproxLocation
- type WebSearchOptions
- type WebSearchUserLocation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendToolJSONDelta ¶ added in v1.10.0
func AppendToolJSONDelta(existing json.RawMessage, delta string) json.RawMessage
func BearerTokenHeaders ¶
BearerTokenHeaders 是标准的 Bearer token 认证 header 构建函数。 用于 multimodal helper 函数的 buildHeadersFunc 参数,避免各 provider 重复定义匿名函数。
func BuildAnthropicToolResultBlock ¶ added in v1.10.0
func BuildAnthropicToolResultBlock(writeback ToolOutputWriteback) map[string]any
func BuildGeminiFunctionResponse ¶ added in v1.10.0
func BuildGeminiFunctionResponse(writeback ToolOutputWriteback) map[string]any
func BuildOpenAIResponsesToolOutputItem ¶ added in v1.10.0
func BuildOpenAIResponsesToolOutputItem(writeback ToolOutputWriteback, idMapper func(string) string) map[string]any
func BuildToolCallTypeIndex ¶ added in v1.10.0
func ChooseCapabilityModel ¶
ChooseCapabilityModel 根据请求模型和默认模型选择最终使用的模型。
func ChooseModel ¶
func ChooseModel(req *llm.ChatRequest, defaultModel, fallbackModel string) string
ChooseModel 根据请求和默认值选择模型
func ConvertCustomToolFormat ¶ added in v1.10.0
func ConvertCustomToolFormat(format *types.ToolFormat) map[string]any
func ConvertResponseFormat ¶
func ConvertResponseFormat(rf *llm.ResponseFormat) any
ConvertResponseFormat 将 llm.ResponseFormat 转换为 OpenAI 兼容的 response_format 参数。 返回 nil 表示不设置 response_format。
func CountOpenAICompatPromptTokens ¶ added in v1.9.4
func CountOpenAICompatPromptTokens(model string, body OpenAICompatRequest) int
CountOpenAICompatPromptTokens estimates prompt tokens from a fully-built OpenAI-compatible request body, immediately before it is sent upstream.
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 IsSearchToolPlaceholder ¶ added in v1.10.0
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 NewCustomToolCall ¶ added in v1.10.0
func NewFunctionToolCall ¶ added in v1.10.0
func NewFunctionToolCall(id, name string, arguments json.RawMessage) types.ToolCall
func NormalizeOpenAIPromptCacheRetention ¶ added in v1.9.1
NormalizeOpenAIPromptCacheRetention validates the OpenAI prompt cache retention value against the current official SDK enum.
func NormalizeStringSliceAny ¶ added in v1.10.0
func NormalizeToolType ¶ added in v1.10.0
func NormalizeUniqueStrings ¶ added in v1.10.0
func NotSupportedError ¶
NotSupportedError 返回不支持的错误
func ReadErrorMessage ¶
ReadErrorMessage 读取响应体中的错误消息 尝试解析 JSON 错误响应,失败则回退到原始文本
func ToLLMChatResponse ¶
func ToLLMChatResponse(oa OpenAICompatResponse, provider string) *llm.ChatResponse
ToLLMChatResponse 将 OpenAI 兼容的响应转换为 llm.ChatResponse.
func ToolOutputResponseMap ¶ added in v1.10.0
func ToolParametersSchemaMap ¶ added in v1.10.0
func ToolParametersSchemaMap(parameters json.RawMessage) map[string]any
func UnwrapStringifiedJSON ¶ added in v1.7.1
func UnwrapStringifiedJSON(raw json.RawMessage) json.RawMessage
UnwrapStringifiedJSON 检测并修复双重序列化的 JSON 参数。 某些模型(如 glm-5)返回的 tool_calls arguments 是字符串形式 (如 `"{\\"city\\":\\"北京\\"}"` )而非原始 JSON 对象。 此函数尝试将其解包为原始 JSON 字节,确保下游 schema 校验和工具执行正常。
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 NormalizedToolChoice ¶ added in v1.10.0
type NormalizedToolChoice struct {
Mode string
SpecificName string
AllowedFunctionNames []string
DisableParallelToolUse *bool
IncludeServerSideToolUse *bool
}
func NormalizeToolChoice ¶ added in v1.10.0
func NormalizeToolChoice(choice any) NormalizedToolChoice
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 OpenAICompatCustomCall ¶ added in v1.9.1
type OpenAICompatCustomCall struct {
Name string `json:"name"`
Input string `json:"input,omitempty"`
}
OpenAICompatCustomCall represents an OpenAI custom tool call payload.
type OpenAICompatCustomTool ¶ added in v1.9.1
type OpenAICompatCustomTool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Format *types.ToolFormat `json:"format,omitempty"`
}
OpenAICompatCustomTool represents an OpenAI custom tool definition.
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"`
Strict *bool `json:"strict,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 级别元数据
PromptCacheKey string `json:"prompt_cache_key,omitempty"` // prompt cache routing key
PromptCacheRetention string `json:"prompt_cache_retention,omitempty"` // prompt cache retention policy
// Responses API 扩展字段
PreviousResponseID string `json:"previous_response_id,omitempty"` // 连续对话上下文 ID
ConversationID string `json:"conversation_id,omitempty"` // OpenAI server-managed conversation ID
Include []string `json:"include,omitempty"` // include 字段
Truncation string `json:"truncation,omitempty"` // auto/disabled
}
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,omitempty"`
Custom *OpenAICompatCustomTool `json:"custom,omitempty"`
}
OpenAICompatTool 表示 OpenAI 兼容的工具定义.
func ConvertToolsToOpenAI ¶
func ConvertToolsToOpenAI(tools []types.ToolSchema) []OpenAICompatTool
ConvertToolsToOpenAI 将 types.ToolSchema 切片转换为 OpenAI 兼容格式.
type OpenAICompatToolCall ¶
type OpenAICompatToolCall struct {
Index int `json:"index"`
ID string `json:"id"`
Type string `json:"type"`
Function *OpenAICompatFunction `json:"function,omitempty"`
Custom *OpenAICompatCustomCall `json:"custom,omitempty"`
}
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 ToolCallDeltaAccumulator ¶ added in v1.10.0
type ToolCallDeltaAccumulator struct {
// contains filtered or unexported fields
}
func NewToolCallDeltaAccumulator ¶ added in v1.10.0
func NewToolCallDeltaAccumulator() *ToolCallDeltaAccumulator
func (*ToolCallDeltaAccumulator) Append ¶ added in v1.10.0
func (a *ToolCallDeltaAccumulator) Append(itemID, delta string)
func (*ToolCallDeltaAccumulator) CompleteCustom ¶ added in v1.10.0
func (a *ToolCallDeltaAccumulator) CompleteCustom(itemID string) (types.ToolCall, bool)
func (*ToolCallDeltaAccumulator) CompleteFunction ¶ added in v1.10.0
func (a *ToolCallDeltaAccumulator) CompleteFunction(itemID string) (types.ToolCall, bool)
func (*ToolCallDeltaAccumulator) Register ¶ added in v1.10.0
func (a *ToolCallDeltaAccumulator) Register(itemID, toolType, name, callID string)
type ToolOutputWriteback ¶ added in v1.10.0
type ToolOutputWriteback struct {
ToolType string
CallID string
Name string
Content string
IsError bool
}
func ToolOutputFromMessage ¶ added in v1.10.0
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.