fix: 使用openai兼容接口调用部分渠道在最终端点为claude原生端点下还是走了openai扣减input_token的逻辑
This commit is contained in:
@@ -223,11 +223,8 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycom
|
|||||||
switch info.RelayFormat {
|
switch info.RelayFormat {
|
||||||
case types.RelayFormatClaude:
|
case types.RelayFormatClaude:
|
||||||
if supportsAliAnthropicMessages(info.UpstreamModelName) {
|
if supportsAliAnthropicMessages(info.UpstreamModelName) {
|
||||||
if info.IsStream {
|
adaptor := claude.Adaptor{}
|
||||||
return claude.ClaudeStreamHandler(c, resp, info)
|
return adaptor.DoResponse(c, resp, info)
|
||||||
}
|
|
||||||
|
|
||||||
return claude.ClaudeHandler(c, resp, info)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
adaptor := openai.Adaptor{}
|
adaptor := openai.Adaptor{}
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
|
info.FinalRequestRelayFormat = types.RelayFormatClaude
|
||||||
if info.IsStream {
|
if info.IsStream {
|
||||||
return ClaudeStreamHandler(c, resp, info)
|
return ClaudeStreamHandler(c, resp, info)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayIn
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
|
func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
|
||||||
return nil, errors.New("codex channel: endpoint not supported")
|
return nil, errors.New("codex channel: /v1/messages endpoint not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
||||||
@@ -41,15 +41,15 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
||||||
return nil, errors.New("codex channel: endpoint not supported")
|
return nil, errors.New("codex channel: /v1/chat/completions endpoint not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
||||||
return nil, errors.New("codex channel: endpoint not supported")
|
return nil, errors.New("codex channel: /v1/rerank endpoint not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
||||||
return nil, errors.New("codex channel: endpoint not supported")
|
return nil, errors.New("codex channel: /v1/embeddings endpoint not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
||||||
|
|||||||
@@ -95,11 +95,8 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
switch info.RelayFormat {
|
switch info.RelayFormat {
|
||||||
case types.RelayFormatClaude:
|
case types.RelayFormatClaude:
|
||||||
if info.IsStream {
|
adaptor := claude.Adaptor{}
|
||||||
return claude.ClaudeStreamHandler(c, resp, info)
|
return adaptor.DoResponse(c, resp, info)
|
||||||
} else {
|
|
||||||
return claude.ClaudeHandler(c, resp, info)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
adaptor := openai.Adaptor{}
|
adaptor := openai.Adaptor{}
|
||||||
return adaptor.DoResponse(c, resp, info)
|
return adaptor.DoResponse(c, resp, info)
|
||||||
|
|||||||
@@ -102,11 +102,8 @@ func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.Rela
|
|||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
switch info.RelayFormat {
|
switch info.RelayFormat {
|
||||||
case types.RelayFormatClaude:
|
case types.RelayFormatClaude:
|
||||||
if info.IsStream {
|
adaptor := claude.Adaptor{}
|
||||||
return claude.ClaudeStreamHandler(c, resp, info)
|
return adaptor.DoResponse(c, resp, info)
|
||||||
} else {
|
|
||||||
return claude.ClaudeHandler(c, resp, info)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
adaptor := openai.Adaptor{}
|
adaptor := openai.Adaptor{}
|
||||||
return adaptor.DoResponse(c, resp, info)
|
return adaptor.DoResponse(c, resp, info)
|
||||||
|
|||||||
@@ -365,10 +365,11 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
|
claudeAdaptor := claude.Adaptor{}
|
||||||
if info.IsStream {
|
if info.IsStream {
|
||||||
switch a.RequestMode {
|
switch a.RequestMode {
|
||||||
case RequestModeClaude:
|
case RequestModeClaude:
|
||||||
return claude.ClaudeStreamHandler(c, resp, info)
|
return claudeAdaptor.DoResponse(c, resp, info)
|
||||||
case RequestModeGemini:
|
case RequestModeGemini:
|
||||||
if info.RelayMode == constant.RelayModeGemini {
|
if info.RelayMode == constant.RelayModeGemini {
|
||||||
return gemini.GeminiTextGenerationStreamHandler(c, info, resp)
|
return gemini.GeminiTextGenerationStreamHandler(c, info, resp)
|
||||||
@@ -381,7 +382,7 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycom
|
|||||||
} else {
|
} else {
|
||||||
switch a.RequestMode {
|
switch a.RequestMode {
|
||||||
case RequestModeClaude:
|
case RequestModeClaude:
|
||||||
return claude.ClaudeHandler(c, resp, info)
|
return claudeAdaptor.DoResponse(c, resp, info)
|
||||||
case RequestModeGemini:
|
case RequestModeGemini:
|
||||||
if info.RelayMode == constant.RelayModeGemini {
|
if info.RelayMode == constant.RelayModeGemini {
|
||||||
return gemini.GeminiTextGenerationHandler(c, info, resp)
|
return gemini.GeminiTextGenerationHandler(c, info, resp)
|
||||||
|
|||||||
@@ -347,10 +347,8 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
if info.RelayFormat == types.RelayFormatClaude {
|
if info.RelayFormat == types.RelayFormatClaude {
|
||||||
if _, ok := channelconstant.ChannelSpecialBases[info.ChannelBaseUrl]; ok {
|
if _, ok := channelconstant.ChannelSpecialBases[info.ChannelBaseUrl]; ok {
|
||||||
if info.IsStream {
|
adaptor := claude.Adaptor{}
|
||||||
return claude.ClaudeStreamHandler(c, resp, info)
|
return adaptor.DoResponse(c, resp, info)
|
||||||
}
|
|
||||||
return claude.ClaudeHandler(c, resp, info)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -109,11 +109,8 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
switch info.RelayFormat {
|
switch info.RelayFormat {
|
||||||
case types.RelayFormatClaude:
|
case types.RelayFormatClaude:
|
||||||
if info.IsStream {
|
adaptor := claude.Adaptor{}
|
||||||
return claude.ClaudeStreamHandler(c, resp, info)
|
return adaptor.DoResponse(c, resp, info)
|
||||||
} else {
|
|
||||||
return claude.ClaudeHandler(c, resp, info)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
if info.RelayMode == relayconstant.RelayModeImagesGenerations {
|
if info.RelayMode == relayconstant.RelayModeImagesGenerations {
|
||||||
return zhipu4vImageHandler(c, resp, info)
|
return zhipu4vImageHandler(c, resp, info)
|
||||||
|
|||||||
@@ -145,6 +145,8 @@ type RelayInfo struct {
|
|||||||
// RequestConversionChain records request format conversions in order, e.g.
|
// RequestConversionChain records request format conversions in order, e.g.
|
||||||
// ["openai", "openai_responses"] or ["openai", "claude"].
|
// ["openai", "openai_responses"] or ["openai", "claude"].
|
||||||
RequestConversionChain []types.RelayFormat
|
RequestConversionChain []types.RelayFormat
|
||||||
|
// 最终请求到上游的格式 TODO: 当前仅设置了Claude
|
||||||
|
FinalRequestRelayFormat types.RelayFormat
|
||||||
|
|
||||||
ThinkingContentInfo
|
ThinkingContentInfo
|
||||||
TokenCountMeta
|
TokenCountMeta
|
||||||
|
|||||||
@@ -334,7 +334,7 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage
|
|||||||
|
|
||||||
var audioInputQuota decimal.Decimal
|
var audioInputQuota decimal.Decimal
|
||||||
var audioInputPrice float64
|
var audioInputPrice float64
|
||||||
isClaudeUsageSemantic := relayInfo.ChannelType == constant.ChannelTypeAnthropic
|
isClaudeUsageSemantic := relayInfo.FinalRequestRelayFormat == types.RelayFormatClaude
|
||||||
if !relayInfo.PriceData.UsePrice {
|
if !relayInfo.PriceData.UsePrice {
|
||||||
baseTokens := dPromptTokens
|
baseTokens := dPromptTokens
|
||||||
// 减去 cached tokens
|
// 减去 cached tokens
|
||||||
|
|||||||
Reference in New Issue
Block a user