refactor: 实现 ConversionEngine 协议转换引擎,替代旧 protocol 包
引入 Canonical Model 和 ProtocolAdapter 架构,支持 OpenAI/Anthropic 协议间 无缝转换,统一 ProxyHandler 替代分散的 OpenAI/Anthropic Handler,简化 ProviderClient 为协议无关的 HTTP 发送器,Provider 新增 protocol 字段。
This commit is contained in:
71
backend/internal/conversion/canonical/extended.go
Normal file
71
backend/internal/conversion/canonical/extended.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package canonical
|
||||
|
||||
// CanonicalModel 规范模型
|
||||
type CanonicalModel struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Created int64 `json:"created,omitempty"`
|
||||
OwnedBy string `json:"owned_by,omitempty"`
|
||||
}
|
||||
|
||||
// CanonicalModelList 规范模型列表
|
||||
type CanonicalModelList struct {
|
||||
Models []CanonicalModel `json:"models"`
|
||||
}
|
||||
|
||||
// CanonicalModelInfo 规范模型详情
|
||||
type CanonicalModelInfo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Created int64 `json:"created,omitempty"`
|
||||
OwnedBy string `json:"owned_by,omitempty"`
|
||||
}
|
||||
|
||||
// CanonicalEmbeddingRequest 规范嵌入请求
|
||||
type CanonicalEmbeddingRequest struct {
|
||||
Model string `json:"model"`
|
||||
Input any `json:"input"` // string 或 []string
|
||||
EncodingFormat string `json:"encoding_format,omitempty"`
|
||||
Dimensions *int `json:"dimensions,omitempty"`
|
||||
}
|
||||
|
||||
// CanonicalEmbeddingResponse 规范嵌入响应
|
||||
type CanonicalEmbeddingResponse struct {
|
||||
Data []EmbeddingData `json:"data"`
|
||||
Model string `json:"model"`
|
||||
Usage EmbeddingUsage `json:"usage"`
|
||||
}
|
||||
|
||||
// EmbeddingData 嵌入数据项
|
||||
type EmbeddingData struct {
|
||||
Index int `json:"index"`
|
||||
Embedding any `json:"embedding"` // 根据格式不同可能是 []float64 或 base64 字符串
|
||||
}
|
||||
|
||||
// EmbeddingUsage 嵌入用量
|
||||
type EmbeddingUsage struct {
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
TotalTokens int `json:"total_tokens"`
|
||||
}
|
||||
|
||||
// CanonicalRerankRequest 规范重排序请求
|
||||
type CanonicalRerankRequest struct {
|
||||
Model string `json:"model"`
|
||||
Query string `json:"query"`
|
||||
Documents []string `json:"documents"`
|
||||
TopN *int `json:"top_n,omitempty"`
|
||||
ReturnDocuments *bool `json:"return_documents,omitempty"`
|
||||
}
|
||||
|
||||
// CanonicalRerankResponse 规范重排序响应
|
||||
type CanonicalRerankResponse struct {
|
||||
Results []RerankResult `json:"results"`
|
||||
Model string `json:"model"`
|
||||
}
|
||||
|
||||
// RerankResult 重排序结果项
|
||||
type RerankResult struct {
|
||||
Index int `json:"index"`
|
||||
RelevanceScore float64 `json:"relevance_score"`
|
||||
Document *string `json:"document,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user