feat: Support transforming reasoning_content returned by Qwen to OpenAI contract (#1791)

This commit is contained in:
Kent Dong
2025-02-21 17:32:02 +08:00
committed by GitHub
parent 2986e1911d
commit fabc22f218
4 changed files with 86 additions and 24 deletions

View File

@@ -1,6 +1,9 @@
package provider
import "strings"
import (
"fmt"
"strings"
)
const (
streamEventIdItemKey = "id:"
@@ -110,9 +113,16 @@ type chatCompletionChoice struct {
}
type usage struct {
PromptTokens int `json:"prompt_tokens,omitempty"`
CompletionTokens int `json:"completion_tokens,omitempty"`
TotalTokens int `json:"total_tokens,omitempty"`
PromptTokens int `json:"prompt_tokens,omitempty"`
CompletionTokens int `json:"completion_tokens,omitempty"`
TotalTokens int `json:"total_tokens,omitempty"`
CompletionTokensDetails *completionTokensDetails `json:"completion_tokens_details,omitempty"`
}
type completionTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens,omitempty"`
AcceptedPredictionTokens int `json:"accepted_prediction_tokens,omitempty"`
RejectedPredictionTokens int `json:"rejected_prediction_tokens,omitempty"`
}
type chatMessage struct {
@@ -126,6 +136,24 @@ type chatMessage struct {
Refusal string `json:"refusal,omitempty"`
}
func (m *chatMessage) handleReasoningContent(reasoningContentMode string) {
if m.ReasoningContent == "" {
return
}
switch reasoningContentMode {
case reasoningBehaviorIgnore:
m.ReasoningContent = ""
break
case reasoningBehaviorConcat:
m.Content = fmt.Sprintf("%v\n%v", m.ReasoningContent, m.Content)
m.ReasoningContent = ""
break
case reasoningBehaviorPassThrough:
default:
break
}
}
type messageContent struct {
Type string `json:"type,omitempty"`
Text string `json:"text"`
@@ -138,6 +166,9 @@ type imageUrl struct {
}
func (m *chatMessage) IsEmpty() bool {
if m.ReasoningContent != "" {
return false
}
if m.IsStringContent() && m.Content != "" {
return false
}