Skip to content

Commit 96f7308

Browse files
authored
Adding EmptyDocsError failover to gen_answer (#1073)
1 parent d8af3ac commit 96f7308

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/paperqa/agents/tools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ async def gen_answer(self, state: EnvironmentState) -> str:
309309
Args:
310310
state: Current state.
311311
"""
312+
if not state.docs.docs:
313+
raise EmptyDocsError("Not generating an answer due to having no papers.")
314+
312315
logger.info(f"Generating answer for '{state.session.question}'.")
313316

314317
if f"{self.TOOL_FN_NAME}_initialized" in self.settings.agent.callbacks:

src/paperqa/docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ async def aquery(
742742
contexts = session.contexts
743743
if answer_config.get_evidence_if_no_contexts and not contexts:
744744
session = await self.aget_evidence(
745-
session,
745+
query=session,
746746
callbacks=callbacks,
747747
settings=settings,
748748
embedding_model=embedding_model,

tests/test_agents.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from functools import wraps
1313
from pathlib import Path
1414
from typing import cast
15-
from unittest.mock import AsyncMock, patch
15+
from unittest.mock import AsyncMock, MagicMock, patch
1616
from uuid import uuid4
1717

1818
import ldp.agent
@@ -467,15 +467,27 @@ async def llm_model_call(*args, **kwargs):
467467
@pytest.mark.asyncio
468468
async def test_timeout(agent_test_settings: Settings, agent_type: str | type) -> None:
469469
agent_test_settings.prompts.pre = None
470-
agent_test_settings.agent.timeout = 0.001
470+
agent_test_settings.agent.timeout = 0.05 # Give time for Environment.reset()
471471
agent_test_settings.llm = "gpt-4o-mini"
472472
agent_test_settings.agent.tool_names = {"gen_answer", "complete"}
473-
response = await agent_query(
474-
query="Are COVID-19 vaccines effective?",
475-
settings=agent_test_settings,
476-
agent_type=agent_type,
477-
)
478-
# ensure that GenerateAnswerTool was called
473+
docs = Docs()
474+
475+
async def custom_aget_evidence(*_, **kwargs) -> PQASession: # noqa: RUF029
476+
return kwargs["query"]
477+
478+
with (
479+
patch.object(docs, "docs", {"stub_key": MagicMock(spec_set=Doc)}),
480+
patch.multiple(
481+
Docs, clear_docs=MagicMock(), aget_evidence=custom_aget_evidence
482+
),
483+
):
484+
response = await agent_query(
485+
query="Are COVID-19 vaccines effective?",
486+
settings=agent_test_settings,
487+
docs=docs,
488+
agent_type=agent_type,
489+
)
490+
# Ensure that GenerateAnswerTool was called in truncation's failover
479491
assert response.status == AgentStatus.TRUNCATED, "Agent did not timeout"
480492
assert CANNOT_ANSWER_PHRASE in response.session.answer
481493

0 commit comments

Comments
 (0)