Repository navigation
Expand file tree
/
Copy pathModel_batch_CoT_PoT.py
More file actions
114 lines (99 loc) · 5.54 KB
/
Copy pathModel_batch_CoT_PoT.py
File metadata and controls
114 lines (99 loc) · 5.54 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
from docx import Document
import json
from datetime import datetime
import torch
import os
from utils.prompts import extract_code
from utils import dolphin_chat_utils
from utils.chat_utils import get_gpu_memory
from utils.docx_utils import add_para, add_title, add_dictionary, add_para_highlight
from utils.intercept_error import convert_full_text, check_indent_and_run
from utils.logger_config import LoggerConfig
os.environ["TOKENIZERS_PARALLELISM"] = "false"
torch.cuda.empty_cache()
print(get_gpu_memory())
date = datetime.now().date()
now_time = datetime.now().time()
model_dir = "/mnt/zmb/zmb_workspace/model/"
auto_dir = "prompts/template/Auto/"
save_json_dir = "outputs/auto/json/"
log_dir = 'outputs/auto_cot_pot/log/{0}'.format(date)
os.makedirs(log_dir, exist_ok=True)
logger = LoggerConfig(log_file='{0}/{1}.log'.format(log_dir, now_time)).logger
model_device1 = 'auto'
model_tokenizer_device = 'cuda'
model1_name = "dolphin-2.2.1-mistral-7b"
model1_path = model_dir + model1_name
model1, tokenizer1 = dolphin_chat_utils.load_model(model1_path, model_device1)
logger.info(get_gpu_memory())
doc = Document()
doc.add_heading('已采纳题目结果', 0)
dataset_dir = "dataset/benchmark/"
adopt_json_file = dataset_dir + "adopt.json"
save_docx_dir = "outputs/auto_cot_pot/docx/"
save_docx_file = save_docx_dir + "results.docx"
os.makedirs(save_docx_dir, exist_ok=True)
doc.save(save_docx_file)
role_path = "prompts/model_role/financial_analyst_assistant_sample"
system_m = open(role_path, 'r', encoding='utf-8').read().replace("\n", " ")
def main():
q_sum = 0
q_correct = 0
adopt_json = json.load(open(adopt_json_file, 'r'))
for dataset_name, adopt_q_list in adopt_json.items():
q_num = 0
file_in = open(dataset_dir + dataset_name + ".txt", 'r', encoding='utf-8')
for query in file_in:
q_num += 1
if q_num in adopt_q_list:
try:
add_doc = Document(save_docx_file)
q_sum += 1
history = []
query_list = query.strip().split("###")
question = query_list.pop(-1)
question = convert_full_text(question).strip()
logger.info("{0}: 题目{1}: {2}".format(dataset_name, q_num, question))
answer_list = query_list
# CoT
CoT1_simple = ("Below is a question about financial calculation.\nQuestion: {question}\n"
"Let's solve the question step by step.")
CoT2_heuristic = ("Please first list all the numerical information in the question. "
"Focus exclusively on the numbers given in the question, assigning each a "
"unique variable."
" Then list all calculation formulas.\nQuestion: {question}\nAnswer:")
CoT3 = ("Below is a question about financial calculation.\nQuestion: {question}\n"
"Please list all variables and required calculation formulas in the question."
"Then let's solve the question step by step.")
CoT_input = CoT3.format(question=question)
history, CoT_output, _ = dolphin_chat_utils.chat(
history, system_m, model1, tokenizer1, model1_name, CoT_input, logger, model_tokenizer_device,
False)
# PoT
PoT_input = "Let's write a Python program."
history, PoT_output, input_all = dolphin_chat_utils.chat(
history, system_m, model1, tokenizer1, model1_name, PoT_input, logger, model_tokenizer_device,
False)
output_code = extract_code(PoT_output)
PoT_answer = check_indent_and_run(output_code)
logger.info("answer_list: {0}\npot_result: {1}\n\n".format(answer_list, PoT_answer))
logger.info("### PoT_input\n{0}\n\n### PoT_output\n{1}\n\n".format(input_all, PoT_output))
if all(elem.strip() in PoT_answer.split(",") for elem in answer_list):
add_title(add_doc, "\n\n{0}: 题目{1}, 答案正确".format(dataset_name, q_num))
add_para(add_doc, question)
q_correct += 1
add_para(add_doc, "### PoT_input\n{0}\n\n### PoT_output\n{1}\n\n".format(input_all, PoT_output))
else:
add_title(add_doc, "\n\n{0}: 题目{1}, 答案错误".format(dataset_name, q_num))
add_para(add_doc, question)
add_para(add_doc, "### CoT_input\n{0}\n\n### CoT_output\n{1}\n\n".format(CoT_input, CoT_output))
add_para(add_doc, "### PoT_input\n{0}\n\n### PoT_output\n{1}\n\n".format(input_all, PoT_output))
add_para_highlight(add_doc, "answer_list: {0}\npot_result: {1}\n\n".format(answer_list, PoT_answer))
add_doc.save(save_docx_file)
logger.info("### q_sum: {0}, q_correct: {1}, accuracy:{2}% ###".format(
q_sum, q_correct, (q_correct / q_sum) * 100))
except Exception as e:
logger.info("{0}, q_{1}, New Error: {2}".format(dataset_name, q_num, str(e)))
continue
if __name__ == '__main__':
main()