fix: omit unpopulated max_completion_tokens to prevent 1-token truncation

This commit is contained in:
Luxferre
2026-09-05 16:52:16 +03:00
parent 65ede5ec2a
commit 6877e2b358
+13 -4
View File
@@ -114,8 +114,8 @@ type ChatCompletionRequest struct {
Tools []Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
Stream bool `json:"stream"`
MaxTokens int `json:"max_tokens"`
MaxCompletionTokens int `json:"max_completion_tokens"`
MaxTokens int `json:"max_tokens,omitempty"`
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
ReasoningEffort string `json:"reasoning_effort,omitempty"`
@@ -1508,9 +1508,14 @@ func (s *QwenService) chatDirectOpenAI(endpointURL string, w http.ResponseWriter
reqCopy := req
reqCopy.Model = resolvedModel
if reqCopy.MaxTokens == 0 && maxTokens > 0 {
reqCopy.MaxTokens = maxTokens
effTokens := maxTokens
if reqCopy.MaxCompletionTokens > 0 {
effTokens = reqCopy.MaxCompletionTokens
} else if reqCopy.MaxTokens > 0 {
effTokens = reqCopy.MaxTokens
}
reqCopy.MaxTokens = effTokens
reqCopy.MaxCompletionTokens = 0
reqJSON, err := json.Marshal(reqCopy)
if err != nil {
@@ -1541,6 +1546,10 @@ func (s *QwenService) chatDirectOpenAI(endpointURL string, w http.ResponseWriter
}
for k, vv := range resp.Header {
lowerKey := strings.ToLower(k)
if lowerKey == "content-length" || lowerKey == "transfer-encoding" || lowerKey == "connection" || lowerKey == "keep-alive" {
continue
}
for _, v := range vv {
w.Header().Add(k, v)
}