mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 04:59:37 +08:00
fix: leave the research module alone when it is not installed
The deferred-tool notes imported deeptutor.tools.research_tools on every turn that had local deferred tools, so a minimal install (setup tools deferred, no research module) failed the chat turn with ModuleNotFoundError. Import it only when the research module is enabled, as the rest of the pipeline already does. Refs #2972
This commit is contained in:
@@ -1224,15 +1224,21 @@ class AgenticLoopPipeline:
|
||||
loader = self._deferred_loader
|
||||
loaded = loader.loaded_names if loader is not None else set()
|
||||
notes: list[str] = list(getattr(self, "_eager_guides", []))
|
||||
from deeptutor.tools.research_tools import SCIENTIFIC_TOOL_NAMES, scientific_context
|
||||
from deeptutor.modules.catalog import module_enabled
|
||||
|
||||
research = [n for n in SCIENTIFIC_TOOL_NAMES if n in local_deferred and n not in loaded]
|
||||
if research and scientific_context(context.user_message):
|
||||
notes.append(
|
||||
self._t("notices.research_tools_hint").format(
|
||||
tools=", ".join(f"`{name}`" for name in research)
|
||||
# The research module may not be installed (a minimal install ships
|
||||
# without ``deeptutor.tools.research_tools``); then there is nothing to
|
||||
# point at, and the setup tools alone must not pull it in.
|
||||
if module_enabled("research"):
|
||||
from deeptutor.tools.research_tools import SCIENTIFIC_TOOL_NAMES, scientific_context
|
||||
|
||||
research = [n for n in SCIENTIFIC_TOOL_NAMES if n in local_deferred and n not in loaded]
|
||||
if research and scientific_context(context.user_message):
|
||||
notes.append(
|
||||
self._t("notices.research_tools_hint").format(
|
||||
tools=", ".join(f"`{name}`" for name in research)
|
||||
)
|
||||
)
|
||||
)
|
||||
from deeptutor.capabilities import all_loop_capabilities
|
||||
|
||||
for cap in all_loop_capabilities(context):
|
||||
|
||||
@@ -211,3 +211,34 @@ async def test_unchanged_toggles_give_byte_identical_tools(
|
||||
assert "reason" not in third["names"]
|
||||
fourth = await chat.turn("hi again", enabled_tools=["web_search"])
|
||||
assert json.dumps(fourth["tools"]) == json.dumps(third["tools"])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_turn_without_the_research_module_never_imports_it(
|
||||
_isolated_runtime: dict[str, bool], monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""A minimal install has no ``deeptutor.tools.research_tools``.
|
||||
|
||||
The setup tools alone are deferred there, and building the turn's notes for
|
||||
them must not reach for the research module.
|
||||
"""
|
||||
import sys
|
||||
|
||||
from deeptutor.modules import catalog
|
||||
|
||||
real_enabled = catalog.module_enabled
|
||||
|
||||
def enabled(name: str, *args: Any, **kwargs: Any) -> bool:
|
||||
return name != "research" and real_enabled(name, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(catalog, "module_enabled", enabled)
|
||||
# ``None`` in sys.modules makes any import of the module raise ImportError.
|
||||
monkeypatch.setitem(sys.modules, "deeptutor.tools.research_tools", None)
|
||||
|
||||
chat = _Conversation()
|
||||
first = await chat.turn("请帮我找一篇关于导数教学的论文")
|
||||
assert "load_tools" in first["names"]
|
||||
assert not set(SCIENTIFIC_TOOL_NAMES) & set(first["names"])
|
||||
# The setup tools are still offered on demand, the research hint is not.
|
||||
assert "inspect_setup" in first["manifest"]
|
||||
assert "preprint" not in first["note"]
|
||||
|
||||
Reference in New Issue
Block a user