mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-10 21:55:42 +08:00
fix: align model default handling (#16782)
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
import asyncio
|
||||
import importlib.util
|
||||
import re
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
@@ -775,7 +776,7 @@ def _load_chat_routes_unit_module(monkeypatch):
|
||||
class _StubTenantService:
|
||||
@staticmethod
|
||||
def get_by_id(_tenant_id):
|
||||
return True, SimpleNamespace(llm_id="glm-4")
|
||||
return True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id")
|
||||
|
||||
@staticmethod
|
||||
def get_joined_tenants_by_user_id(_user_id):
|
||||
@@ -1142,7 +1143,7 @@ def test_chat_create_accepts_provider_scoped_rerank_id_unit(monkeypatch):
|
||||
"vector_similarity_weight": 0.25,
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4@CI@ZHIPU-AI")))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4@CI@ZHIPU-AI", tenant_llm_id="tenant-llm-id")))
|
||||
monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
|
||||
monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
|
||||
monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [_DummyKB()])
|
||||
@@ -1166,7 +1167,7 @@ def test_chat_create_accepts_provider_scoped_rerank_id_unit(monkeypatch):
|
||||
assert saved["rerank_id"] == "custom-reranker@OpenAI"
|
||||
assert {
|
||||
"tenant_id": "tenant-1",
|
||||
"model_name": "custom-reranker@OpenAI",
|
||||
"model_ref": "custom-reranker@OpenAI",
|
||||
"model_type": "rerank",
|
||||
} in query_calls
|
||||
|
||||
@@ -1176,7 +1177,7 @@ def test_chat_create_allows_default_knowledge_placeholder_without_sources_unit(m
|
||||
module = _load_chat_routes_unit_module(monkeypatch)
|
||||
saved = {}
|
||||
_set_route_unit_request_json(monkeypatch, module, {"name": "chat-a"})
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id")))
|
||||
monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
|
||||
monkeypatch.setattr(module, "get_api_key", lambda *_args, **_kwargs: SimpleNamespace(id=1))
|
||||
|
||||
@@ -1215,7 +1216,7 @@ def test_chat_create_uses_direct_chat_fields_unit(monkeypatch):
|
||||
"vector_similarity_weight": 0.25,
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id")))
|
||||
monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [])
|
||||
monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
|
||||
monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [_DummyKB()])
|
||||
@@ -1371,7 +1372,7 @@ def test_patch_chat_drops_response_only_fields_before_update_unit(monkeypatch):
|
||||
_set_route_unit_request_json(monkeypatch, module, payload)
|
||||
monkeypatch.setattr(module.DialogService, "query", lambda **kwargs: [] if "name" in kwargs else [SimpleNamespace(id="chat-1")])
|
||||
monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(existing)))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id")))
|
||||
monkeypatch.setattr(module.KnowledgebaseService, "accessible", lambda **_kwargs: [SimpleNamespace(id="kb-1")])
|
||||
monkeypatch.setattr(module.KnowledgebaseService, "query", lambda **_kwargs: [_DummyKB()])
|
||||
monkeypatch.setattr(module, "get_api_key", lambda *args, **kwargs: SimpleNamespace(id=1))
|
||||
@@ -1399,7 +1400,7 @@ def test_patch_chat_merges_prompt_and_llm_settings_unit(monkeypatch):
|
||||
)
|
||||
monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [SimpleNamespace(id="chat-1")])
|
||||
monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(existing)))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id")))
|
||||
|
||||
def _update(_chat_id, payload):
|
||||
updated.update(payload)
|
||||
@@ -1442,7 +1443,7 @@ def test_update_chat_allows_knowledge_placeholder_without_sources_unit(monkeypat
|
||||
)
|
||||
monkeypatch.setattr(module.DialogService, "query", lambda **_kwargs: [SimpleNamespace(id="chat-1")])
|
||||
monkeypatch.setattr(module.DialogService, "get_by_id", lambda _id: (True, _DummyDialogRecord(existing)))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4")))
|
||||
monkeypatch.setattr(module.TenantService, "get_by_id", lambda _tid: (True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id")))
|
||||
updated = {}
|
||||
|
||||
def _update(_chat_id, payload):
|
||||
@@ -1540,7 +1541,15 @@ def test_chat_create_llm_contract(rest_client, clear_chats, ensure_parsed_docume
|
||||
body = res.json()
|
||||
assert body["code"] == expected_code, (scenario_name, body)
|
||||
if expected_code == 0:
|
||||
assert body["data"]["llm_id"] == expected_llm_id, (scenario_name, body)
|
||||
actual_llm_id = body["data"]["llm_id"]
|
||||
tenant_llm_id = body["data"].get("tenant_llm_id")
|
||||
if expected_llm_id == "glm-4-flash@CI@ZHIPU-AI":
|
||||
if tenant_llm_id:
|
||||
assert actual_llm_id == tenant_llm_id, (scenario_name, body)
|
||||
else:
|
||||
assert re.fullmatch(r"[0-9a-f]{32}", actual_llm_id), (scenario_name, body)
|
||||
else:
|
||||
assert actual_llm_id == expected_llm_id, (scenario_name, body)
|
||||
assert body["data"]["llm_setting"] == expected_llm_setting, (scenario_name, body)
|
||||
else:
|
||||
assert body["message"] == expected_message, (scenario_name, body)
|
||||
@@ -1819,7 +1828,15 @@ def test_chat_update_llm_contract(rest_client, clear_chats, ensure_parsed_docume
|
||||
get_payload = get_res.json()
|
||||
assert get_payload["code"] == 0, (scenario_name, get_payload)
|
||||
assert get_payload["data"]["name"] == updated_name, (scenario_name, get_payload)
|
||||
assert get_payload["data"]["llm_id"] == expected_llm_id, (scenario_name, get_payload)
|
||||
actual_llm_id = get_payload["data"]["llm_id"]
|
||||
tenant_llm_id = get_payload["data"].get("tenant_llm_id")
|
||||
if expected_llm_id == "glm-4-flash@CI@ZHIPU-AI":
|
||||
if tenant_llm_id:
|
||||
assert actual_llm_id == tenant_llm_id, (scenario_name, get_payload)
|
||||
else:
|
||||
assert re.fullmatch(r"[0-9a-f]{32}", actual_llm_id), (scenario_name, get_payload)
|
||||
else:
|
||||
assert actual_llm_id == expected_llm_id, (scenario_name, get_payload)
|
||||
assert get_payload["data"]["llm_setting"] == expected_llm_setting, (scenario_name, get_payload)
|
||||
else:
|
||||
assert body["message"] == expected_message, (scenario_name, body)
|
||||
|
||||
@@ -832,7 +832,7 @@ def test_dataset_update_embedding_model_invalid_and_none_contract(rest_client, c
|
||||
assert list_res.status_code == 200
|
||||
list_payload = list_res.json()
|
||||
assert list_payload["code"] == 0, list_payload
|
||||
assert list_payload["data"][0]["embedding_model"] == "BAAI/bge-small-en-v1.5@Local@Builtin", list_payload
|
||||
assert list_payload["data"][0]["embedding_model"].startswith("BAAI/bge-small-en-v1.5"), list_payload
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
@@ -1197,7 +1197,10 @@ def test_dataset_create_embedding_model_contract(rest_client, clear_datasets, na
|
||||
pytest.xfail(f"Environment has no authorized tenant model for {embedding_model}: {payload}")
|
||||
assert payload["code"] == expected_code, payload
|
||||
if expected_embedding_model is not None:
|
||||
assert payload["data"]["embedding_model"] == expected_embedding_model, payload
|
||||
if name in {"embedding_model_unset", "embedding_model_none"}:
|
||||
assert payload["data"]["embedding_model"].startswith("BAAI/bge-small-en-v1.5"), payload
|
||||
else:
|
||||
assert payload["data"]["embedding_model"] == expected_embedding_model, payload
|
||||
if expected_message is not None:
|
||||
assert payload["message"] == expected_message, payload
|
||||
|
||||
|
||||
@@ -1532,7 +1532,7 @@ def _load_chat_routes_unit_module(monkeypatch):
|
||||
"TenantService",
|
||||
(),
|
||||
{
|
||||
"get_by_id": staticmethod(lambda _tenant_id: (True, SimpleNamespace(llm_id="glm-4"))),
|
||||
"get_by_id": staticmethod(lambda _tenant_id: (True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id"))),
|
||||
"get_joined_tenants_by_user_id": staticmethod(lambda _user_id: [{"tenant_id": "tenant-1"}, {"tenant_id": "team-tenant-2"}]),
|
||||
},
|
||||
)
|
||||
@@ -1599,7 +1599,7 @@ def test_create_chat_uses_tenant_default_llm_when_llm_id_is_null_unit(monkeypatc
|
||||
|
||||
res = _run(module.create.__wrapped__())
|
||||
assert res["code"] == 0
|
||||
assert saved["llm_id"] == "glm-4"
|
||||
assert saved["llm_id"] == "tenant-llm-id"
|
||||
assert saved["llm_setting"]["temperature"] == 0.8
|
||||
|
||||
|
||||
|
||||
@@ -237,13 +237,13 @@ class TestDatasetCreate:
|
||||
def test_embedding_model_unset(self, client):
|
||||
payload = {"name": "embedding_model_unset"}
|
||||
dataset = client.create_dataset(**payload)
|
||||
assert dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(dataset)
|
||||
assert dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(dataset)
|
||||
|
||||
@pytest.mark.p2
|
||||
def test_embedding_model_none(self, client):
|
||||
payload = {"name": "embedding_model_none", "embedding_model": None}
|
||||
dataset = client.create_dataset(**payload)
|
||||
assert dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(dataset)
|
||||
assert dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(dataset)
|
||||
|
||||
@pytest.mark.p2
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -229,10 +229,10 @@ class TestDatasetUpdate:
|
||||
def test_embedding_model_none(self, client, add_dataset_func):
|
||||
dataset = add_dataset_func
|
||||
dataset.update({"embedding_model": None})
|
||||
assert dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(dataset)
|
||||
assert dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(dataset)
|
||||
|
||||
retrieved_dataset = client.get_dataset(name=dataset.name)
|
||||
assert retrieved_dataset.embedding_model == "BAAI/bge-small-en-v1.5@Local@Builtin", str(retrieved_dataset)
|
||||
assert retrieved_dataset.embedding_model.split("@", 1)[0] == "BAAI/bge-small-en-v1.5", str(retrieved_dataset)
|
||||
|
||||
@pytest.mark.p2
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from enum import IntEnum
|
||||
from types import ModuleType, SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
@@ -26,6 +27,16 @@ import pytest
|
||||
pytestmark = pytest.mark.p2
|
||||
|
||||
|
||||
class _StubModelTypeBinary(IntEnum):
|
||||
CHAT = 1
|
||||
EMBEDDING = 2
|
||||
SPEECH2TEXT = 4
|
||||
IMAGE2TEXT = 8
|
||||
RERANK = 16
|
||||
TTS = 32
|
||||
OCR = 64
|
||||
|
||||
|
||||
def _stub(monkeypatch, name, **attrs):
|
||||
mod = ModuleType(name)
|
||||
for key, value in attrs.items():
|
||||
@@ -122,6 +133,8 @@ def _load_delete_datasets_module(monkeypatch, *, f2d_rows, file_filter_delete):
|
||||
_stub(
|
||||
monkeypatch,
|
||||
"api.db.db_models",
|
||||
DB=SimpleNamespace(connection_context=lambda: lambda func: func),
|
||||
TenantModel=SimpleNamespace(),
|
||||
File=SimpleNamespace(source_type="source_type", id="id", type="type", name="name"),
|
||||
)
|
||||
_stub(
|
||||
@@ -130,8 +143,18 @@ def _load_delete_datasets_module(monkeypatch, *, f2d_rows, file_filter_delete):
|
||||
PAGERANK_FLD="pagerank",
|
||||
TAG_FLD="tag",
|
||||
FileSource=SimpleNamespace(KNOWLEDGEBASE="knowledgebase"),
|
||||
PipelineTaskType=SimpleNamespace(
|
||||
PARSE="parse",
|
||||
DOWNLOAD="download",
|
||||
RAPTOR="raptor",
|
||||
GRAPH_RAG="graph_rag",
|
||||
MINDMAP="mindmap",
|
||||
ARTIFACT="artifact",
|
||||
SKILL="skill",
|
||||
),
|
||||
StatusEnum=SimpleNamespace(),
|
||||
LLMType=SimpleNamespace(),
|
||||
ModelTypeBinary=_StubModelTypeBinary,
|
||||
)
|
||||
_stub(
|
||||
monkeypatch,
|
||||
|
||||
@@ -25,6 +25,7 @@ import sys
|
||||
from pathlib import Path
|
||||
from enum import IntEnum
|
||||
from types import ModuleType, SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -141,6 +142,7 @@ def _load_module(monkeypatch, *, tenant_model_records, factory_llm_infos=None):
|
||||
ensure_mineru_from_env=lambda *a, **kw: None,
|
||||
ensure_paddleocr_from_env=lambda *a, **kw: None,
|
||||
ensure_opendataloader_from_env=lambda *a, **kw: None,
|
||||
resolve_model_id=MagicMock(),
|
||||
)
|
||||
_stub(
|
||||
monkeypatch,
|
||||
|
||||
Reference in New Issue
Block a user