-
Notifications
You must be signed in to change notification settings - Fork 601
/
task.py
358 lines (323 loc) · 12.7 KB
/
task.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
__all__ = [
"ENV_NAME",
"TASK_DATASET_NAME",
"GradablePaperQAEnvironment",
"LitQATaskDataset",
"LitQAv2TaskDataset",
"LitQAv2TaskSplit",
]
import logging
import re
from abc import ABC
from collections.abc import Awaitable, Callable, Sequence
from copy import deepcopy
from enum import StrEnum
from typing import TYPE_CHECKING, Any, Self, assert_never
from aviary.core import (
TASK_DATASET_REGISTRY,
Frame,
Messages,
TaskDataset,
ToolRequestMessage,
ToolResponseMessage,
)
from aviary.env import ENV_REGISTRY
from paperqa.types import DocDetails
from .search import SearchIndex, maybe_get_manifest
try:
from ldp.alg import ComputeTrajectoryMetricsMixin
except ImportError:
class ComputeTrajectoryMetricsMixin: # type: ignore[no-redef]
"""Placeholder for when ldp isn't installed."""
from paperqa.docs import Docs
from paperqa.litqa import (
DEFAULT_EVAL_MODEL_NAME,
DEFAULT_LABBENCH_HF_HUB_NAME,
DEFAULT_REWARD_DISTRIBUTION,
LitQAEvaluation,
read_litqa_v2_from_hub,
)
from paperqa.llms import EmbeddingModel, LiteLLMModel, LLMModel
from paperqa.types import PQASession
from .env import POPULATE_FROM_SETTINGS, PaperQAEnvironment
from .models import QueryRequest
from .tools import GenerateAnswer
if TYPE_CHECKING:
from ldp.data_structures import Trajectory
logger = logging.getLogger(__name__)
class GradablePaperQAEnvironment(PaperQAEnvironment):
"""Extended environment that can grade answers."""
def __init__(
self,
query: QueryRequest,
docs: Docs,
llm_model: LiteLLMModel | None = POPULATE_FROM_SETTINGS,
summary_llm_model: LiteLLMModel | None = POPULATE_FROM_SETTINGS,
embedding_model: EmbeddingModel | None = POPULATE_FROM_SETTINGS,
evaluation_from_answer: (
Callable[[PQASession | str], Awaitable[LitQAEvaluation]] | None
) = None,
sources: str | list[str] | None = None,
rewards: Sequence[float] = DEFAULT_REWARD_DISTRIBUTION,
evaluation_callback: Callable[[LitQAEvaluation], Awaitable] | None = None,
**env_kwargs,
):
super().__init__(
query, docs, llm_model, summary_llm_model, embedding_model, **env_kwargs
)
self._evaluation_from_answer = evaluation_from_answer
# Enables checking an Index has the right DOI(s)
self.sources: list[str] | None = (
[sources] if isinstance(sources, str) else sources
)
self._evaluation_callback = evaluation_callback
self._rewards = rewards
async def validate_sources(
self, manifest_or_index: dict[str, DocDetails] | SearchIndex | None = None
) -> None:
"""Validate the sources can be found in the input manifest or index."""
if not self.sources:
return
if manifest_or_index is None: # Let's try to load in the manifest
manifest_or_index = await maybe_get_manifest(
filename=await self._query.settings.agent.index.finalize_manifest_file()
)
if isinstance(manifest_or_index, SearchIndex):
entity: str = "index"
file_names: set[str] = {k for k in await manifest_or_index.index_files if k}
lowercased_dois: set[str] = set()
else:
entity = "manifest"
file_names = {k for k in manifest_or_index if k}
lowercased_dois = {
v["doi"].lower() for v in manifest_or_index.values() if v["doi"]
}
if not file_names: # File names being empty means something's wrong
logger.warning(
f"Can't validate sources {self.sources} without a correctly specified"
f" {entity}."
)
return
not_found = [
s
for s in self.sources
if s not in file_names and s.lower() not in lowercased_dois
]
if not_found:
raise ValueError(
f"Sources {not_found} of {self.sources} not found in the {entity},"
f" the corresponding query was {self._query.query!r}."
)
async def step(
self, action: ToolRequestMessage
) -> tuple[Messages, float, bool, bool]:
messages, reward, done, truncated = await super().step(action)
if not done or not self._evaluation_from_answer:
return messages, reward, done, truncated
valid_answers, failed_answer_messages = [], []
for m in messages:
if (
not isinstance(m, ToolResponseMessage)
or m.name != GenerateAnswer.gen_answer.__name__
):
continue # Filter out non-answer messages (in case parallel tool calls)
if answer := GenerateAnswer.extract_answer_from_message(content=m.content):
valid_answers.append(answer)
else:
failed_answer_messages.append(m)
if not valid_answers: # No answer, so no positive reward
return messages, reward, done, truncated
if len(valid_answers) != 1:
raise NotImplementedError(
f"Expected just one answer message, got more than one in {messages}."
)
answer = valid_answers[0]
if failed_answer_messages:
logger.warning(
"More than one answer detected, discarding failed answer messages"
f" {failed_answer_messages}, continuing with answer {answer}."
)
# Okay, so we have one answer that was not a failed answer. Let's evaluate it
evaluation = await self._evaluation_from_answer(answer)
if evaluation_callback := self._evaluation_callback:
await evaluation_callback(evaluation)
return messages, reward + self._rewards[evaluation.value], done, truncated
def export_frame(self) -> Frame:
raise NotImplementedError("Didn't yet need to export a frame.")
def __deepcopy__(self, memo) -> Self:
copy_state = deepcopy(self.state, memo)
# We don't know the side effects of deep copying a litellm.Router,
# so we force a shallow copy of these LiteLLMModels
env_model_kwargs: dict[str, Any] = {
name: model if model is None else type(model)(**model.model_dump())
for name, model in (
("llm_model", self._llm_model),
("summary_llm_model", self._summary_llm_model),
("embedding_model", self._embedding_model),
)
}
copy_self = type(self)(
query=deepcopy(self._query, memo), # deepcopy for _docs_name
docs=copy_state.docs,
evaluation_from_answer=self._evaluation_from_answer,
sources=self.sources,
rewards=self._rewards,
evaluation_callback=self._evaluation_callback,
**env_model_kwargs,
)
copy_self.state = copy_state
# Because we shallow copied the LiteLLMModels, we need to re-make the
# tool functions within the tools
copy_self.tools = copy_self.make_tools()
return copy_self
ENV_NAME = "paperqa-local"
ENV_REGISTRY[ENV_NAME] = (
GradablePaperQAEnvironment.__module__,
GradablePaperQAEnvironment.__name__,
)
class LitQATaskDataset(
TaskDataset[GradablePaperQAEnvironment], ComputeTrajectoryMetricsMixin, ABC
):
"""
Abstract base class for a task dataset of LitQA v1 or v2 questions.
This is an ABC because it's non-specific to a LitQA version.
Examples include LitQA v1, v2, or a test stub version of LitQA.
"""
def __init__(
self,
base_query: QueryRequest | dict | None = None,
base_docs: Docs | dict | None = None,
rewards: Sequence[float] = DEFAULT_REWARD_DISTRIBUTION,
eval_model: LLMModel | str = DEFAULT_EVAL_MODEL_NAME,
**env_kwargs,
):
if base_query is None:
base_query = QueryRequest()
if isinstance(base_query, dict):
base_query = QueryRequest(**base_query)
self._base_query = base_query
if base_docs is None:
base_docs = Docs()
if isinstance(base_docs, dict):
base_docs = Docs(**base_docs)
self._base_docs = base_docs
self._rewards = rewards
self._env_kwargs = env_kwargs
self._eval_model = eval_model
def _make_gradable_environment(
self,
ideal: str,
distractors: str | list[str],
question: str,
use_unsure: bool = True,
sources: str | list[str] | None = None,
) -> GradablePaperQAEnvironment:
qa_prompt, evaluation_from_answer = LitQAEvaluation.from_question(
ideal=ideal,
distractors=distractors,
question=question,
use_unsure=use_unsure,
eval_model=self._eval_model,
)
query = self._base_query.model_copy()
query.query = qa_prompt
return GradablePaperQAEnvironment(
query=query,
docs=self._base_docs.model_copy(),
evaluation_from_answer=evaluation_from_answer,
sources=sources,
rewards=self._rewards,
**self._env_kwargs,
)
def compute_trajectory_metrics(
self, trajectories: "Sequence[Trajectory]"
) -> dict[str, list[float]]:
total_paper_count: list[float] = []
relevant_paper_count: list[float] = []
evidence_count: list[float] = []
for t in trajectories:
split_answers = [
split_answers
for split_answers in (
re.split(
pattern=GenerateAnswer.ANSWER_SPLIT_REGEX_PATTERN,
string=obs.content,
)
for obs in t.steps[-1].next_observation
if (
isinstance(obs, ToolResponseMessage)
and obs.name == GenerateAnswer.TOOL_FN_NAME
)
)
# Filter for places where the regex split succeeded
if len(split_answers) >= 4 # noqa: PLR2004
]
for i, metric_list in enumerate(
(total_paper_count, relevant_paper_count, evidence_count),
start=1, # Regex extraction of status starts after answer
):
metric_list.append( # Use mean to allow for multiple answers
sum(int(sa[i]) for sa in split_answers) / len(split_answers)
if split_answers # Avoid div0 (when no answer was made)
else 0
)
return super().compute_trajectory_metrics(trajectories) | {
"total_paper_count": total_paper_count,
"relevant_paper_count": relevant_paper_count,
"evidence_count": evidence_count,
"correct": [
int(t.steps[-1].reward == self._rewards[0]) for t in trajectories
],
"correct_unsure": [
int(t.steps[-1].reward in {self._rewards[0], self._rewards[1]})
for t in trajectories
],
}
class LitQAv2TaskSplit(StrEnum):
TRAIN = "train"
EVAL = "eval"
class LitQAv2TaskDataset(LitQATaskDataset):
"""Task dataset of LitQA v2 questions."""
def __init__(
self,
*args,
labbench_dataset: str = DEFAULT_LABBENCH_HF_HUB_NAME,
split: str | LitQAv2TaskSplit = LitQAv2TaskSplit.EVAL,
**kwargs,
):
super().__init__(*args, **kwargs)
train_df, eval_df = read_litqa_v2_from_hub(labbench_dataset)
split = LitQAv2TaskSplit(split)
if split == LitQAv2TaskSplit.TRAIN:
self.data = train_df
elif split == LitQAv2TaskSplit.EVAL:
self.data = eval_df
else:
assert_never(split)
def get_new_env_by_idx(self, idx: int) -> GradablePaperQAEnvironment:
sources = []
for s in self.data.iloc[idx].sources:
try:
(doi,) = (
s.split(substr, maxsplit=1)[1]
for substr in DocDetails.DOI_URL_FORMATS
if substr in s
)
except ValueError as exc:
raise NotImplementedError(
f"Didn't handle DOI extraction from source {s!r}."
) from exc
sources.append(doi)
return self._make_gradable_environment(
ideal=self.data.iloc[idx].ideal,
distractors=self.data.iloc[idx].distractors,
question=self.data.iloc[idx].question,
sources=sources,
)
def __len__(self) -> int:
return len(self.data)
TASK_DATASET_NAME = "litqa-v2"
TASK_DATASET_REGISTRY[TASK_DATASET_NAME] = (
LitQAv2TaskDataset.__module__,
LitQAv2TaskDataset.__name__,
)