MCPcopy Create free account
hub / github.com/RUC-NLPIR/WebThinker / extract_answer_fn

Function extract_answer_fn

scripts/evaluate/evaluate.py:18–63  ·  view source on GitHub ↗
(output, mode='qa', extract_answer=False)

Source from the content-addressed store, hash-verified

16
17
18def extract_answer_fn(output, mode='qa', extract_answer=False):
19 if extract_answer == False and mode not in ['infogen', 'summary', 'research']:
20 if mode == 'qa':
21 return output.strip()
22 pred_answer_lines = output.replace("\n\n", "\n").strip().split('\n')
23 pred_answer = '\n'.join(pred_answer_lines[-3:])
24 return pred_answer
25 extracted_text = ''
26 if mode == 'codegen':
27 pattern = r'```python\s*(.*?)\s*```' # Extract the code between ```python and ```
28 matches = re.findall(pattern, output, re.DOTALL | re.IGNORECASE)
29 if matches:
30 extracted_text = matches[-1].strip() # Take the last match
31 elif mode in ['infogen', 'summary', 'research']:
32 pattern_info = "**Final Information"
33 if "</think>\n" in output:
34 extracted_text = output.split("</think>\n")[-1].split("<|begin_click_link|>")[0].replace(pattern_info, "").strip(':**').strip('\n').strip("```").strip() # 提取</think>后面的内容
35 if mode == 'infogen':
36 extracted_text = '\n'.join(extracted_text.replace("\n\n", "\n").split('\n')[:5]) # 只保留前5行
37 elif pattern_info in output:
38 extracted_text = output.split(pattern_info)[-1].split("<|begin_click_link|>")[0].strip('\n').strip(':**').strip("```").strip() # 提取**Final Information**后面的内容
39 if mode == 'infogen':
40 extracted_text = '\n'.join(extracted_text.replace("\n\n", "\n").split('\n')[:5]) # 只保留前5行
41 else:
42 # extracted_text = "No helpful information found."
43 extracted_text = '\n'.join(output.strip().replace("</think>\n", "").replace("\n\n", "\n").split('\n')[-5:]) # 若没提取到,只保留最后5行
44 if mode == 'research':
45 extracted_text = extracted_text[:6000]
46 else:
47 extracted_text = extracted_text[:2500]
48 elif mode in ['math', 'choose', 'qa']:
49 pattern = r'\\boxed\{(.*)\}'
50 matches = re.findall(pattern, output)
51 if matches:
52 extracted_text = matches[-1] # Take the last match
53 else:
54 pattern = 'ANSWER:'
55 if pattern in output:
56 extracted_text = output.split(pattern)[-1].strip('**').strip()
57 if mode in ['choose']:
58 inner_pattern = r'\\text\{(.*)\}'
59 inner_matches = re.findall(inner_pattern, extracted_text)
60 if inner_matches:
61 extracted_text = inner_matches[-1] # Take the last match
62 extracted_text = extracted_text.strip("()")
63 return extracted_text
64
65
66async def llm_evaluate_equivalence_single(

Callers 5

process_single_sequenceFunction · 0.90
process_single_sequenceFunction · 0.90
evaluate_predictionsFunction · 0.70
run_evaluationFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected