mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	feat: enhance reasoning token handling in OpenAI adaptor
This commit is contained in:
		@@ -66,6 +66,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)
 | 
			
		||||
			}
 | 
			
		||||
@@ -99,6 +102,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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -104,6 +104,9 @@ func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.M
 | 
			
		||||
 | 
			
		||||
	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 {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user