mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 01:50:21 +08:00
fix(rag): report empty retrieval results (#1500)
This commit is contained in:
@@ -124,6 +124,11 @@ class RAGTool(_PromptHintsMixin, BaseTool):
|
||||
**extra_kwargs,
|
||||
)
|
||||
content = result.get("answer") or result.get("content", "")
|
||||
if not content and not result.get("error_type") and not result.get("sources"):
|
||||
content = (
|
||||
f"No matching content was found in knowledge base '{kb_name}'. "
|
||||
"The search completed successfully."
|
||||
)
|
||||
return ToolResult(
|
||||
content=content,
|
||||
sources=_rag_sources(result, query=query, kb_name=kb_name),
|
||||
|
||||
@@ -330,6 +330,47 @@ async def test_rag_tool_falls_back_to_query_echo(monkeypatch: pytest.MonkeyPatch
|
||||
assert result.sources == [{"type": "rag", "query": "what is a tensor", "kb_name": "demo-kb"}]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rag_tool_reports_successful_empty_retrieval(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""A no-hit search must remain visible instead of becoming empty tool output."""
|
||||
|
||||
async def fake_rag_search(**_kwargs: Any) -> dict[str, Any]:
|
||||
return {"answer": "", "content": "", "sources": [], "provider": "ima"}
|
||||
|
||||
_install_module(monkeypatch, "deeptutor.tools.rag_tool", rag_search=fake_rag_search)
|
||||
|
||||
result = await RAGTool().execute(query="what is a tensor", kb_name="demo-kb")
|
||||
|
||||
assert result.content == (
|
||||
"No matching content was found in knowledge base 'demo-kb'. "
|
||||
"The search completed successfully."
|
||||
)
|
||||
assert "error_type" not in result.metadata
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rag_tool_does_not_disguise_errors_as_empty_results(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
async def fake_rag_search(**_kwargs: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"answer": "",
|
||||
"content": "",
|
||||
"sources": [],
|
||||
"provider": "ima",
|
||||
"error_type": "retrieval_error",
|
||||
}
|
||||
|
||||
_install_module(monkeypatch, "deeptutor.tools.rag_tool", rag_search=fake_rag_search)
|
||||
|
||||
result = await RAGTool().execute(query="what is a tensor", kb_name="demo-kb")
|
||||
|
||||
assert result.content == ""
|
||||
assert result.metadata["error_type"] == "retrieval_error"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rag_tool_rejects_empty_query(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
called = False
|
||||
|
||||
Reference in New Issue
Block a user