CPM-9G/My_project/knowledge_api.py

55 lines
1.6 KiB
Python

import re
import json
# 加载测试数据
def load_test_data(file_path):
test_data = []
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
test_data.append(json.loads(line.strip()))
except Exception as e:
print(f"加载测试数据时发生错误: {e}")
return test_data
# 保存提交结果
def save_submissions_to_jsonl(submissions, output_file_path):
try:
with open(output_file_path, 'w', encoding='utf-8') as file:
for submission in submissions:
json.dump(submission, file, ensure_ascii=False)
file.write('\n')
print(f"提交的结果已保存到 {output_file_path}")
except Exception as e:
print(f"保存提交结果时发生错误: {e}")
# 检测答案中的选项
def detect_choice_in_answer(answer_text):
possible_choices = ['A', 'B', 'C', 'D']
for choice in possible_choices:
if re.search(rf'(?<![a-zA-Z]){choice}(?![a-zA-Z])', answer_text):
return choice
return None
# 提取代码
def extract_code_between_triple_backticks(raw_output):
# 从 raw_output 中提取第一个和第二个 ``` 之间的代码块。
parts = raw_output.split("```")
# 如果找到至少两个 ```
if len(parts) >= 3:
code_block = parts[1].strip() # 提取第一个 ``` 和第二个 ``` 之间的部分
# 如果代码块中包含 "python\n",则删除它
if "python\n" in code_block:
code_block = code_block.replace("python\n", "")
return code_block
else:
return "" # 如果没有找到成对的 ```,返回空字符串