|
|
import json
|
|
|
import pandas as pd
|
|
|
import os
|
|
|
|
|
|
def save_responses(type, index, response, output_file, raw_output_file):
|
|
|
data_path = './data/data.xlsx'
|
|
|
if type == 'yes_no':
|
|
|
sheet = 'Yes or No Train'
|
|
|
elif type == 'factoid':
|
|
|
sheet = 'Factoid Train'
|
|
|
data_train = pd.read_excel(data_path, sheet_name=sheet)
|
|
|
question_train = data_train['Question']
|
|
|
|
|
|
|
|
|
if hasattr(response, 'content'):
|
|
|
content = response.content
|
|
|
elif isinstance(response, dict) and 'content' in response:
|
|
|
content = response['content']
|
|
|
else:
|
|
|
content = "无效的响应格式"
|
|
|
|
|
|
|
|
|
result = {
|
|
|
"question": question_train[index],
|
|
|
"answer": content
|
|
|
}
|
|
|
|
|
|
|
|
|
if os.path.exists(output_file):
|
|
|
with open(output_file, 'r', encoding='utf-8') as f:
|
|
|
existing_data = json.load(f)
|
|
|
else:
|
|
|
existing_data = []
|
|
|
|
|
|
|
|
|
existing_data.append(result)
|
|
|
|
|
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
|
json.dump(existing_data, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
|
raw_response = {
|
|
|
"question": question_train[index],
|
|
|
"raw_response": str(response)
|
|
|
}
|
|
|
|
|
|
if os.path.exists(raw_output_file):
|
|
|
with open(raw_output_file, 'r', encoding='utf-8') as f:
|
|
|
existing_raw_data = json.load(f)
|
|
|
else:
|
|
|
existing_raw_data = []
|
|
|
|
|
|
|
|
|
existing_raw_data.append(raw_response)
|
|
|
|
|
|
with open(raw_output_file, 'w', encoding='utf-8') as f:
|
|
|
json.dump(existing_raw_data, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
def save_responses_knn(prompt, type, index, response, output_file, raw_output_file):
|
|
|
data_path = './data/data.xlsx'
|
|
|
if type == 'yes_no':
|
|
|
sheet = 'Yes or No Train'
|
|
|
elif type == 'factoid':
|
|
|
sheet = 'Factoid Train'
|
|
|
data_train = pd.read_excel(data_path, sheet_name=sheet)
|
|
|
question_train = data_train['Question']
|
|
|
|
|
|
|
|
|
if hasattr(response, 'content'):
|
|
|
content = response.content
|
|
|
elif isinstance(response, dict) and 'content' in response:
|
|
|
content = response['content']
|
|
|
else:
|
|
|
content = "无效的响应格式"
|
|
|
|
|
|
|
|
|
result = {
|
|
|
"question": question_train[index],
|
|
|
"answer": content
|
|
|
}
|
|
|
|
|
|
|
|
|
if os.path.exists(output_file):
|
|
|
with open(output_file, 'r', encoding='utf-8') as f:
|
|
|
existing_data = json.load(f)
|
|
|
else:
|
|
|
existing_data = []
|
|
|
|
|
|
|
|
|
existing_data.append(result)
|
|
|
|
|
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
|
json.dump(existing_data, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
|
raw_response = {
|
|
|
"prompt": prompt,
|
|
|
"raw_response": str(response)
|
|
|
}
|
|
|
|
|
|
if os.path.exists(raw_output_file):
|
|
|
with open(raw_output_file, 'r', encoding='utf-8') as f:
|
|
|
existing_raw_data = json.load(f)
|
|
|
else:
|
|
|
existing_raw_data = []
|
|
|
|
|
|
|
|
|
existing_raw_data.append(raw_response)
|
|
|
|
|
|
with open(raw_output_file, 'w', encoding='utf-8') as f:
|
|
|
json.dump(existing_raw_data, f, ensure_ascii=False, indent=4) |