任务函数:调用 API 并写入结果
(idx, prompt, images, data_source, ground_truth)
| 69 | return done_indices |
| 70 | |
| 71 | def query_openai_task(idx, prompt, images, data_source, ground_truth): |
| 72 | """任务函数:调用 API 并写入结果""" |
| 73 | try: |
| 74 | messages = [] |
| 75 | for p in prompt: |
| 76 | if p["role"] == "user": |
| 77 | content_parts = [{"type": "text", "text": p["content"]}] |
| 78 | for img_path in images: |
| 79 | data_url = image_to_data_url(img_path["image"].replace("file://", "")) |
| 80 | content_parts.append({"type": "image_url", "image_url": {"url": data_url}}) |
| 81 | messages.append({"role": "user", "content": content_parts}) |
| 82 | else: |
| 83 | messages.append(p) |
| 84 | |
| 85 | resp = client.chat.completions.create(model=MODEL_NAME, messages=messages) |
| 86 | solution_str = resp.choices[0].message.content |
| 87 | |
| 88 | if solution_str: |
| 89 | result = { |
| 90 | "index": idx, |
| 91 | "data_source": data_source, |
| 92 | "ground_truth": ground_truth, |
| 93 | "solution_str": solution_str |
| 94 | } |
| 95 | with write_lock: # 保证写入文件不会冲突 |
| 96 | with open(OUTPUT_FILE, "a", encoding="utf-8") as fout: |
| 97 | fout.write(json.dumps(result, ensure_ascii=False) + "\n") |
| 98 | return True |
| 99 | except Exception as e: |
| 100 | print(f"[ERROR] idx {idx} API 调用失败: {e}") |
| 101 | return False |
| 102 | |
| 103 | def main(parquet_files): |
| 104 | df = load_parquet_files(parquet_files) |
nothing calls this directly
no test coverage detected