-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_tasks.py
More file actions
753 lines (605 loc) · 25.1 KB
/
Copy pathbench_tasks.py
File metadata and controls
753 lines (605 loc) · 25.1 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import asyncio
from dataclasses import dataclass
import json
import os
import random
from typing import Any, Coroutine, Optional, TypeVar
import re
import traceback
from openai import AsyncClient, InternalServerError, LengthFinishReasonError
from pydantic import BaseModel
from bench_utils import (
extract_block_spans,
calculate_latex_command_text_ratio,
check_span_overlapping,
get_questions,
)
from arxiv import get_arxiv_title
SYSTEM_PROMPT = "Please read the following paper and answer the question below."
T = TypeVar('T')
BaseModelT = TypeVar('BaseModelT', bound=BaseModel)
class GlobalConfig:
cot: bool = False
one_shot_question: Optional[dict] = None
add_context: bool = True
invoke_extra_body_kwargs: dict = {}
temperature: float = 0.0
is_thinking_model: bool = False
num_filters = None
stat_counter = 0
dry_run = False
@staticmethod
def kwargs():
return dict(
add_cot=GlobalConfig.cot,
add_one_shot_question=GlobalConfig.one_shot_question,
add_context=GlobalConfig.add_context,
)
def get_invoke_model_name() -> str:
if (model_name := os.getenv('EXT_MODEL')):
return model_name
else:
return 'custom-model'
async def invoke_model(
async_client: AsyncClient, messages: list[dict],
max_tokens: int, temperature = None, response_format = None,
strip_thinking = True,
) -> Optional[str]:
if GlobalConfig.dry_run:
return ''
for retry in range(10):
try:
response = await async_client.chat.completions.create(
model=get_invoke_model_name(),
messages=messages,
temperature=temperature if temperature is not None else GlobalConfig.temperature,
max_tokens=max_tokens + (2048 if GlobalConfig.is_thinking_model else 0),
response_format=response_format,
extra_body=GlobalConfig.invoke_extra_body_kwargs,
)
except InternalServerError as e:
traceback.print_exception(e)
return ''
if response.choices is None or response.choices[0].message.content is None:
print('No choices in response, maybe rate limit')
await asyncio.sleep(5)
continue
content = response.choices[0].message.content
if strip_thinking:
if '</think>' in content:
content = content[content.find('</think>') + len('</think>'):]
assert '</think>' not in content
return content
return ''
async def invole_model_parsed(
async_client: AsyncClient, messages: list[dict],
max_tokens: int, response_format: BaseModelT, temperature = None,
) -> Optional[BaseModelT]:
if GlobalConfig.dry_run:
return None
try:
response = await async_client.beta.chat.completions.parse(
model=get_invoke_model_name(),
messages=messages,
temperature=temperature if temperature is not None else GlobalConfig.temperature,
max_tokens=max_tokens + (8192 if GlobalConfig.is_thinking_model else 0),
response_format=response_format,
extra_body={
"provider": {"require_parameters": True},
**GlobalConfig.invoke_extra_body_kwargs,
},
)
except InternalServerError as e:
traceback.print_exception(e)
return None
except LengthFinishReasonError as e:
traceback.print_exception(e)
return None
return response.choices[0].message.parsed
def check_answer_in_response(outputs, questions) -> int:
correct = 0
for output, question in zip(outputs, questions):
answer = question[-1]
if answer in output:
correct += 1
return correct
def extract_figure_labels(latex_content: str) -> list[str]:
figure_pattern = r'\\begin{figure\*?}(.*?)\\end{figure\*?}'
label_pattern = r'\\label{([^}]*)}'
labels = []
figure_matches = re.finditer(figure_pattern, latex_content, re.DOTALL)
for match in figure_matches:
figure_content = match.group(1)
label_matches = re.finditer(label_pattern, figure_content)
for label_match in label_matches:
labels.append(label_match.group(1))
return labels
@dataclass
class BenchTask:
arxiv_id: str
json_name: str
add_one_shot_question: Optional[dict] = None
add_context: bool = True
def __post_init__(self):
self.json_output_name = self.json_name
if self.add_one_shot_question:
if self.add_one_shot_question['type'] == 'bench':
full_text = self.add_one_shot_question['bench']['text']
question = get_questions(self.add_one_shot_question['bench'][self.json_name])[0]
self.json_output_name += '_one_shot'
self.one_shot_messages = [
{"role": "user", "content": self.build_query(full_text, question)},
{"role": "assistant", "content": self.build_answer(full_text, question)},
]
else:
assert 0
else:
self.one_shot_messages = []
if not self.add_context:
self.json_output_name += '_no_context'
def get_paper_title_string(self) -> str:
return f'[{self.arxiv_id}] {get_arxiv_title("https://arxiv.org/abs/" + self.arxiv_id)}\n'
def generate_bench(self, **kwargs) -> list[tuple]:
raise NotImplementedError()
def run_task_cached(self, cache: dict, client: AsyncClient, full_text: str, questions: list[tuple]) -> list[Coroutine[Any, Any, T]]:
cache_data = cache.get(self.json_output_name)
if cache_data is None or GlobalConfig.dry_run:
return self.run_task(client, full_text, questions)
else:
assert full_text == cache.get('text')
assert questions == cache_data.get('questions')
async def warp(x):
return x
return [warp(x) for x in cache_data['outputs']]
def run_task(self, client: AsyncClient, full_text: str, questions: list[tuple]) -> list[Coroutine[Any, Any, T]]:
raise NotImplementedError()
def dump_task(self, text: str, questions: list[tuple], outputs: list[T]) -> dict:
raise NotImplementedError()
def build_query(self, full_text: str, question: tuple) -> str:
raise NotImplementedError()
def build_answer(self, full_text: str, question: tuple) -> str:
raise NotImplementedError()
@dataclass
class SectionCountTask(BenchTask):
json_name: str = 'section_count'
def __post_init__(self):
super().__post_init__()
self.json_output_name += '_v2'
def generate_bench(self, full_text: str, **kwargs) -> list[tuple]:
pattern = r'\\section\b'
matches = list(re.finditer(pattern, full_text, re.DOTALL))
count = len(matches)
return [(count,)]
def run_task(self, client: AsyncClient, full_text: str, questions: list[tuple]) -> list[Coroutine[Any, Any, int]]:
return [
self.eval(client, full_text, questions[0])
]
def build_query(self, full_text: str, question: tuple) -> str:
if self.add_context:
query = f'''\
```latex
{full_text}
```
'''
else:
query = self.get_paper_title_string()
query += '''
Question: In the paper above, how many sections are there (with the "\section" command, including "\section*")? Please think step by step and give the answer at last. Put the answer number inside the <answer> </answer> tag.
'''
return query
def build_answer(self, full_text: str, question: tuple):
pattern = r'\\section(?:\*|\[(?:[^]]*)\])?\{([^}]*)\}'
matches = list(re.finditer(pattern, full_text, re.DOTALL))
count = len(matches)
res = "Let's count how many sections are there in the paper:\n"
for idx, match in enumerate(matches, start=1):
res += f'{idx}. {match.group(0)}\n'
res += '\n'
res += f'So, there are <answer>{count}</answer> sections are there.'
return res
async def eval(self, client: AsyncClient, full_text: str, question: tuple) -> int:
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
*self.one_shot_messages,
{"role": "user", "content": self.build_query(full_text, question)}
]
response = await invoke_model(
client,
messages,
max_tokens=1024,
strip_thinking=False,
)
# print(messages)
# print(response)
match = re.search(r'<answer>\D*(\d+).*</answer>', response)
if match is not None:
section_count = int(match.group(1))
return section_count, response
else:
return -1, response
def dump_task(self, text, questions, outputs):
pattern = r'\\section(?:\*|\[(?:[^]]*)\])?\{([^}]*)\}'
matches = list(re.finditer(pattern, text, re.DOTALL))
count = len(matches)
output = outputs[0]
if isinstance(output, tuple):
answer, response = output
kwargs = {'_response': response}
else:
answer = output
kwargs = {}
correct = answer == count
return {
'correct': int(correct),
'accuracy': int(correct),
'total': 1,
'questions': questions,
'outputs': [answer],
**kwargs,
}
@dataclass
class ParagraphOrderingTask(BenchTask):
json_name: str = 'paragraph_ordering'
def generate_bench(self, full_text: str, **kwargs) -> list[tuple]:
block_spans = extract_block_spans(full_text)
questions = []
pattern = r'(.{20,}?)\n\s*\n+(.{20,}?)\n\s*\n+(.{20,}?)\n\s*\n+(.{20,}?)\n\s*\n+'
for match in re.finditer(pattern, full_text, re.DOTALL):
span = match.span(0)
if not check_span_overlapping([span], block_spans) and \
calculate_latex_command_text_ratio(match.group(1)) < 0.5 and \
calculate_latex_command_text_ratio(match.group(2)) < 0.5 and \
calculate_latex_command_text_ratio(match.group(3)) < 0.5 and \
calculate_latex_command_text_ratio(match.group(4)) < 0.5 and \
True:
paragraphs = [
match.group(1),
match.group(2),
match.group(3),
match.group(4),
]
indices = list(range(len(paragraphs)))
random.shuffle(indices)
questions.append((span, paragraphs, indices))
random.shuffle(questions)
return questions[:50]
def run_task(self, client: AsyncClient, full_text: str, questions: list[tuple]) -> list[Coroutine[Any, Any, str]]:
return [
self.eval(client, full_text, *t)
for t in questions
]
def build_query(self, full_text: str, question: tuple) -> str:
span, paragraphs, indices = question
if self.add_context:
modified_text = full_text[:span[0]] + '__MISSING_PARAGRAPHS__' + full_text[span[1]:]
query = f'''\
```latex
{modified_text}
```
'''
else:
query = self.get_paper_title_string()
query += f'''
Question: In the paper above, there is a __MISSING_PARAGRAPHS__ blank, where 4 paragraphs are taken out and shuffled. Please put them back in the correct order and answer in the exact form of 'X X X X', where each 'X' is the letter representing a paragraph.
The paragraphs are as follows:
A.
{paragraphs[indices[0]]}
B.
{paragraphs[indices[1]]}
C.
{paragraphs[indices[2]]}
D.
{paragraphs[indices[3]]}
Please think step by step and give the answer at last.
'''
return query
def build_answer(self, full_text: str, question: tuple) -> str:
span, paragraphs, indices = question
inv_indices = {v: k for k, v in enumerate(indices)}
return ' '.join(chr(ord('A') + inv_indices[i]) for i in range(4))
async def eval(
self, client: AsyncClient, full_text: str,
span, paragraphs, indices
) -> int:
query = self.build_query(full_text, (span, paragraphs, indices))
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
*self.one_shot_messages,
{"role": "user", "content": query}
]
response_content = await invoke_model(
client,
messages=messages,
max_tokens=512,
)
return response_content
def dump_task(self, text, questions, outputs):
correct = 0
for question, output in zip(questions, outputs):
inv_indices = {v: k for k, v in enumerate(question[2])}
seq = ' '.join(chr(ord('A') + inv_indices[i]) for i in range(4))
correct += int(seq in output)
return {
'correct': correct,
'accuracy': correct / len(questions) if len(questions) > 0 else float('nan'),
'total': len(questions),
'questions': questions,
'outputs': outputs,
}
@dataclass
class XRefTask(BenchTask):
json_name: str = 'xref'
def __post_init__(self):
super().__post_init__()
self.json_output_name += '_fast'
def generate_bench(self, full_text: str, **kwargs) -> list[tuple]:
figure_labels = extract_figure_labels(full_text)
xref_questions = []
for line in re.split(r'\n\s*\n+', full_text):
matches = re.finditer(r'\\(?:auto|c|)ref{([a-zA-Z0-9\-_:.]+?)}', line)
for match in matches:
replaced = line[:match.start(1)] + '______' + line[match.end(1):]
answer = match.group(1)
if 'fig' in answer or answer in figure_labels:
continue
if answer in full_text.replace(line, replaced):
xref_questions.append((line, replaced, answer))
return xref_questions
def run_task(self, client: AsyncClient, full_text: str, questions: list[tuple]) -> list[Coroutine[Any, Any, str]]:
if self.add_one_shot_question and self.add_one_shot_question['type'] == 'same_paper':
if len(questions) < 1:
return []
original, replaced = questions[0][:2]
modified_text = full_text.replace(original, replaced)
questions = [question for question in questions if question[0] in modified_text]
return [
self.eval_xref(client, full_text, *q[:2], (original, replaced))
for q in questions
]
else:
return [
self.eval_xref(client, full_text, *q[:2])
for q in questions
]
def build_query(self, full_text: str, question: tuple, add_text=True) -> str:
original, replaced = question[:2]
if self.add_context:
if add_text:
modified_text = full_text.replace(original, replaced)
modified_text = re.sub(r'(\\(?:auto|c|)ref)\{([a-zA-Z0-9\-_:.]+?)\}', r'\1\{xxx\}', modified_text)
query = f'''\
```latex
{modified_text}
```
'''
else:
assert full_text == ''
query = ''
else:
query = self.get_paper_title_string() + '\n'
query += f'''\
Question: In the following paragraph, which reference should be filled in the blank (______)?
```latex
{replaced}
```'''
return query
def build_answer(self, full_text: str, question: tuple) -> str:
return question[-1]
async def eval_xref(self, async_client: AsyncClient, text: str, original: str, replaced: str, same_paper_one_shot_question = None) -> str:
if same_paper_one_shot_question is not None:
original_a, replaced_a = same_paper_one_shot_question[:2]
modified_text = text.replace(original, replaced)
query_a = self.build_query(modified_text, (original_a, replaced_a))
answer_a = self.build_answer(modified_text, same_paper_one_shot_question)
query_b = self.build_query('', (original, replaced), add_text=False)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": query_a},
{"role": "assistant", "content": answer_a},
{"role": "user", "content": query_b}
]
else:
query = self.build_query(text, (original, replaced))
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
*self.one_shot_messages,
{"role": "user", "content": query}
]
response_content = await invoke_model(
async_client,
messages=messages,
max_tokens=512,
)
return response_content
def dump_task(self, text, questions, outputs):
questions = questions[-len(outputs):]
correct = check_answer_in_response(outputs, questions)
return {
'correct': correct,
'total': len(questions),
'accuracy': correct / len(questions) if len(questions) > 0 else float('nan'),
'questions': questions,
'outputs': outputs,
}
@dataclass
class CitationListTask(BenchTask):
json_name: str = 'citation_list_v2'
def __post_init__(self):
super().__post_init__()
self.json_output_name += '_fast'
def generate_bench(self, full_text: str, ref_map: dict, **kwargs) -> list[tuple]:
if len(ref_map) < 10:
return []
cite_pattern = r'\\(?:cite|citep|citet|citealp|citeauthor|citeyear)(?:\[.*?\])?\s*\{([^}]+)\}'
questions = []
sampled_keys = list(ref_map.keys())
cite_info = json.dumps(
[
{
'ID': ID,
'title': ref_map[ID]['title'],
'abstract': ref_map[ID]['abstract'],
}
for ID in sampled_keys
],
indent=4,
ensure_ascii=False,
)
for line in re.split(r'\n\s*\n+', full_text):
matches = re.finditer(cite_pattern, line, re.IGNORECASE)
unique_keys = set()
for match in matches:
keys = match.group(1).split(',')
for key in keys:
unique_keys.add(key.strip())
for key in unique_keys:
if key in ref_map:
replaced = line.replace(key, '______')
answer = key
questions.append((line, replaced, cite_info, answer))
return questions
def run_task(self, client: AsyncClient, full_text: str, questions: list[tuple]) -> list[Coroutine[Any, Any, str]]:
return [
self.eval_citations(client, full_text, *q[:3])
for q in questions
]
def build_query(self, full_text: str, question: tuple[str, str, str, str]) -> str:
original, replaced, cite_info, _ = question
if self.add_context:
modified_text = full_text.replace(original, replaced)
cite_pattern = r'(\\(?:cite|citep|citet|citealp|citeauthor|citeyear)(?:\[.*?\])?\s*)\{([^}]+)\}'
modified_text = re.sub(cite_pattern, r'\1\{xxx\}', modified_text)
query = f'''\
```latex
{modified_text}
```
'''
else:
query = self.get_paper_title_string() + '\n'
query += f'''\
Here is the information of some possible citations:
```json
{cite_info}
```
Question: In the following paragraph, which citation id should be filled in the blank (______)?
```latex
{replaced}
```
'''
return query
def build_answer(self, full_text: str, question: tuple[str, str, str, str]) -> str:
class CitationAnswer(BaseModel):
citation_id: str | None = None
return CitationAnswer(citation_id=question[3]).model_dump_json(indent=4)
async def eval_citations(self, async_client: AsyncClient, text: str, original: str, replaced: str, cite_info: str) -> str:
query = self.build_query(text, (original, replaced, cite_info, None))
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
*self.one_shot_messages,
{"role": "user", "content": query}
]
response_content = await invoke_model(
async_client,
messages=messages,
max_tokens=512,
)
return response_content
def dump_task(self, text, questions, outputs):
correct = check_answer_in_response(outputs, questions)
return {
'correct': correct,
'total': len(questions),
'accuracy': correct / len(questions) if len(questions) > 0 else float('nan'),
'questions': questions,
'outputs': outputs,
}
@dataclass
class FillNumberTask(BenchTask):
json_name: str = 'fill_number'
def __post_init__(self):
super().__post_init__()
self.json_output_name += '_v2'
def generate_bench(self, full_text: str, **kwargs) -> list[tuple[int, int]]:
figure_table_spans = extract_block_spans(full_text)
result_spans = self.extract_result_spans(full_text)
questions = []
number_pattern = r'\b(-?\d+(?:\.\d+)?)\b'
for match in re.finditer(number_pattern, full_text):
span = match.span(1)
if not check_span_overlapping([span], figure_table_spans) and \
check_span_overlapping([span], result_spans):
questions.append(span)
random.shuffle(questions)
return questions[:50]
def extract_result_spans(self, latex_content: str) -> list[tuple[int, int]]:
spans = []
pattern = r'(\\section{[^}]*(?:Result)[^}]*}.*?)(?:\\section|$)'
matches = re.finditer(pattern, latex_content, re.DOTALL)
for match in matches:
span = match.span(1)
spans.append(span)
pattern = r'(\\subsection{[^}]*(?:Result)[^}]*}.*?)(?:\\subsection|$)'
matches = re.finditer(pattern, latex_content, re.DOTALL)
for match in matches:
span = match.span(1)
spans.append(span)
return spans
def run_task(self, client: AsyncClient, full_text: str, questions: list[tuple[int, int]]) -> list[Coroutine[Any, Any, float]]:
return [
self.eval_fill_number(client, full_text, q)
for q in questions
]
def build_query(self, full_text: str, question: tuple[int, int]) -> str:
if self.add_context:
modified_text = full_text[:question[0]] + '___FILL_HERE___' + full_text[question[1]:]
query = f'''\
```latex
{modified_text}
```'''
else:
query = self.get_paper_title_string()
query += '''
Question: In the paper above, there is a blank number with placeholder (___FILL_HERE___). Please think step by step and fill in the blank with the most appropriate number. You should put the answer number between the "<answer> </answer>" tag, such as "<answer>2.0</asnwer>".
'''
return query
def build_answer(self, full_text: str, question: tuple[int, int]) -> str:
number = float(full_text[question[0]:question[1]])
return f"It seems that the number should be filled is <answer>{number}</answer>."
async def eval_fill_number(self, async_client: AsyncClient, text: str, span: tuple[int, int]) -> float:
query = self.build_query(text, span)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
*self.one_shot_messages,
{"role": "user", "content": query}
]
response = await invoke_model(
async_client,
messages,
max_tokens=1024,
)
# print(messages)
# print(response)
match = re.search(r'<answer>\D*\b(-?\d+(?:\.\d+)?)\b.*</answer>', response)
if match is not None:
number = float(match.group(1))
return number
else:
return ''
def dump_task(self, text, questions, outputs):
correct = self.compare_numbers(
outputs,
[text[start:end] for start, end in questions]
)
return {
'correct': correct,
'total': len(questions),
'accuracy': correct / len(questions) if len(questions) > 0 else float('nan'),
'questions': questions,
'outputs': outputs,
}
def compare_numbers(self, outputs, answers) -> int:
correct = 0
for output, answer in zip(outputs, answers):
if output == float(answer):
correct += 1
return correct