I’m having trouble with the new OpenAI SDK v1.0. My code for a business plan generator isn’t working anymore. It keeps giving me an AttributeError
saying the ‘OpenAI’ object doesn’t have a ‘Completion’ attribute. I’m not sure how to fix this. Can someone point me in the right direction?
Here’s a snippet of what I’m trying to do:
def create_section_content(section_title, project_description):
response = ai_client.generate(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a business plan expert."},
{"role": "user", "content": f"Write a detailed {section_title} section for a business plan. The project is about: {project_description}"}
],
max_tokens=300
)
return response.choices[0].message.content.strip()
Any ideas on how to update this for the new SDK version? Thanks!
I’ve been grappling with the OpenAI SDK v1.0 changes too. The main issue is that they’ve completely revamped the API structure. To fix your code, you need to use the chat completions endpoint instead of the old completion class.
Here’s what worked for me:
Update your import statement:
from openai import OpenAI
client = OpenAI()
Modify your function:
def create_section_content(section_title, project_description):
response = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[
{'role': 'system', 'content': 'You are a business plan expert.'},
{'role': 'user', 'content': f'Write a detailed {section_title} section for a business plan. The project is about: {project_description}'}
],
max_tokens=300
)
return response.choices[0].message.content.strip()
This should resolve the AttributeError and get your business plan generator back on track.
I’ve been working with the OpenAI SDK v1.0 for a few weeks now, and it’s clear that the new version requires a few changes compared to the old one. The key difference is that you’ll need to use the chat completions API rather than the legacy completion endpoint.
For instance, ensure your import is set up like this:
from openai import OpenAI
client = OpenAI()
Then modify your function accordingly:
def create_section_content(section_title, project_description):
response = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[
{'role': 'system', 'content': 'You are a business plan expert.'},
{'role': 'user', 'content': f'Write a detailed {section_title} section for a business plan. The project is about: {project_description}'}
],
max_tokens=300
)
return response.choices[0].message.content.strip()
This should resolve the AttributeError and help your business plan generator run smoothly.
The issue you’re facing is due to significant changes in the OpenAI SDK v1.0. The ‘Completion’ class has been deprecated, and the API structure has been overhauled. To resolve this, you’ll need to update your code to use the new chat completions endpoint.
Here’s how you can modify your function:
def create_section_content(section_title, project_description):
response = ai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a business plan expert."},
{"role": "user", "content": f"Write a detailed {section_title} section for a business plan. The project is about: {project_description}"}
],
max_tokens=300
)
return response.choices[0].message.content.strip()
Also, ensure you’re initializing the client correctly:
from openai import OpenAI
ai_client = OpenAI()
This should resolve your AttributeError and get your business plan generator working again with the new SDK version.
I recently faced a similar issue when upgrading to the OpenAI SDK v1.0. The main change is that the ‘Completion’ class has been replaced with the ‘ChatCompletion’ class for text generation tasks.
To fix your code, you’ll need to update the method name from ‘generate’ to ‘chat.completions.create’. Also, the response structure has changed slightly. Here’s how you can modify your function:
def create_section_content(section_title, project_description):
response = ai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a business plan expert."},
{"role": "user", "content": f"Write a detailed {section_title} section for a business plan. The project is about: {project_description}"}
],
max_tokens=300
)
return response.choices[0].message.content.strip()
This should resolve the AttributeError you’re encountering. Remember to import the OpenAI client correctly at the top of your file: ‘from openai import OpenAI’. Hope this helps you get your business plan generator back up and running!
yo, i had the same prob with the new sdk. it’s a pain, right? quick fix: change ‘generate’ to ‘chat.completions.create’ and tweak the response handling. like this:
response = ai_client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[
{'role': 'system', 'content': 'You're a biz plan pro.'},
{'role': 'user', 'content': f'Write {section_title} for biz plan about: {project_description}'}
],
max_tokens=300
)
should work now. good luck!