MCPcopy Create free account
hub / github.com/Felixgithub2017/MMCU / eval

Function eval

TestChatGPT.py:83–154  ·  view source on GitHub ↗
(args, subject, dev_df, test_df)

Source from the content-addressed store, hash-verified

81 f.write(contents)
82
83def eval(args, subject, dev_df, test_df):
84 logfile = "chatgptestlogfile0512"
85 cors = []
86 #labels = []
87 preds = []
88 for i in range(test_df.shape[0]):
89 # get prompt and make sure it fits
90 k = args.ntrain
91 prompt_end = format_example(test_df, i, include_answer=False)
92 train_prompt = gen_prompt(dev_df, k)
93 prompt = train_prompt + prompt_end
94
95 #print("train_prompt:", train_prompt)
96 print("题目:", prompt)
97 with open(logfile, 'a', encoding='utf8') as f:
98 f.write(prompt+"\n")
99
100 try:
101 label = test_df.iloc[i, test_df.shape[1]-1]
102 # A B C D 特殊处理
103 label = label.replace(" ", "").replace("A", "A").replace("B", "B").replace("C", "C").replace("D", "D")
104 label = label.replace("\u3000", "").replace(",", "")
105 print("正确答案:", label)
106 with open(logfile, 'a', encoding='utf8') as f:
107 f.write("正确答案:"+label+"\n")
108 except Exception as e:
109 print(e)
110 break
111
112 while True:
113 try:
114 time.sleep(1)
115 pred = plain_chat(prompt)
116 pred = pred.replace("、", "").replace(".", "").replace(",", "").replace(";", "").replace(",", "")
117 try:
118 # 识别答案pattern
119 pred = find_valid_substrings(pred)[0]
120 except Exception as e:
121 print(e)
122 pred = "未成功回答"
123 print("模型预测答案:", pred)
124 with open(logfile, 'a', encoding='utf8') as f:
125 f.write("模型预测答案:"+pred+"\n")
126 break
127 except Exception as e:
128 print(e)
129 print("pausing")
130 time.sleep(2)
131 continue
132
133 try:
134 cor = pred == label
135 print("是否答对:", cor)
136 with open(logfile, 'a', encoding='utf8') as f:
137 f.write("是否答对:"+str(cor)+"\n")
138 cors.append(cor)
139 preds.append(pred+"|||"+label+"|||"+str(cor))
140

Callers 1

mainFunction · 0.70

Calls 4

format_exampleFunction · 0.70
gen_promptFunction · 0.70
plain_chatFunction · 0.70
find_valid_substringsFunction · 0.70

Tested by

no test coverage detected