Files
ragflow/internal/entity/models/common_test.go
bitloi 220ee9dbfb fix: normalize reasoning model families (#15612)
### What problem does this PR solve?

Closes #15611.

RAGFlow's fallback reasoning parser only recognized the exact model
family `qwen3`. For provider-prefixed Qwen model names such as
SiliconFlow's `qwen/qwen3-8b`, the derived model class can be
`qwen/qwen3`, so inline `<think>...</think>` content was not split from
the visible answer when `reasoning_content` was absent.

This PR normalizes model-family detection before fallback reasoning
extraction, keeps the parser nil-safe, and adds focused tests for Qwen3
variants plus Gitee and SiliconFlow chat responses.

It also makes SiliconFlow propagate `ChatConfig.Thinking` into the chat
request body, matching the existing Gitee behavior, so Qwen thinking
mode is actually enabled when requested.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] Refactoring

### Validation

- `/root/go/bin/gofmt -l internal/entity/models/common.go
internal/entity/models/common_test.go
internal/entity/models/reasoning_family_provider_test.go
internal/entity/models/siliconflow.go`
- `git diff --check`
- `/root/go/bin/go test ./internal/entity/models -run
'Test(NormalizeModelFamily|GetThinkingAndAnswer|GiteeChatExtractsQwenThinkingFromInlineContent|SiliconflowChatExtractsProviderPrefixedQwenThinkingFromInlineContent)'
-vet=off -count=1`

Note: the full package command `/root/go/bin/go test
./internal/entity/models -vet=off -count=1` now runs locally, but it
currently fails on an unrelated existing
`TestAstraflowEmbedReturnsNoSuchMethod` panic in
`internal/entity/models/astraflow.go:482`.
2026-06-08 13:32:52 +08:00

85 lines
2.4 KiB
Go

package models
import "testing"
func TestNormalizeModelFamily(t *testing.T) {
tests := []struct {
name string
input *string
want string
}{
{name: "nil", input: nil, want: ""},
{name: "empty", input: modelFamilyTestString(""), want: ""},
{name: "qwen3", input: modelFamilyTestString("qwen3"), want: "qwen3"},
{name: "qwen3 variant", input: modelFamilyTestString("qwen3-8b"), want: "qwen3"},
{name: "provider-prefixed qwen3", input: modelFamilyTestString("qwen/qwen3-8b"), want: "qwen3"},
{name: "case-varied qwen3", input: modelFamilyTestString("Qwen/Qwen3.5-4B"), want: "qwen3"},
{name: "provider-prefixed non-qwen", input: modelFamilyTestString("deepseek/deepseek-r1"), want: "deepseek"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NormalizeModelFamily(tt.input); got != tt.want {
t.Fatalf("NormalizeModelFamily()=%q, want %q", got, tt.want)
}
})
}
}
func TestGetThinkingAndAnswerExtractsQwenThinking(t *testing.T) {
tests := []string{
"qwen3",
"qwen3-8b",
"qwen/qwen3",
"qwen/qwen3-8b",
"Qwen/Qwen3.5-4B",
}
for _, modelFamily := range tests {
t.Run(modelFamily, func(t *testing.T) {
content := "<think>\nreasoning</think>\nanswer"
reasoning, answer := GetThinkingAndAnswer(&modelFamily, &content)
if reasoning == nil || *reasoning != "reasoning" {
t.Fatalf("reasoning=%v, want reasoning", reasoning)
}
if answer == nil || *answer != "answer" {
t.Fatalf("answer=%v, want answer", answer)
}
})
}
}
func TestGetThinkingAndAnswerSkipsUnknownFamily(t *testing.T) {
modelFamily := "deepseek/deepseek-r1"
content := "<think>reasoning</think>answer"
reasoning, answer := GetThinkingAndAnswer(&modelFamily, &content)
if reasoning != nil {
t.Fatalf("reasoning=%q, want nil", *reasoning)
}
if answer == nil || *answer != content {
t.Fatalf("answer=%v, want original content", answer)
}
}
func TestGetThinkingAndAnswerHandlesNilInputs(t *testing.T) {
reasoning, answer := GetThinkingAndAnswer(nil, nil)
if reasoning != nil || answer != nil {
t.Fatalf("GetThinkingAndAnswer(nil, nil)=(%v, %v), want nils", reasoning, answer)
}
content := "answer"
reasoning, answer = GetThinkingAndAnswer(nil, &content)
if reasoning != nil {
t.Fatalf("reasoning=%q, want nil", *reasoning)
}
if answer == nil || *answer != content {
t.Fatalf("answer=%v, want original content", answer)
}
}
func modelFamilyTestString(value string) *string {
return &value
}