Skip to content

Commit c2cdc16

Browse files
MEKXHlyingbug
authored andcommitted
fix(chat): strip thinking content from model output
Handle thinking models that return wrapped content even when Thinking=false is configured. This is a fallback strategy for models like MiniMax-M2.1 that do not fully support the Thinking parameter. The thinking tags and their enclosed content are completely stripped.
1 parent 9a3f09c commit c2cdc16

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

internal/models/chat/remote_api.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"strings"
1011

1112
"github.com/Tencent/WeKnora/internal/logger"
1213
"github.com/Tencent/WeKnora/internal/models/provider"
@@ -241,8 +242,13 @@ func (c *RemoteAPIChat) parseCompletionResponse(resp *openai.ChatCompletionRespo
241242
}
242243

243244
choice := resp.Choices[0]
245+
246+
// 处理思考模型的输出:移除 <think></think> 标签包裹的思考过程
247+
// 为设置了 Thinking=false 但模型仍返回思考内容的情况和部分不支持Thinking=false的思考模型(例如Miniax-M2.1)提供兜底策略
248+
content := removeThinkingContent(choice.Message.Content)
249+
244250
response := &types.ChatResponse{
245-
Content: choice.Message.Content,
251+
Content: content,
246252
FinishReason: string(choice.FinishReason),
247253
Usage: struct {
248254
PromptTokens int `json:"prompt_tokens"`
@@ -272,6 +278,28 @@ func (c *RemoteAPIChat) parseCompletionResponse(resp *openai.ChatCompletionRespo
272278
return response, nil
273279
}
274280

281+
// removeThinkingContent 移除思考模型输出中的 <think></think> 思考过程
282+
// 仅当内容以 <think> 开头时才处理
283+
func removeThinkingContent(content string) string {
284+
const thinkStartTag = "<think>"
285+
const thinkEndTag = "</think>"
286+
287+
trimmed := strings.TrimSpace(content)
288+
if !strings.HasPrefix(trimmed, thinkStartTag) {
289+
return content
290+
}
291+
292+
// 查找最后一个 </think> 标签(处理嵌套情况)
293+
if lastEndIdx := strings.LastIndex(trimmed, thinkEndTag); lastEndIdx != -1 {
294+
if result := strings.TrimSpace(trimmed[lastEndIdx+len(thinkEndTag):]); result != "" {
295+
return result
296+
}
297+
return ""
298+
}
299+
300+
return "" // 未找到 </think>,可能思考内容过长被截断,返回空字符串
301+
}
302+
275303
// ChatStream 进行流式聊天
276304
func (c *RemoteAPIChat) ChatStream(ctx context.Context, messages []Message, opts *ChatOptions) (<-chan types.StreamResponse, error) {
277305
req := c.BuildChatCompletionRequest(messages, opts, true)

0 commit comments

Comments
 (0)