mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-07 04:38:18 +08:00
fix: retain inferred source page inspection
This commit is contained in:
@@ -25,6 +25,12 @@ _FIGURE = re.compile(r"(?:fig(?:ure)?\.?|图|圖|table|表)\s*([0-9]+(?:[.\-][0-
|
||||
_PAGE = re.compile(r"(?:page|p\.|第)\s*(\d+)\s*(?:页|頁)?", re.I)
|
||||
|
||||
|
||||
def page_reference(text: str) -> int | None:
|
||||
"""Return the first page reference recognized by visual inspection."""
|
||||
match = _PAGE.search(text)
|
||||
return int(match.group(1)) if match else None
|
||||
|
||||
|
||||
def figure_labels(text: str) -> list[str]:
|
||||
return list(dict.fromkeys(m.group(1).casefold() for m in _FIGURE.finditer(text)))
|
||||
|
||||
@@ -232,8 +238,7 @@ def retrieve_visual(
|
||||
error="multiple_references",
|
||||
)
|
||||
label = labels[0] if labels else ""
|
||||
match = _PAGE.search(query)
|
||||
page = int(match.group(1)) if match else None
|
||||
page = page_reference(query)
|
||||
candidates = [
|
||||
r
|
||||
for key, r in records.items()
|
||||
|
||||
@@ -301,7 +301,11 @@ class RAGTool(_PromptHintsMixin, BaseTool):
|
||||
raise ValueError("RAG requires an explicit kb_name.")
|
||||
event_sink = None if kwargs.get("_mastery_source_context") else kwargs.get("event_sink")
|
||||
vision_supported = bool(kwargs.get("_vision_supported", False))
|
||||
from deeptutor.services.rag.source_visuals import figure_labels, retrieve_visual
|
||||
from deeptutor.services.rag.source_visuals import (
|
||||
figure_labels,
|
||||
page_reference,
|
||||
retrieve_visual,
|
||||
)
|
||||
|
||||
exact_keys = ("source_path", "asset_id", "figure", "page", "region", "source_hash")
|
||||
selectors = _normalize_rag_selectors(kwargs)
|
||||
@@ -323,7 +327,11 @@ class RAGTool(_PromptHintsMixin, BaseTool):
|
||||
return ToolResult(
|
||||
content=f"Knowledge base '{kb_name}' is not accessible.", success=False
|
||||
)
|
||||
if selectors.get("source_path") and not visual_requested:
|
||||
if (
|
||||
selectors.get("source_path")
|
||||
and not visual_requested
|
||||
and page_reference(query) is None
|
||||
):
|
||||
return ToolResult(
|
||||
content=(
|
||||
"RAG does not support scoped text retrieval by source_path. "
|
||||
|
||||
@@ -197,3 +197,24 @@ async def test_source_only_scope_keeps_knowledge_base_access_check(kb, rag_calls
|
||||
assert "not accessible" in result.content
|
||||
assert result.sources == []
|
||||
assert rag_calls["rag_search"] == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("query", ["Inspect page 1", "检查第1页", "Inspect p. 1"])
|
||||
@pytest.mark.asyncio
|
||||
async def test_source_with_query_page_keeps_visual_inspection(kb, rag_calls, query):
|
||||
result = await RAGTool().execute(
|
||||
query=query,
|
||||
kb_name=kb.name,
|
||||
source_path="manual.pdf",
|
||||
_vision_supported=True,
|
||||
)
|
||||
assert result.success
|
||||
assert result.metadata["evidence_kind"] == "exact_source_visual"
|
||||
assert result.model_message["content"][1]["type"] == "image_url"
|
||||
assert "pump seal" in result.content
|
||||
assert rag_calls["rag_search"] == []
|
||||
|
||||
unscoped = await RAGTool().execute(query=query, kb_name=kb.name)
|
||||
assert unscoped.success
|
||||
assert unscoped.content == "semantic answer"
|
||||
assert len(rag_calls["rag_search"]) == 1
|
||||
|
||||
Reference in New Issue
Block a user