Skip to content

Commit 4a531a5

Browse files
committed
Making current evidence and relevant evidence different in status
1 parent 8e1a69c commit 4a531a5

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

src/paperqa/agents/env.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,25 @@ def make_clinical_trial_status(
141141
total_clinical_trials: int,
142142
relevant_clinical_trials: int,
143143
evidence_count: int,
144+
relevant_evidence_count: int,
144145
cost: float,
145146
) -> str:
146147
return (
147148
f"Status: Paper Count={total_paper_count}"
148149
f" | Relevant Papers={relevant_paper_count}"
149150
f" | Clinical Trial Count={total_clinical_trials}"
150151
f" | Relevant Clinical Trials={relevant_clinical_trials}"
151-
f" | Current Evidence={evidence_count}"
152+
f" | Evidence Count={evidence_count}"
153+
f" | Relevant Evidence={relevant_evidence_count}"
152154
f" | Current Cost=${cost:.4f}"
153155
)
154156

155157

156-
# SEE: https://regex101.com/r/L0L5MH/1
158+
# SEE: https://regex101.com/r/L0L5MH/4
157159
CLINICAL_STATUS_SEARCH_REGEX_PATTERN: str = (
158-
r"Status: Paper Count=(\d+) \| Relevant Papers=(\d+)(?:\s\|\sClinical Trial"
159-
r" Count=(\d+)\s\|\sRelevant Clinical Trials=(\d+))?\s\|\sCurrent Evidence=(\d+)"
160+
r"Status: Paper Count=(\d+)\s\|\sRelevant Papers=(\d+)"
161+
r"(?:\s\|\sClinical Trial Count=(\d+)\s\|\sRelevant Clinical Trials=(\d+))?"
162+
r"\s\|\sEvidence Count=(\d+)\s\|\sRelevant Evidence=(\d+)"
160163
)
161164

162165

@@ -195,7 +198,8 @@ def clinical_trial_status(state: "EnvironmentState") -> str:
195198
in getattr(c.text.doc, "other", {}).get("client_source", [])
196199
}
197200
),
198-
evidence_count=len(relevant_contexts),
201+
evidence_count=len(state.session.contexts),
202+
relevant_evidence_count=len(relevant_contexts),
199203
cost=state.session.cost,
200204
)
201205

src/paperqa/agents/tools.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525

2626

2727
def make_status(
28-
total_paper_count: int, relevant_paper_count: int, evidence_count: int, cost: float
28+
total_paper_count: int,
29+
relevant_paper_count: int,
30+
evidence_count: int,
31+
relevant_evidence_count: int,
32+
cost: float,
2933
) -> str:
3034
return (
3135
f"Status: Paper Count={total_paper_count}"
32-
f" | Relevant Papers={relevant_paper_count} | Current Evidence={evidence_count}"
36+
f" | Relevant Papers={relevant_paper_count}"
37+
f" | Evidence Count={evidence_count}"
38+
f" | Relevant Evidence={relevant_evidence_count}"
3339
f" | Current Cost=${cost:.4f}"
3440
)
3541

@@ -39,7 +45,8 @@ def default_status(state: "EnvironmentState") -> str:
3945
return make_status(
4046
total_paper_count=len(state.docs.docs),
4147
relevant_paper_count=len({c.text.doc.dockey for c in relevant_contexts}),
42-
evidence_count=len(relevant_contexts),
48+
evidence_count=len(state.session.contexts),
49+
relevant_evidence_count=len(relevant_contexts),
4350
cost=state.session.cost,
4451
)
4552

@@ -60,9 +67,10 @@ class EnvironmentState(BaseModel):
6067
),
6168
)
6269

