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