Skip to content

Commit

Permalink
Expose more contexts as output from gather_evidence tool (#793)
Browse files Browse the repository at this point in the history
Co-authored-by: James Braza <[email protected]>
  • Loading branch information
mskarlin and jamesbraza authored Jan 7, 2025
1 parent 13a38c3 commit abeddbc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
14 changes: 10 additions & 4 deletions paperqa/agents/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,18 @@ async def gather_evidence(self, question: str, state: EnvironmentState) -> str:
sorted_contexts = sorted(
state.session.contexts, key=lambda x: x.score, reverse=True
)
best_evidence = (
f" Best evidence:\n\n{sorted_contexts[0].context}"
if sorted_contexts
else ""

top_contexts = "\n".join(
[
f"{n + 1}. {sc.context}\n"
for n, sc in enumerate(
sorted_contexts[: self.settings.agent.agent_evidence_n]
)
]
)

best_evidence = f" Best evidence(s):\n\n{top_contexts}" if top_contexts else ""

if f"{self.TOOL_FN_NAME}_completed" in self.settings.agent.callbacks:
await asyncio.gather(
*(
Expand Down
6 changes: 6 additions & 0 deletions paperqa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ class AgentSettings(BaseModel):
)
search_count: int = 8
wipe_context_on_answer_failure: bool = True
agent_evidence_n: int = Field(
default=1,
ge=1,
description="Top n ranked evidences shown to the "
"agent after the GatherEvidence tool.",
)
timeout: float = Field(
default=500.0,
description=(
Expand Down
20 changes: 19 additions & 1 deletion tests/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,30 @@ def new_status(state: EnvironmentState) -> str:
summary_llm_model=summary_llm_model,
embedding_model=embedding_model,
)
await gather_evidence_tool.gather_evidence(session.question, state=env_state)

response = await gather_evidence_tool.gather_evidence(
session.question, state=env_state
)

if callback_type == "async":
gather_evidence_initialized_callback.assert_awaited_once_with(env_state)
gather_evidence_completed_callback.assert_awaited_once_with(env_state)

# ensure 1 piece of top evidence is returned
assert "\n1." in response, "gather_evidence did not return any results"
assert (
"\n2." not in response
), "gather_evidence should return only 1 context, not 2"

# now adjust to give the agent 2x pieces of evidence
gather_evidence_tool.settings.agent.agent_evidence_n = 2
response = await gather_evidence_tool.gather_evidence(
session.question, state=env_state
)
# ensure both evidences are returned
assert "\n1." in response, "gather_evidence did not return any results"
assert "\n2." in response, "gather_evidence should return 2 contexts"

assert session.contexts, "Evidence did not return any results"
assert not session.answer, "Expected no answer yet"

Expand Down

0 comments on commit abeddbc

Please sign in to comment.