Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(openai): pass request reasoning level to AsyncChat so reasoning=5 selects the agentic path (#21342)

cielcc1b077db67e
diff --git a/internal/service/openai_chat.go b/internal/service/openai_chat.go
index c45a6c9ca2..937e2629fd 100644
--- a/internal/service/openai_chat.go
+++ b/internal/service/openai_chat.go
@@ -21,11 +21,12 @@ import (
"encoding/json"
"fmt"
"net/http"
- "ragflow/internal/entity"
"regexp"
"strings"
"time"
+ "ragflow/internal/entity"
+
"ragflow/internal/common"
permissionresponse "ragflow/internal/permission/response"
"ragflow/internal/tokenizer"
@@ -131,6 +132,7 @@ type OpenAIChatRequest struct {
Messages []map[string]interface{} `json:"messages"`
Stream *bool `json:"stream,omitempty"`
ExtraBody interface{} `json:"extra_body,omitempty"`
+ Reasoning interface{} `json:"reasoning,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
@@ -185,8 +187,8 @@ func (s *OpenAIChatService) OpenAIChatCompletions(c *gin.Context, userID, chatID
}
}
- var needReference = false
- var includeRefMetadata = false
+ needReference := false
+ includeRefMetadata := false
var metadataFields []string
var metadataCondition map[string]interface{}
if eb, ok := req.ExtraBody.(map[string]interface{}); ok {
@@ -333,14 +335,7 @@ func (s *OpenAIChatService) OpenAIChatCompletions(c *gin.Context, userID, chatID
}
}
- chatKwargs := map[string]interface{}{
- "toolcall_session": nil, // no tool calls on OpenAI-compat path
- "tools": nil,
- "quote": needReference,
- }
- if docIDsStr != "" {
- chatKwargs["doc_ids"] = docIDsStr
- }
+ chatKwargs := openAIChatKwargs(&req, needReference, docIDsStr)
asyncResults, asyncErr := s.pipeline.AsyncChat(ctx, userID, dialog, filteredMessages, openaiReq.Stream, chatKwargs)
if asyncErr != nil {
s.writeDataError(c, asyncErr.Error())
@@ -552,7 +547,7 @@ func (s *OpenAIChatService) filterMessages(messages []map[string]interface{}) []
// cleanCitationMarkers strips "##N$$" markers from the answer.
func cleanCitationMarkers(s string) string {
- var citationMarkerRegex = regexp.MustCompile(`##\d+\$\$`)
+ citationMarkerRegex := regexp.MustCompile(`##\d+\$\$`)
return citationMarkerRegex.ReplaceAllString(s, "")
}
@@ -570,6 +565,27 @@ func isContentDelta(answer *string) bool {
return true
}
+// openAIChatKwargs builds the pipeline kwargs for one OpenAI-compat request.
+//
+// `reasoning` rides in the kwargs only. It never joins the generation config,
+// which MergeGenerationConfig folds into the dialog's saved llm_setting, so the
+// selection lasts one turn. Leaving the key out when the request omits it keeps
+// AsyncChat falling back to the dialog's prompt_config level.
+func openAIChatKwargs(req *OpenAIChatRequest, needReference bool, docIDsStr string) map[string]interface{} {
+ kwargs := map[string]interface{}{
+ "toolcall_session": nil, // no tool calls on OpenAI-compat path
+ "tools": nil,
+ "quote": needReference,
+ }
+ if docIDsStr != "" {
+ kwargs["doc_ids"] = docIDsStr
+ }
+ if req.Reasoning != nil {
+ kwargs["reasoning"] = req.Reasoning
+ }
+ return kwargs
+}
+
// extractGenerationConfig mirrors Python's extract_generation_config.
func extractGenerationConfig(req *OpenAIChatRequest) map[string]interface{} {
cfg := make(map[string]interface{})
diff --git a/internal/service/openai_chat_test.go b/internal/service/openai_chat_test.go
index 4dc94f62bc..cd4b3cc8ce 100644
--- a/internal/service/openai_chat_test.go
+++ b/internal/service/openai_chat_test.go
@@ -525,13 +525,60 @@ func TestService_ExtractGenerationConfig_OnlyKnownFields(t *testing.T) {
if cfg["presence_penalty"] != pres {
t.Fatalf("presence_penalty: got %v want %v", cfg["presence_penalty"], pres)
}
- for _, k := range []string{"stop", "user", "internet", "tools"} {
+ for _, k := range []string{"stop", "user", "internet", "tools", "reasoning"} {
if _, has := cfg[k]; has {
t.Fatalf("%s should not be in generation config, got %v", k, cfg[k])
}
}
}
+// TestOpenAI_ChatKwargs_ReasoningDrivesAgentic pins the wiring: the request
+// body's top-level `reasoning` must reach AsyncChat as kwargs["reasoning"] so
+// resolveReasoningLevel can select the smart-reasoning agent. The three input
+// shapes cover what clients actually send; none may fail the unmarshal.
+func TestOpenAI_ChatKwargs_ReasoningDrivesAgentic(t *testing.T) {
+ bodies := []string{
+ `{"model":"model","messages":[{"role":"user","content":"q"}],"reasoning":5}`,
+ `{"model":"model","messages":[{"role":"user","content":"q"}],"reasoning":5.0}`,
+ `{"model":"model","messages":[{"role":"user","content":"q"}],"reasoning":"5"}`,
+ }
+ for _, body := range bodies {
+ var req OpenAIChatRequest
+ if err := json.Unmarshal([]byte(body), &req); err != nil {
+ t.Fatalf("unmarshal %s: %v", body, err)
+ }
+ kwargs := openAIChatKwargs(&req, false, "")
+ if got := resolveReasoningLevel(kwargs, nil); got != reasoningLevelAgentic {
+ t.Errorf("body %s: resolveReasoningLevel = %d, want %d", body, got, reasoningLevelAgentic)
+ }
+ if _, has := extractGenerationConfig(&req)["reasoning"]; has {
+ t.Errorf("body %s: reasoning must not enter the generation config", body)
+ }
+ }
+}
+
+// TestOpenAI_ChatKwargs_AbsentReasoningKeepsPromptConfigLevel pins the
+// fallback: a request without `reasoning` leaves the key out of the kwargs, so
+// a dialog pinned to the agentic level still gets it.
+func TestOpenAI_ChatKwargs_AbsentReasoningKeepsPromptConfigLevel(t *testing.T) {
+ var req OpenAIChatRequest
+ body := `{"model":"model","messages":[{"role":"user","content":"q"}],"reasoning":null}`
+ if err := json.Unmarshal([]byte(body), &req); err != nil {
+ t.Fatalf("unmarshal %s: %v", body, err)
+ }
+ kwargs := openAIChatKwargs(&req, true, "")
+ if _, has := kwargs["reasoning"]; has {
+ t.Fatalf("reasoning must be absent from kwargs, got %#v", kwargs)
+ }
+ if kwargs["quote"] != true {
+ t.Errorf("quote = %#v, want true", kwargs["quote"])
+ }
+ pinned := map[string]interface{}{"reasoning": float64(reasoningLevelAgentic)}
+ if got := resolveReasoningLevel(kwargs, pinned); got != reasoningLevelAgentic {
+ t.Errorf("resolveReasoningLevel = %d, want %d from prompt_config", got, reasoningLevelAgentic)
+ }
+}
+
// TestService_JoinNonEmpty_JoinsWithSeparator matches strings.Join
// semantics for non-empty inputs and skips empties.
func TestService_JoinNonEmpty_JoinsWithSeparator(t *testing.T) {