63-
# SEE: https://regex101.com/r/RmuVdC/1
70+
# SEE: https://regex101.com/r/RmuVdC/3
6471
STATUS_SEARCH_REGEX_PATTERN: ClassVar[str] = (
65-
r"Status: Paper Count=(\d+) \| Relevant Papers=(\d+) \| Current Evidence=(\d+)"
72+
r"Status: Paper Count=(\d+)\s\|\sRelevant Papers=(\d+)"
73+
r"\s\|\sEvidence Count=(\d+)\s\|\sRelevant Evidence=(\d+)"
6674
)
6775

6876
@computed_field # type: ignore[prop-decorator]
@@ -350,7 +358,7 @@ async def gen_answer(self, state: EnvironmentState) -> str:
350358
# Use to separate answer from status
351359
# NOTE: can match failure to answer or an actual answer
352360
ANSWER_SPLIT_REGEX_PATTERN: ClassVar[str] = (
353-
r" \| " + EnvironmentState.STATUS_SEARCH_REGEX_PATTERN
361+
r"\s\|\s" + EnvironmentState.STATUS_SEARCH_REGEX_PATTERN
354362
)
355363

356364
@classmethod
@@ -359,7 +367,7 @@ def extract_answer_from_message(cls, content: str) -> str:
359367
answer, *rest = re.split(
360368
pattern=cls.ANSWER_SPLIT_REGEX_PATTERN, string=content, maxsplit=1
361369
)
362-
return answer if len(rest) == 4 else "" # noqa: PLR2004
370+
return answer if len(rest) == 5 else "" # noqa: PLR2004
363371

364372

365373
class Reset(NamedTool):
@@ -383,7 +391,7 @@ class Complete(NamedTool):
383391

384392
# Use to separate certainty from status
385393
CERTAINTY_SPLIT_REGEX_PATTERN: ClassVar[str] = (
386-
r" \| " + EnvironmentState.STATUS_SEARCH_REGEX_PATTERN
394+
r"\s\|\s" + EnvironmentState.STATUS_SEARCH_REGEX_PATTERN
387395
)
388396

389397
NO_ANSWER_PHRASE: ClassVar[str] = "No answer generated."

tests/test_agents.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,11 @@ async def test_successful_memory_agent(agent_test_settings: Settings) -> None:
397397
" and you have already tried to answer several times,"
398398
" you can terminate by calling the {complete_tool_name} tool."
399399
" The current status of evidence/papers/cost is "
400-
f"{make_status(total_paper_count=0, relevant_paper_count=0, evidence_count=0, cost=0.0)}" # Started 0 # noqa: E501
400+
f"{make_status(total_paper_count=0, relevant_paper_count=0, evidence_count=0, relevant_evidence_count=0, cost=0.0)}" # Started 0 # noqa: E501
401401
"\n\nTool request message '' for tool calls: paper_search(query='XAI for"
402402
" chemical property prediction', min_year='2018', max_year='2024')"
403403
f" [id={memory_id}]\n\nTool response message '"
404-
f"{make_status(total_paper_count=2, relevant_paper_count=0, evidence_count=0, cost=0.0)}" # Found 2 # noqa: E501
404+
f"{make_status(total_paper_count=2, relevant_paper_count=0, evidence_count=0, relevant_evidence_count=0, cost=0.0)}" # Found 2 # noqa: E501
405405
f"' for tool call ID {memory_id} of tool 'paper_search'"
406406
),
407407
input=(
@@ -412,7 +412,7 @@ async def test_successful_memory_agent(agent_test_settings: Settings) -> None:
412412
" and you have already tried to answer several times,"
413413
" you can terminate by calling the {complete_tool_name} tool."
414414
" The current status of evidence/papers/cost is "
415-
f"{make_status(total_paper_count=0, relevant_paper_count=0, evidence_count=0, cost=0.0)}"
415+
f"{make_status(total_paper_count=0, relevant_paper_count=0, evidence_count=0, relevant_evidence_count=0, cost=0.0)}" # noqa: E501
416416
),
417417
output=(
418418
"Tool request message '' for tool calls: paper_search(query='XAI for"

0 commit comments

Comments
 (0)