Module 'openai' has no attribute 'ChatCompletion'

This is likely my lack of experience with python and jupyter but I’m not sure how to move forward.

I am running python 3.10
I have installed openai

My first cell imports openai and sets the API

import openai
open.api_key = "sk-keyhere"

The second cell defines get_completion, as copied from the course:

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # this is the degree of randomness of the model's output
    )
    return response.choices[0].message["content"]

However, when I attempt to then run the prompt, I get an error referring back to this definition highlighting openai-ChatCompletion.create

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

So if I am understanding this correctly:

The prompt calls get_completion which refers to the openai module and then fails to find ChatCompletion. Now, I know it must be there, as it works on the course notebook without issue.

What am I missing?

Hi @UltraVioletMe I am not sure if this can be the source of the problem

But the API should be set as:

openai.api_key = "sk-keyhere"

Yours is open.api_key which is incorrect, please make the correction and let me know if it works.

I hope it helps!

Well, that’s embarrassing. But it’s not the problem; I had originally copied it from the course example. I’ve also tried

!export OPENAI_API_KEY=‘sk-keyhere’

and creating a .env file in the root directory where that is set.

But you’ve made me think to check my settings on OpenAI and it confirms that the key in question has never been used.

On principle, I generated a new API key and used that, in case there was something wrong with my copy. But it’s the same issue.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 18
      1 text = f"""
      2 You should express what you want a model to do by \ 
      3 providing instructions that are as clear and \ 
   (...)
     11 more detailed and relevant outputs.
     12 """
     13 prompt = f"""
     14 Summarize the text delimited by triple backticks \ 
     15 into a single sentence.
     16 ```{text}```
     17 """
---> 18 response = get_completion(prompt)
     19 print(response)

Cell In[6], line 3, in get_completion(prompt, model)
      1 def get_completion(prompt, model="gpt-3.5-turbo"):
      2     messages = [{"role": "user", "content": prompt}]
----> 3     response = openai.ChatCompletion.create(
      4         model=model,
      5         messages=messages,
      6         temperature=0, # this is the degree of randomness of the model's output
      7     )
      8     return response.choices[0].message["content"]

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

OK intentionally entering the wrong API gets me the same error. I hadn’t thought that was the cause but I’m now going through the posts here that discuss entering the API key.

The end of the course says

You can either set it as the OPENAI_API_KEY environment variable before using the library:

 !export OPENAI_API_KEY='sk-...'

Or, set openai.api_key to its value:

import openai
openai.api_key = "sk-..."

I know the import has worked, as if I remove that, I get an immediate error because it doesn’t know the module.

Now that I am looking at other threads, I can see people talking about setting the API in a .env file, which is what I presume the !export call is doing, but I can’t see where. And I am now wondering if my lack of knowledge of how to set up the environment (this is a new install of Jupyter) is causing me to miss something obvious to everyone else.

Hi @UltraVioletMe, not sure if you got it working, but this post might help if you’re still having issues with setting up the environment in Jupyter: TypeError: '_Environ' object is not callable.

Hope that helps. Let us know if you have any other questions. (:

1 Like

I am really sorry, but I don’t quite get this I’m not calling the _environ object and that’s not in the course.

I can’t make sense of why
openai.api_key = "sk-..."
isn’t working for me and every thread I see on this seems to be using some other way to import the key where there’s clearly some additional steps needed (you must have to put the key somewhere)

OK! I found the answer when I stumbled on this thread:

Although I only installed this last month, for some reason it had installed openai version 0.25.0

I uninstalled and reinstalled which upgraded me to 0.27.8

and now it works perfectly.

Wonderful, glad to hear it’s working for you now! Let us know if you run into any other issues. (: