Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(rag): reject IMA title-only matches without readable text (#1500)

Bingxi Zhao (Frank)810487f81462
diff --git a/deeptutor/services/rag/pipelines/ima/pipeline.py b/deeptutor/services/rag/pipelines/ima/pipeline.py
index 7a30c8712..5a803a6dd 100644
--- a/deeptutor/services/rag/pipelines/ima/pipeline.py
+++ b/deeptutor/services/rag/pipelines/ima/pipeline.py
@@ -81,8 +81,23 @@ class ImaPipeline:
self.logger.error("IMA search failed for '%s': %s", kb_name, exc)
return self._error_result(query, exc, error_type="retrieval_error")
- sources = source_policy.documents_to_sources(page.documents)
- await self._hydrate(client, sources)
+ matched = source_policy.documents_to_sources(page.documents)
+ await self._hydrate(client, matched)
+ # A title match without a snippet or readable media is useful for
+ # hydration, but it is not evidence that the rag tool may cite (#1500).
+ sources = [source for source in matched if str(source.get("content") or "").strip()]
+ if matched and not sources:
+ return {
+ "query": query,
+ "answer": (
+ "Tencent IMA matched document titles, but returned no readable text. "
+ "Check access to the matched media or try again."
+ ),
+ "content": "",
+ "sources": [],
+ "provider": PROVIDER,
+ "error_type": "content_unavailable",
+ }
content = source_policy.render_context(sources)
return {
"query": query,
diff --git a/tests/services/rag/test_ima_pipeline.py b/tests/services/rag/test_ima_pipeline.py
index 7a842a159..46150fe73 100644
--- a/tests/services/rag/test_ima_pipeline.py
+++ b/tests/services/rag/test_ima_pipeline.py
@@ -679,7 +679,7 @@ class TestPipelineSearch:
# Snippets this substantial need no full-text top-up.
assert stub.media_calls == []
- def test_title_only_match_is_kept_without_a_snippet(self, tmp_path) -> None:
+ def test_title_only_match_without_readable_media_is_not_cited(self, tmp_path) -> None:
base = _kb_config(
tmp_path,
{"client_id": "cid", "api_key": "key", "knowledge_base_id": "kb-1"},
@@ -689,8 +689,28 @@ class TestPipelineSearch:
result = asyncio.run(pipeline.search("q", "IMA"))
- assert result["sources"][0]["content"] == ""
- assert result["content"].strip() == "[1] Alpha"
+ assert result["sources"] == []
+ assert result["content"] == ""
+ assert result["error_type"] == "content_unavailable"
+ assert "matched document titles" in result["answer"]
+
+ def test_unreadable_title_match_does_not_pollute_readable_sources(self, tmp_path) -> None:
+ base = _kb_config(
+ tmp_path,
+ {"client_id": "cid", "api_key": "key", "knowledge_base_id": "kb-1"},
+ )
+ stub = _SearchStub(
+ [
+ {"media_id": "unreadable", "title": "Title match"},
+ {"media_id": "readable", "title": "Passage", "highlight_content": _thick("text")},
+ ]
+ )
+ result = asyncio.run(
+ ImaPipeline(kb_base_dir=base, client_factory=lambda _c: stub).search("q", "IMA")
+ )
+
+ assert [source["title"] for source in result["sources"]] == ["Passage"]
+ assert "Title match" not in result["content"]
def test_title_only_match_loads_note_content(self, tmp_path) -> None:
base = _kb_config(
@@ -818,7 +838,7 @@ class TestPipelineSearch:
assert sorted(stub.media_calls) == sorted(f"m{i}" for i in range(DEFAULT_HYDRATION_BUDGET))
hydrated = [source for source in result["sources"] if source["content"]]
assert len(hydrated) == DEFAULT_HYDRATION_BUDGET
- assert result["sources"][-1]["content"] == ""
+ assert len(result["sources"]) == DEFAULT_HYDRATION_BUDGET
def test_downloaded_text_file_is_extracted(self, tmp_path) -> None:
base = _kb_config(