From 62e6ae960f461d938aa76367231230cea9cee309 Mon Sep 17 00:00:00 2001 From: Andy Yang Date: Mon, 23 Mar 2026 14:20:31 +0800 Subject: [PATCH] fix(chat): support reasoning content in streaming responses for vLLM models - Parse reasoning field together with standard stream response using local struct - Unify reasoning content extraction from standard and vLLM response paths - Adapt processStreamDelta to accept reasoning content as a separate parameter - Send reasoning content as thinking response in the stream when available - Ensure compatibility with existing SDK logic by constructing standard choice object --- internal/models/chat/provider_chat.go | 3 ++- internal/models/chat/remote_api.go | 37 ++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/internal/models/chat/provider_chat.go b/internal/models/chat/provider_chat.go index 71837dba..8afbf65d 100644 --- a/internal/models/chat/provider_chat.go +++ b/internal/models/chat/provider_chat.go @@ -78,5 +78,6 @@ func (c *GenericChat) customizeRequest(req *openai.ChatCompletionRequest, opts * req.ChatTemplateKwargs = map[string]interface{}{ "enable_thinking": thinking, } - return nil, false // 使用标准请求(已修改) + + return req, true } diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go index 69f6d957..ae21f8ba 100644 --- a/internal/models/chat/remote_api.go +++ b/internal/models/chat/remote_api.go @@ -477,7 +477,7 @@ func (c *RemoteAPIChat) processStream(ctx context.Context, stream *openai.ChatCo } if len(response.Choices) > 0 { - c.processStreamDelta(ctx, &response.Choices[0], state, streamChan) + c.processStreamDelta(ctx, &response.Choices[0], state, streamChan, response.Choices[0].Delta.ReasoningContent) } } } @@ -522,14 +522,39 @@ func (c *RemoteAPIChat) processRawHTTPStream(ctx context.Context, resp *http.Res continue } - var streamResp openai.ChatCompletionStreamResponse + // 使用局部结构体进行一次性解析,同时捕捉标准字段和 vLLM 的 reasoning 字段,避免性能损失 + var streamResp struct { + openai.ChatCompletionStreamResponse + Choices []struct { + Index int `json:"index"` + Delta struct { + openai.ChatCompletionStreamChoiceDelta + Reasoning string `json:"reasoning,omitempty"` + } `json:"delta"` + FinishReason openai.FinishReason `json:"finish_reason"` + } `json:"choices"` + } + if err := json.Unmarshal(event.Data, &streamResp); err != nil { logger.Errorf(ctx, "Failed to parse stream response: %v", err) continue } if len(streamResp.Choices) > 0 { - c.processStreamDelta(ctx, &streamResp.Choices[0], state, streamChan) + choice := streamResp.Choices[0] + // 统一获取逻辑(支持标准和 vLLM 两种路径) + reasoning := choice.Delta.Reasoning + if reasoning == "" { + reasoning = choice.Delta.ReasoningContent + } + + // 构造一个标准 SDK 兼容的 choice 对象传给下游,保证现有逻辑完全不动 + sdkChoice := openai.ChatCompletionStreamChoice{ + Index: choice.Index, + Delta: choice.Delta.ChatCompletionStreamChoiceDelta, + FinishReason: choice.FinishReason, + } + c.processStreamDelta(ctx, &sdkChoice, state, streamChan, reasoning) } } } @@ -570,7 +595,7 @@ func (s *streamState) buildOrderedToolCalls() []types.LLMToolCall { } // processStreamDelta 处理流式响应的单个 delta -func (c *RemoteAPIChat) processStreamDelta(ctx context.Context, choice *openai.ChatCompletionStreamChoice, state *streamState, streamChan chan types.StreamResponse) { +func (c *RemoteAPIChat) processStreamDelta(ctx context.Context, choice *openai.ChatCompletionStreamChoice, state *streamState, streamChan chan types.StreamResponse, reasoningContent string) { delta := choice.Delta isDone := string(choice.FinishReason) != "" @@ -580,11 +605,11 @@ func (c *RemoteAPIChat) processStreamDelta(ctx context.Context, choice *openai.C } // 发送思考内容(ReasoningContent,支持 DeepSeek 等模型) - if delta.ReasoningContent != "" { + if reasoningContent != "" { state.hasThinking = true streamChan <- types.StreamResponse{ ResponseType: types.ResponseTypeThinking, - Content: delta.ReasoningContent, + Content: reasoningContent, Done: false, } }