Skip to content

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

Fix: apply dia.llm_id overrides (#19214)

Lynnff69dba5429f
diff --git a/api/apps/restful_apis/chat_api.py b/api/apps/restful_apis/chat_api.py
index 8da7920204..85bb49578d 100644
--- a/api/apps/restful_apis/chat_api.py
+++ b/api/apps/restful_apis/chat_api.py
@@ -1316,12 +1316,13 @@ async def session_completion(chat_id_in_arg=""):
dia.llm_id = chat_model_id
dia.tenant_llm_id = None
dia.llm_setting = chat_model_config
- elif not dia.llm_id:
- logging.info("empty chat_model_id in req, use default chat model.")
- _, tenant_info = TenantService.get_by_id(dia.tenant_id)
- if not tenant_info or not tenant_info.llm_id:
- raise LookupError("No default chat model for tenant.")
- dia.llm_id = tenant_info.llm_id
+ else:
+ if not dia.llm_id:
+ logging.info("empty chat_model_id in req, use default chat model.")
+ _, tenant_info = TenantService.get_by_id(dia.tenant_id)
+ if not tenant_info or not tenant_info.llm_id:
+ raise LookupError("No default chat model for tenant.")
+ dia.llm_id = tenant_info.llm_id
merge_generation_config(dia, chat_model_config)
legacy = _get_bool_request_flag(
diff --git a/test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py b/test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
index baa62fad59..484fa63789 100644
--- a/test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
+++ b/test/testcases/test_http_api/test_session_management/test_session_sdk_routes_unit.py
@@ -2663,6 +2663,143 @@ def test_session_completion_merges_generation_params_for_existing_chat(monkeypat
assert base_llm_setting == {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
+def test_session_completion_keeps_dialog_settings_without_overrides(monkeypatch):
+ module = _load_chat_api_module(monkeypatch)
+
+ base_llm_setting = {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
+ dia = SimpleNamespace(
+ id="chat-1",
+ tenant_id="tenant-1",
+ llm_id="model",
+ llm_setting=base_llm_setting,
+ prompt_config={"prologue": ""},
+ kb_ids=[],
+ )
+ conv = SimpleNamespace(
+ id="session-1",
+ dialog_id="chat-1",
+ message=[],
+ reference=[],
+ user_id="authenticated-user",
+ name="test",
+ )
+ conv.to_dict = lambda: {
+ "id": conv.id,
+ "dialog_id": conv.dialog_id,
+ "message": conv.message,
+ "reference": conv.reference,
+ "user_id": conv.user_id,
+ "name": conv.name,
+ }
+ captured = {}
+
+ async def _fake_async_chat(captured_dia, _messages, stream=True, **_kwargs):
+ captured["llm_setting"] = dict(captured_dia.llm_setting)
+ yield {"answer": "ok", "reference": {}}
+
+ monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, dia))
+ monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
+ monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
+ monkeypatch.setattr(module, "async_chat", _fake_async_chat)
+ monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
+ monkeypatch.setattr(
+ module,
+ "get_request_json",
+ lambda: _AwaitableValue(
+ {
+ "chat_id": "chat-1",
+ "session_id": "session-1",
+ "stream": False,
+ "messages": [{"role": "user", "content": "latest question"}],
+ }
+ ),
+ )
+
+ res = _run(inspect.unwrap(module.session_completion)())
+
+ assert res["code"] == 0, res
+ # No per-request generation params were sent, so the merge is a no-op and the
+ # dialog's stored settings must be passed through untouched.
+ assert captured["llm_setting"] == {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
+ assert base_llm_setting == {"temperature": 0.7, "top_p": 0.3, "custom": "keep"}
+
+
+def test_session_completion_assigns_default_model_then_merges_for_existing_chat(monkeypatch):
+ module = _load_chat_api_module(monkeypatch)
+
+ base_llm_setting = {"top_p": 0.3, "custom": "keep"}
+ dia = SimpleNamespace(
+ id="chat-1",
+ tenant_id="tenant-1",
+ llm_id="",
+ llm_setting=base_llm_setting,
+ prompt_config={"prologue": ""},
+ kb_ids=[],
+ )
+ conv = SimpleNamespace(
+ id="session-1",
+ dialog_id="chat-1",
+ message=[],
+ reference=[],
+ user_id="authenticated-user",
+ name="test",
+ )
+ conv.to_dict = lambda: {
+ "id": conv.id,
+ "dialog_id": conv.dialog_id,
+ "message": conv.message,
+ "reference": conv.reference,
+ "user_id": conv.user_id,
+ "name": conv.name,
+ }
+ captured = {}
+
+ async def _fake_async_chat(captured_dia, _messages, stream=True, **_kwargs):
+ captured["llm_id"] = captured_dia.llm_id
+ captured["llm_setting"] = dict(captured_dia.llm_setting)
+ yield {"answer": "ok", "reference": {}}
+
+ monkeypatch.setattr(module.DialogService, "get_by_id", lambda _dialog_id: (True, dia))
+ monkeypatch.setattr(module.ConversationService, "get_by_id", lambda _id: (True, conv))
+ monkeypatch.setattr(module.ConversationService, "update_by_id", lambda *_a, **_k: True, raising=False)
+ monkeypatch.setattr(
+ module.TenantService,
+ "get_by_id",
+ lambda _tenant_id: (True, SimpleNamespace(llm_id="tenant-default")),
+ )
+ monkeypatch.setattr(module, "async_chat", _fake_async_chat)
+ monkeypatch.setattr(module, "structure_answer", lambda _conv, ans, _message_id, _session_id: ans)
+ monkeypatch.setattr(
+ module,
+ "get_request_json",
+ lambda: _AwaitableValue(
+ {
+ "chat_id": "chat-1",
+ "session_id": "session-1",
+ "stream": False,
+ "messages": [{"role": "user", "content": "latest question"}],
+ "temperature": 0,
+ "presence_penalty": 0,
+ }
+ ),
+ )
+
+ res = _run(inspect.unwrap(module.session_completion)())
+
+ assert res["code"] == 0, res
+ # Dialog had no model yet: fall back to the tenant default and still merge the
+ # caller's per-request overrides onto the stored settings.
+ assert captured["llm_id"] == "tenant-default"
+ assert captured["llm_setting"] == {
+ "top_p": 0.3,
+ "custom": "keep",
+ "temperature": 0,
+ "presence_penalty": 0,
+ }
+
+
@pytest.mark.p2
def test_session_completion_can_use_submitted_full_history(monkeypatch):
"""The UI opt-in flag should preserve the previous full-history request behavior."""