Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ""
Copy link
Collaborator

@jamesbraza jamesbraza Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two comments here:

  • Is there a leading space by design?
  • Can we do "{self.settings.agent.agent_evidence_n} best evidence:" to be more precise
    • This may be a bad idea also, so feel free to not do this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, there's a leading space by design, it's been there. On your second bullet -- that seems more confusing to me? i.e. "3 best evidence:"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay agreed, and also when agent_evidence_n = 1 it will be confusing too. Ignore the second one


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
Loading