feat: enhance reasoning token handling in OpenAI adaptor

This commit is contained in:
Laisky.Cai 2025-02-19 08:10:04 +00:00
parent 480f248a3d
commit 5ba60433d7
2 changed files with 12 additions and 0 deletions

View File

@ -64,6 +64,9 @@ func StreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*model.E
if choice.Delta.Reasoning != nil {
reasoningText += *choice.Delta.Reasoning
}
if choice.Delta.ReasoningContent != nil {
reasoningText += *choice.Delta.ReasoningContent
}
responseText += conv.AsString(choice.Delta.Content)
}
@ -97,6 +100,12 @@ func StreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*model.E
return ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), "", nil
}
// If there is no reasoning tokens in the completion, we should count the reasoning tokens in the response.
if len(reasoningText) > 0 &&
(usage.CompletionTokensDetails == nil || usage.CompletionTokensDetails.ReasoningTokens == 0) {
usage.CompletionTokens += CountToken(reasoningText)
}
return nil, reasoningText + responseText, usage
}

View File

@ -102,6 +102,9 @@ func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.M
var quota int64
completionRatio := billingratio.GetCompletionRatio(textRequest.Model, meta.ChannelType)
promptTokens := usage.PromptTokens
// It appears that DeepSeek's official service automatically merges ReasoningTokens into CompletionTokens,
// but the behavior of third-party providers may differ, so for now we do not add them manually.
// completionTokens := usage.CompletionTokens + usage.CompletionTokensDetails.ReasoningTokens
completionTokens := usage.CompletionTokens
quota = int64(math.Ceil((float64(promptTokens) + float64(completionTokens)*completionRatio) * ratio))
if ratio != 0 && quota <= 0 {