Skip to content

Commit 8fb3691

Browse files
Allow agent_query to Use an Existing Search Index Without Rebuilding (#847)
Co-authored-by: James Braza <[email protected]>
1 parent 1bde36c commit 8fb3691

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

paperqa/agents/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async def run_agent(
120120
# Build the index once here, and then all tools won't need to rebuild it
121121
# only build if the a search tool is requested
122122
if PaperSearch.TOOL_FN_NAME in (settings.agent.tool_names or DEFAULT_TOOL_NAMES):
123-
await get_directory_index(settings=settings)
123+
await get_directory_index(settings=settings, build=settings.agent.rebuild_index)
124124

125125
if isinstance(agent_type, str) and agent_type.lower() == FAKE_AGENT_TYPE:
126126
session, agent_status = await run_fake_agent(

paperqa/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ class AgentSettings(BaseModel):
523523
)
524524
index: IndexSettings = Field(default_factory=IndexSettings)
525525

526+
rebuild_index: bool = Field(
527+
default=True,
528+
description=(
529+
"Flag to rebuild the index at the start of agent runners, default is True"
530+
" for CLI users to ensure all source PDFs are pulled in."
531+
),
532+
)
533+
526534
callbacks: Mapping[str, Sequence[Callable[[_EnvironmentState], Any]]] = Field(
527535
default_factory=dict,
528536
description="""

tests/test_agents.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,20 @@ async def test_search_pagination(agent_test_settings: Settings) -> None:
10801080
), "Second page should match second slice of all results"
10811081

10821082

1083+
@pytest.mark.asyncio
1084+
async def test_empty_index_without_index_rebuild(agent_test_settings: Settings):
1085+
"""Test that empty index and `rebuild_index=False` lead to a RuntimeError."""
1086+
agent_test_settings.agent = AgentSettings(index=IndexSettings()) # empty index
1087+
agent_test_settings.agent.rebuild_index = False
1088+
with pytest.raises(RuntimeError, match=r"Index .* was empty, please rebuild it."):
1089+
await agent_query(
1090+
query="Are COVID-19 vaccines effective?",
1091+
settings=agent_test_settings,
1092+
agent_type=FAKE_AGENT_TYPE,
1093+
force_index_rebuild=False,
1094+
)
1095+
1096+
10831097
class TestClinicalTrialSearchTool:
10841098
@pytest.mark.asyncio
10851099
async def test_continuation(self) -> None:

0 commit comments

Comments
 (0)