| 90 | print(f"[{i}] Failed after retries: {e}") |
| 91 | return i, None, cur_token_cost |
| 92 | def evaluate_example(query_file,result_file,output_file,dataset): |
| 93 | total_token_cost = 0 |
| 94 | DEEPSEEK_MODEL = "xxxx" |
| 95 | DEEPSEEK_URL = "xxxx" |
| 96 | client = OpenAI(api_key='xxx',base_url=DEEPSEEK_URL) |
| 97 | |
| 98 | queries = [] |
| 99 | with open(query_file, "r", encoding="utf-8") as infile: |
| 100 | for line_number, line in enumerate(infile, start=1): |
| 101 | line = line.strip() |
| 102 | if not line: |
| 103 | continue |
| 104 | try: |
| 105 | json_obj = json.loads(line) |
| 106 | if "mix" in query_file: |
| 107 | query = json_obj.get("input") |
| 108 | MAX_QUERIES=130 |
| 109 | else: |
| 110 | query = json_obj.get("query") |
| 111 | MAX_QUERIES=100 |
| 112 | queries.append(query) |
| 113 | except json.JSONDecodeError as e: |
| 114 | print( |
| 115 | f"JSON decoding error in file {query_file} at line {line_number}: {e}" |
| 116 | ) |
| 117 | queries = queries[:MAX_QUERIES] |
| 118 | with open(result_file, "r") as f: |
| 119 | answers = f.readlines() |
| 120 | answers = [json.loads(i)["answer"] for i in answers][:MAX_QUERIES] |
| 121 | with open(f"datasets/{dataset}/{dataset}.jsonl", "r") as f: |
| 122 | benchmarks=f.readlines() |
| 123 | benchmarks=[json.loads(i)["answers"] for i in benchmarks][:MAX_QUERIES] |
| 124 | |
| 125 | total_token_cost = 0 |
| 126 | results = [None] * len(queries) |
| 127 | |
| 128 | with ThreadPoolExecutor(max_workers=8) as executor: |
| 129 | futures = { |
| 130 | executor.submit(evaluate_item, i, q, a, b, tokenizer, ): i |
| 131 | for i, (q, a, b) in enumerate(zip(queries, answers, benchmarks)) |
| 132 | } |
| 133 | |
| 134 | for future in as_completed(futures): |
| 135 | i, evaluation, token_cost = future.result() |
| 136 | total_token_cost += token_cost |
| 137 | results[i] = evaluation |
| 138 | if evaluation is not None: |
| 139 | print(f"Successfully evaluated {i+1}/{len(queries)}") |
| 140 | |
| 141 | # 只写入成功的结果 |
| 142 | with jsonlines.open(output_file, mode="w") as writer: |
| 143 | for eval_item in results: |
| 144 | if eval_item is not None: |
| 145 | writer.write(eval_item) |
| 146 | |
| 147 | return total_token_cost |