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
This commit is contained in:
Andy Yang
2026-03-23 14:20:31 +08:00
committed by lyingbug
parent 5b43d04d6b
commit 62e6ae960f
2 changed files with 33 additions and 7 deletions
+2 -1
View File
@@ -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
}
+31 -6
View File
@@ -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,
}
}