Skip to content

Commit

Permalink
Turned off rebuilding of the index every paper search
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbraza committed Oct 1, 2024
1 parent f6fe152 commit fb2ea23
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions paperqa/agents/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ def make_initial_state_and_tools(self) -> tuple[EnvironmentState, list[Tool]]:
return self.state, self.tools

async def reset(self) -> tuple[list[Message], list[Tool]]:
# NOTE: don't build the index here, as sometimes we asyncio.gather over this
# method, and our current design (as of v5.0.10) could hit race conditions
# because index building does not use file locks
self._docs.clear_docs()
self.state, self.tools = self.make_initial_state_and_tools()
return (
Expand Down
4 changes: 3 additions & 1 deletion paperqa/agents/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .env import PaperQAEnvironment
from .helpers import litellm_get_search_query, table_formatter
from .models import AgentStatus, AnswerResponse, QueryRequest, SimpleProfiler
from .search import SearchDocumentStorage, SearchIndex
from .search import SearchDocumentStorage, SearchIndex, get_directory_index
from .tools import EnvironmentState, GatherEvidence, GenerateAnswer, PaperSearch

if TYPE_CHECKING:
Expand Down Expand Up @@ -106,6 +106,8 @@ async def run_agent(
f" query {query.model_dump()}."
)

# Build the index once here, and then all tools won't need to rebuild it
await get_directory_index(settings=query.settings)
if isinstance(agent_type, str) and agent_type.lower() == FAKE_AGENT_TYPE:
answer, agent_status = await run_fake_agent(query, docs, **runner_kwargs)
elif tool_selector_or_none := query.settings.make_aviary_tool_selector(agent_type):
Expand Down
2 changes: 1 addition & 1 deletion paperqa/agents/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def paper_search(
offset = self.previous_searches[search_key] = 0

logger.info(f"Starting paper search for {query!r}.")
index = await get_directory_index(settings=self.settings)
index = await get_directory_index(settings=self.settings, build=False)
results: list[Docs] = await index.query(
query,
top_n=self.settings.agent.search_count,
Expand Down

0 comments on commit fb2ea23

Please sign in to comment.