Error AttributeError: module 'openai' has no attribute 'ChatCompletion'

Hello
I am trying to use the gpt-4 model in my epro code when trying to run the service it gives me the following error

AttributeError: Module ‘openai’ does not have attribute ‘ChatCompletion’

For what is this? Is it possible to use that model?

1 Like

I’d make sure you have the latest OpenAI library and that it’s compatible with your code.

How can you advise me to have this library?

Attached is a screenshot of where I update the library.

If your code was using that old of a library, it’s likely not compatible with modern library.

You’d have to post code snippets for us to be able to help. Is it running after updating library? Still same error?

This is my code

Is it running after updating library? Still same error?

Since you did such a major update on the library, you might try uninstalling it an installing fresh. Most likely a library error, though…

I uninstalled the library and reinstalled it and it still gives me the same error.

What else can I validate or what changes can be made to the code?

How’s your file called? Do you have any files in your current directory called “openai”?

1 Like

Good idea. It sounds like it’s not reading the library correctly?

The folder where I have the files is called openai-quickstart-python, and inside this folder I have several files that end in .py and the static, templates and venv folders.

And how’s the file you’re trying to execute the code in called?

The file where I have the code is called app.py

Is GPT-4 working for you in Playground?

Can you try to connect via cURL to make sure it works that way?

Yes, I tried it in playgroud and it works:

And it also works with postman

Yeah, I’m out of ideas. With that error, though, it sounds like it’s not loading the correct library.

Maybe set-up a test script with just basics to see if it works?

If it’s strange, I tried on another server, updated the library and it gives me the same error, could it be the python version?

I think you have to use chat.completion in the update now, so it would look something like this:

  completion = openai.chat.completions.create(
    model=self.model,
    messages=self.get_messages(prompt, question),
    max_tokens=500,
    temperature=float(self.temperature),
    
  )
response = completion.choices[0].message.content

1 Like

Yeah, also encountering the same issue.

    params = {
        "model": "gpt-4-vision-preview",
        "messages": PROMPT_MESSAGES,
        "api_key": os.environ["OPENAI_API_KEY"],
        "headers": {"Openai-Version": "2023-11-07"},
        "max_tokens": 500,
    }

    result = openai.ChatCompletion.create(**params)
    print(result.choices[0].message.content)
    return result.choices[0].message.content

Im doing everything exactly as from latest Jason AI video and on his video you can see openai.ChatCompletion.create works fine.

Is it possible it doesn’t work because of this major outage across ChatGPT and API?

You should put a debug statement in the code and inspect the ‘openai’ variable to see what it contains (I think you should be able to see what methods are even on it). It’s probably an outdated version of the library, like others have said. If it was null seems like you’d get a different error.

1 Like

@Shebi You are correct. It feels like they changed it overnight. This works in a Jupyter notebook

    import openai
    response = openai.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a 5 year old"},
            {"role": "user", "content": "what is pi"},
        ] 
    )
    
    message_content = response.choices[0].message.content
    print (message_content)