Error with openai.Completion.create()

I’ve searched the forum and the internet and I can not figure out how to fix this issue. Can someone please tell me how to fix this problem?

#code
import openai
import os

openai.api_key = os.getenv("OPENAI_API_KEY", "XXXXXXXXX") 

def chat_with_gpt():
print("Welcome to ChatGPT Clone! Type 'exit' to end the conversation.\n")

# Conversation history
messages = [{"role": "system", "content": "You are a helpful assistant."}]

while True:
    # Get user input
    user_input = input("You: ")
    
    if user_input.lower() == "exit":
        print("Goodbye!")
        break

    # Append user message to history
    messages.append({"role": "user", "content": user_input})

    try:
        # Call the OpenAI API
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", 
        )
        # Get assistant's reply
        assistant_reply = response.choices[0].message.content
        print(f"ChatGPT: {assistant_reply}")

        # Append assistant response to history
        messages.append({"role": "assistant", "content": assistant_reply})
    except Exception as e:
        print(f"An error occurred: {e}")
        break

if __name__ == "__main__":
chat_with_gpt()

Error:
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at {link} for the API.

You can run openai migrate to 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: {link}

runfile(‘/Users/joshua.faulkner/.spyder-py3/ChatGPTclone.py’, wdir=‘/Users/joshua.faulkner/.spyder-py3’)
Welcome to ChatGPT Clone! Type ‘exit’ to end the conversation.

You: Hello
An error occurred:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at {link} for the API.

You can run openai migrate to 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: {link}


OpenAI Python library version: openai 1.56.0

Python Version: Python 3.12.7


Thanks in advance for any help you can give me.

1 Like

Welcome to the community!

What error are you getting?

Are you using the latest OpenAI library?

1 Like

Sorry, I updated the original post with the error. Thanks for the reply.

I know it has to do with depreciated code and I’ve found many potential solutions on the Internet, but none of them seem to work for me. I’m wondering if there is something specific to my code that is different to the others on the Internet.

I’m using openai 1.56.0

1 Like

No worries. Sounds like you’re using old code and new library. I would look for new code.

What are you trying to do? Where did you find the code you’re trying to get working?

1 Like

Right now I’m just trying to make a Chat GPT clone by using the openai API. I’ve been using a combination of code snippets I’ve found on the internet and Chat GPT generated code. Any suggestions on where I should be looking for up-to-date code? I’m pretty new to this, so any direction is helpful.