OpenAI v1.0.0+版本openai.File调用报错的解决方案求助
解决OpenAI微调API版本兼容问题
问题背景
正在进行ChatGPT微调,目标是让模型根据特定主题生成相关子主题/概念。训练数据集、执行脚本及报错信息如下:
训练数据集
{"prompt":"Wellbeing of Child->","completion":" Immunization, Equal nurturing of boys and girls, Parents Mental health, Shaking baby, Children with disabilities, Dental care (only gum care), Importance of height and weight measurement, Feeding during sickness, Playing with rattle, Make mealtimes fun, Avoid physical maltreatment, Reducing screen time, Childrens common diseases, Nurturing care for children\n"} {"prompt":"Learning and Development->","completion":" Reducing screen time, Ways of learnings, Family values, Show name talk, Importance of height and weight measurement, Toilet training, Sorting and matching, Drowning, Learning through play\n"} {"prompt":"Hygiene and Safety->","completion":" Hygiene, Safety, Drowning, Handwashing, Avoid physical maltreatment\n"} {"prompt":"Breastfeeding->","completion":" Importance of Breastfeeding, Method of breastfeeding, Demerits of Infant formula milk, Maternal Nutrition, Sore Nipples, Feeding during sickness, Complementary feeding, Healthy feeding, Interactions with baby during breastfeeding\n"}
执行脚本
import openai import json import os api_key =" " openai.api_key = api_key ##检查训练数据 !openai tools fine_tunes.prepare_data -f training_data_prepared.jsonl -q response = openai.File.create( file=open("training_data_prepared.jsonl","rb"), purpose = "fine-tune") print(response)
报错信息
APIRemovedInV1:
You tried to access openai.File, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
You can runopenai migrateto automatically upgrade your codebase to use the 1.0.0 interface.
Alternatively, you can pin your installation to the old version, e.g.pip install openai==0.28
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
可行解决方案
方案1:适配OpenAI v1.0+版本代码
OpenAI v1.0+重构了API调用逻辑,需改用客户端模式调用,同时微调流程也有更新。修改后的完整脚本如下:
from openai import OpenAI api_key = "你的API密钥" client = OpenAI(api_key=api_key) # 训练数据检查命令不受版本影响,可继续使用 !openai tools fine_tunes.prepare_data -f training_data_prepared.jsonl -q # 上传训练文件(替换原openai.File.create) file_response = client.files.create( file=open("training_data_prepared.jsonl", "rb"), purpose="fine-tune" ) print("文件上传结果:", file_response) # 启动微调任务(v1.0+使用fine_tuning.jobs.create) fine_tune_response = client.fine_tuning.jobs.create( training_file=file_response.id, model="gpt-3.5-turbo" # 可根据需求选择gpt-3.5-turbo-0613等特定版本 ) print("微调任务启动结果:", fine_tune_response)
方案2:降级到旧版本OpenAI库
如果不想修改代码,直接安装兼容旧API的版本即可:
- 卸载当前版本:
pip uninstall -y openai
- 安装指定旧版本:
pip install openai==0.28
- 验证版本:
pip show openai
确认版本显示为0.28.x后,原脚本无需修改即可正常运行。
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

