如何用Python通过Fine-tune ID循环检测OpenAI微调完成状态
使用while循环检测OpenAI微调任务状态
你已完成训练文件上传并创建微调任务,但此时任务处于pending状态,因此fine_tuned_model字段为null。要获取可用的微调模型ID,可通过Fine-tune ID编写while循环轮询任务状态,直到任务完成或失败。
实现代码
import time import openai # 替换为你的Fine-tune ID(创建任务时返回的id字段值) fine_tune_id = "ft-IqBdk4WJETm4KakIzfZeCHgS" while True: # 查询当前微调任务状态 status_response = openai.FineTune.retrieve(fine_tune_id) current_status = status_response["status"] current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print(f"[{current_time}] 微调任务状态: {current_status}") if current_status == "succeeded": fine_tuned_model = status_response["fine_tuned_model"] print(f"微调完成!可用模型ID: {fine_tuned_model}") break elif current_status == "failed": print("微调任务失败,请检查任务详情或训练数据") # 可选:打印失败详情 # print("失败详情:", status_response.get("status_details")) break else: # 任务仍在执行,间隔60秒后再次查询(可按需调整间隔) time.sleep(60)
关键说明
- 状态判断:OpenAI微调任务常见状态包括
pending(待处理)、running(运行中)、succeeded(成功完成)、failed(失败)。仅当状态为succeeded时,fine_tuned_model字段才会返回可用的模型ID。 - 查询间隔:设置合理的等待间隔(如60秒),避免过于频繁的API请求触发限流。
- 健壮性优化:若需要更稳定的实现,可添加
try-except块处理网络异常、API限流等问题。
内容的提问来源于stack exchange,提问作者Martin
相关产品推荐
相关产品推荐

