Yongkang ZOU commited on
Commit
4e6c049
·
1 Parent(s): dfe572b

update answer format

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -8,23 +8,21 @@ from langchain_core.messages import HumanMessage
8
 
9
  import re
10
 
11
- import re
12
-
13
  def extract_answer(text: str) -> str:
14
  """
15
  Clean and extract the final answer from agent output.
16
- - Removes 'FINAL ANSWER:' if present.
17
- - Strips trailing punctuation and whitespace.
18
- - Normalizes comma/semicolon-separated lists.
19
  """
20
- # 提取 FINAL ANSWER 后的内容(如果存在)
21
- match = re.search(r"final answer[::]?\s*(.+)", text, re.IGNORECASE)
22
- answer = match.group(1) if match else text
23
 
24
- # 去除结尾句号和首尾空格
25
- answer = answer.strip().rstrip('.').strip()
 
26
 
27
- # 多项答案的格式标准化(如 a , b -> a,b)
28
  if ',' in answer:
29
  answer = ",".join(part.strip() for part in answer.split(','))
30
  if ';' in answer:
@@ -33,6 +31,7 @@ def extract_answer(text: str) -> str:
33
  return answer
34
 
35
 
 
36
  # --- Constants ---
37
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
38
 
 
8
 
9
  import re
10
 
 
 
11
  def extract_answer(text: str) -> str:
12
  """
13
  Clean and extract the final answer from agent output.
14
+ Removes prefixes like 'FINAL ANSWER:', trims punctuation,
15
+ and normalizes separators.
 
16
  """
17
+ # 提取 final answer 后内容
18
+ match = re.search(r"(final\s*answer|answer\s*is)[::]?\s*(.+)", text, re.IGNORECASE)
19
+ answer = match.group(2) if match else text
20
 
21
+ # 清理格式
22
+ answer = answer.strip().lstrip(":").strip() # ✅ 去掉前导冒号和空格
23
+ answer = answer.rstrip('.').strip()
24
 
25
+ # 多项格式化
26
  if ',' in answer:
27
  answer = ",".join(part.strip() for part in answer.split(','))
28
  if ';' in answer:
 
31
  return answer
32
 
33
 
34
+
35
  # --- Constants ---
36
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
37