Jupyter中OpenAI对象无fine_tunes属性报错,求解决方案
AttributeError: 'OpenAI' object has no attribute 'fine_tunes' 解决方案
报错信息
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[10], line 5 2 dataset_path = r"*****" 4 # Fine-tune the model with the examples from the dataset ----> 5 fine_tuned_model_id = fine_tune_model_with_examples(dataset_path) 6 print(f"Fine-tuned model ID: {fine_tuned_model_id}") 8 # Example source code to analyze Cell In[9], line 34, in fine_tune_model_with_examples(dataset_path) 26 # Launch fine-tuning 27 fine_tuning_parameters = { 28 "training_file": train_file_id, 29 "validation_file": test_file_id, 30 "model": "gpt-3.5-turbo", 31 "n_epochs": 4 32 } ---> 34 response = client.fine_tunes.create(**fine_tuning_parameters) 35 return response['id'] AttributeError: 'OpenAI' object has no attribute 'fine_tunes'
引发报错的代码
# Function to fine-tune the model def fine_tune_model_with_examples(dataset_path): # Load and balance the dataset balanced_dataset = load_and_balance_dataset(dataset_path) # Split the dataset into training and test sets train_set, test_set = split_dataset(balanced_dataset) # Prepare fine-tuning data in chat format train_examples = prepare_fine_tuning_data(train_set) test_examples = prepare_fine_tuning_data(test_set) # Save training and test data to JSONL files save_to_jsonl(train_examples, "train_fine_tuning_data.jsonl") save_to_jsonl(test_examples, "test_fine_tuning_data.jsonl") # Upload training and test data to OpenAI with open("train_fine_tuning_data.jsonl", 'rb') as train_file: train_file_response = client.files.create(file=train_file, purpose='fine-tune') with open("test_fine_tuning_data.jsonl", 'rb') as test_file: test_file_response = client.files.create(file=test_file, purpose='fine-tune') train_file_id = train_file_response.id test_file_id = test_file_response.id # Launch fine-tuning fine_tuning_parameters = { "training_file": train_file_id, "validation_file": test_file_id, "model": "gpt-3.5-turbo", "n_epochs": 4 } response = client.fine_tunes.create(**fine_tuning_parameters) return response['id']
问题背景
在Jupyter Notebook 7.0.8环境下用Python开发,运行代码时出现上述报错。已更新所有依赖包,且按照OpenAI API更新说明将openai.FineTune.create()调整为client.fine_tunes.create(),但错误仍存在。
解决方案
核心原因
你使用的是OpenAI Python SDK v1.x版本,该版本对微调接口做了重构:旧版的fine_tunes模块被替换为fine_tuning.jobs,同时参数结构和返回值处理逻辑也有变化。
具体修改步骤
- 确认SDK版本:执行
pip show openai查看版本号,若为v1.x开头,则需按以下方式修改代码。 - 调整接口调用路径:将
client.fine_tunes.create()替换为client.fine_tuning.jobs.create()。 - 修改参数结构:原参数
n_epochs需要嵌套在hyperparameters字典中传递。 - 更新返回值获取方式:v1.x版本中返回的是对象,需用
response.id替代response['id']。
修改后的代码片段
# Launch fine-tuning fine_tuning_parameters = { "training_file": train_file_id, "validation_file": test_file_id, "model": "gpt-3.5-turbo", "hyperparameters": {"n_epochs": 4} } response = client.fine_tuning.jobs.create(**fine_tuning_parameters) return response.id
内容的提问来源于stack exchange,提问作者Codezilla
相关产品推荐
相关产品推荐

