mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 13:09:57 +08:00
* feat(rag): support local rerankers for LightRAG queries Allow administrators to select a cross-encoder alongside the native LightRAG role models. Run scoring in a reusable worker and partition cached answers by reranker so model changes take effect on existing indexes. Refs #2701 * fix(api): refresh the router index for LightRAG settings * fix(rag): run LightRAG reranking in the shared reranker worker anyio's process pool re-runs the server's __main__ file by path. When the backend runs as `python -m deeptutor_cli.main serve` (the desktop app), that file's relative imports fail, so every rerank call failed with 'Error during worker process initialization' and LightRAG silently kept the original chunk order. Reuse the python -m reranker worker that the LlamaIndex pipeline already uses. --------- Co-authored-by: lipeih <lipeihan@eazytec.com> Co-authored-by: zszz3 <zszz3@users.noreply.github.com>
227 lines
8.7 KiB
Python
227 lines
8.7 KiB
Python
"""Settings are wired directly to the pinned native LightRAG constructor."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import types
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services.rag.pipelines.lightrag import engine, roles
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
importlib.util.find_spec("lightrag") is None,
|
|
reason="requires the optional rag-lightrag extra",
|
|
)
|
|
|
|
|
|
class _NativeLightRag:
|
|
def __init__(self, **kwargs) -> None:
|
|
self.kwargs = kwargs
|
|
|
|
|
|
def _stub_build(monkeypatch) -> None:
|
|
query_config = types.SimpleNamespace(binding="openai")
|
|
monkeypatch.setattr(engine, "_require_exact_version", lambda: None)
|
|
monkeypatch.setattr(engine, "_register_parser", lambda: None)
|
|
monkeypatch.setattr(engine, "_controlled_class", lambda: _NativeLightRag)
|
|
monkeypatch.setattr(engine, "build_llm_model_func", lambda **_kwargs: "llm")
|
|
monkeypatch.setattr(engine, "build_embedding_func", lambda **_kwargs: "embedding")
|
|
monkeypatch.setattr(
|
|
roles,
|
|
"resolve_query_roles",
|
|
lambda: {role: roles.RoleCall(query_config, None, 4, 240) for role in ("keyword", "query")},
|
|
)
|
|
monkeypatch.setattr(
|
|
"deeptutor.services.rag.pipelines.lightrag.indexing_policy.cache_identity_for_config",
|
|
lambda _config: "query-fingerprint",
|
|
)
|
|
|
|
|
|
def test_native_constructor_receives_every_supported_knob(monkeypatch, tmp_path: Path) -> None:
|
|
_stub_build(monkeypatch)
|
|
monkeypatch.setattr(
|
|
engine, "indexing_kwargs_from_settings", lambda: {"max_parallel_parse_native": 4}
|
|
)
|
|
monkeypatch.setattr(
|
|
engine,
|
|
"constructor_kwargs_from_settings",
|
|
lambda: {
|
|
"llm_model_max_async": 8,
|
|
"entity_extract_max_gleaning": 2,
|
|
"default_llm_timeout": 480,
|
|
},
|
|
)
|
|
|
|
rag = engine.build_rag(tmp_path)
|
|
|
|
assert rag.kwargs["working_dir"] == str(tmp_path)
|
|
assert rag.kwargs["workspace"] == engine.workspace_for(tmp_path)
|
|
assert rag.kwargs["llm_model_func"] == "llm"
|
|
assert rag.kwargs["embedding_func"] == "embedding"
|
|
assert rag.kwargs["auto_manage_storages_states"] is False
|
|
assert rag.kwargs["max_parallel_parse_native"] == 4
|
|
assert rag.kwargs["llm_model_max_async"] == 8
|
|
assert rag.kwargs["entity_extract_max_gleaning"] == 2
|
|
assert rag.kwargs["default_llm_timeout"] == 480
|
|
assert rag.kwargs["vlm_process_enable"] is False
|
|
assert rag.kwargs["llm_model_name"] == "query-fingerprint"
|
|
assert set(rag.kwargs["role_llm_configs"]) == {"keyword", "query"}
|
|
|
|
|
|
def test_global_dedicated_selection_drives_query_roles_not_embedding(
|
|
monkeypatch, tmp_path: Path
|
|
) -> None:
|
|
_stub_build(monkeypatch)
|
|
query_config = types.SimpleNamespace(binding="dedicated")
|
|
llm_calls: list[dict[str, object]] = []
|
|
embedding_calls: list[dict[str, object]] = []
|
|
|
|
def build_llm(**kwargs):
|
|
llm_calls.append(kwargs)
|
|
return "llm"
|
|
|
|
def build_embedding(**kwargs):
|
|
embedding_calls.append(kwargs)
|
|
return "embedding"
|
|
|
|
monkeypatch.setattr(engine, "build_llm_model_func", build_llm)
|
|
monkeypatch.setattr(engine, "build_embedding_func", build_embedding)
|
|
monkeypatch.setattr(
|
|
roles,
|
|
"resolve_query_roles",
|
|
lambda: {role: roles.RoleCall(query_config, None, 4, 240) for role in ("keyword", "query")},
|
|
)
|
|
monkeypatch.setattr(engine, "indexing_kwargs_from_settings", dict)
|
|
monkeypatch.setattr(engine, "constructor_kwargs_from_settings", dict)
|
|
|
|
engine.build_rag(tmp_path)
|
|
|
|
assert llm_calls == [{"llm_config": query_config, "owner": None}] * 3
|
|
assert embedding_calls == [{}]
|
|
|
|
|
|
def test_query_reranks_chunks_by_index_and_partitions_cached_answers(monkeypatch, tmp_path):
|
|
from lightrag.utils import apply_rerank_if_enabled
|
|
|
|
from deeptutor.services.rag.pipelines.lightrag import rerank
|
|
|
|
_stub_build(monkeypatch)
|
|
settings = {}
|
|
monkeypatch.setattr("deeptutor.services.config.load_lightrag_settings", lambda: settings)
|
|
disabled = engine.build_rag(tmp_path).kwargs
|
|
assert "rerank_model_func" not in disabled
|
|
|
|
# The shared ``python -m`` reranker worker returns raw cross-encoder logits.
|
|
score = Mock(return_value=[-2.0, 2.2])
|
|
monkeypatch.setattr(rerank, "_score_in_worker", score)
|
|
settings["reranker_model"] = "first-model"
|
|
first = engine.build_rag(tmp_path).kwargs
|
|
chunks = [{"chunk_id": "a", "content": "same"}, {"chunk_id": "b", "content": "same"}]
|
|
ranked = asyncio.run(apply_rerank_if_enabled("query", chunks, first, top_n=1))
|
|
assert [row["chunk_id"] for row in ranked] == ["b"]
|
|
assert ranked[0]["rerank_score"] == pytest.approx(rerank._sigmoid(2.2))
|
|
score.assert_called_once_with("first-model", [("query", "same"), ("query", "same")])
|
|
|
|
settings["reranker_model"] = "second-model"
|
|
second = engine.build_rag(tmp_path).kwargs
|
|
assert (
|
|
first["role_llm_configs"]["query"].metadata != second["role_llm_configs"]["query"].metadata
|
|
)
|
|
assert (
|
|
first["role_llm_configs"]["keyword"].metadata
|
|
== second["role_llm_configs"]["keyword"].metadata
|
|
)
|
|
# An unavailable model keeps LightRAG's original chunk order.
|
|
score.return_value = None
|
|
assert asyncio.run(apply_rerank_if_enabled("query", chunks, second, top_n=1)) == chunks
|
|
score.assert_called_with("second-model", [("query", "same"), ("query", "same")])
|
|
|
|
|
|
def test_constructor_forwards_captured_embedding_config(monkeypatch, tmp_path: Path) -> None:
|
|
from deeptutor.services.embedding.config import EmbeddingConfig
|
|
|
|
_stub_build(monkeypatch)
|
|
captured = []
|
|
embedding = EmbeddingConfig(model="accepted", dim=3, api_key="test-key")
|
|
|
|
def build_embedding(**kwargs):
|
|
captured.append(kwargs)
|
|
return "embedding"
|
|
|
|
monkeypatch.setattr(engine, "build_embedding_func", build_embedding)
|
|
engine.build_rag(tmp_path, embedding_config=embedding)
|
|
assert captured == [{"embedding_config": embedding}]
|
|
|
|
|
|
def test_vlm_role_is_only_configured_when_enabled(monkeypatch, tmp_path: Path) -> None:
|
|
_stub_build(monkeypatch)
|
|
monkeypatch.setattr(engine, "build_vision_model_func", lambda **_kwargs: "vision")
|
|
monkeypatch.setattr(engine, "indexing_kwargs_from_settings", dict)
|
|
monkeypatch.setattr(engine, "constructor_kwargs_from_settings", dict)
|
|
|
|
snapshot = types.SimpleNamespace(
|
|
config=types.SimpleNamespace(binding="openai"),
|
|
owner=object(),
|
|
descriptor={"endpoint": "https://example.test/v1"},
|
|
)
|
|
monkeypatch.setattr(
|
|
"deeptutor.services.rag.pipelines.lightrag.indexing_policy.cache_identity",
|
|
lambda _snapshot: "snapshot-fingerprint",
|
|
)
|
|
|
|
pair = types.SimpleNamespace(extract=snapshot, vlm=snapshot, limits={}, embedding_config=None)
|
|
rag = engine.build_rag(tmp_path, enable_vlm=True, indexing_snapshot=pair)
|
|
|
|
role = rag.kwargs["role_llm_configs"]["vlm"]
|
|
assert role.func == "vision"
|
|
assert rag.kwargs["vlm_process_enable"] is True
|
|
|
|
|
|
def test_indexing_uses_frozen_roles_without_resolving_query_models(
|
|
monkeypatch, tmp_path: Path
|
|
) -> None:
|
|
_stub_build(monkeypatch)
|
|
monkeypatch.setattr(
|
|
"deeptutor.services.config.load_lightrag_settings", lambda: {"reranker_model": "query-only"}
|
|
)
|
|
monkeypatch.setattr(engine, "indexing_kwargs_from_settings", dict)
|
|
monkeypatch.setattr(engine, "constructor_kwargs_from_settings", dict)
|
|
llm_calls: list[dict[str, object]] = []
|
|
vision_calls: list[dict[str, object]] = []
|
|
|
|
def build_llm(**kwargs):
|
|
llm_calls.append(kwargs)
|
|
return f"llm-{len(llm_calls)}"
|
|
|
|
def build_vision(**kwargs):
|
|
vision_calls.append(kwargs)
|
|
return "vision"
|
|
|
|
monkeypatch.setattr(engine, "build_llm_model_func", build_llm)
|
|
monkeypatch.setattr(engine, "build_vision_model_func", build_vision)
|
|
snapshot = types.SimpleNamespace(
|
|
config=types.SimpleNamespace(binding="openai"),
|
|
owner=object(),
|
|
descriptor={"endpoint": "https://example.test/v1"},
|
|
)
|
|
monkeypatch.setattr(
|
|
"deeptutor.services.rag.pipelines.lightrag.indexing_policy.cache_identity",
|
|
lambda _snapshot: "snapshot-fingerprint",
|
|
)
|
|
|
|
pair = types.SimpleNamespace(extract=snapshot, vlm=snapshot, limits={}, embedding_config=None)
|
|
rag = engine.build_rag(tmp_path, enable_vlm=True, indexing_snapshot=pair)
|
|
|
|
assert "llm_config" in llm_calls[0]
|
|
assert llm_calls[1] == {"llm_config": snapshot.config, "owner": snapshot.owner}
|
|
assert vision_calls == [{"llm_config": snapshot.config, "owner": snapshot.owner}]
|
|
assert rag.kwargs["llm_model_func"] == "llm-2"
|
|
assert "rerank_model_func" not in rag.kwargs
|
|
assert rag.kwargs["role_llm_configs"]["extract"].func == "llm-1"
|
|
assert rag.kwargs["role_llm_configs"]["vlm"].func == "vision"
|
|
assert set(rag.kwargs["role_llm_configs"]) == {"extract", "vlm"}
|