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

Describe the bug

The feature was working few hours ago, now it’s giving error.

To Reproduce

import openai

openai.api_key = 'key'

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

Code snippets

No response

OS

macOS

Python version

Python v3.7.1

Library version

openai v0.26.5

no function as ChatCompletion the defined methods

import openai
print(openai.__dir__())

[‘name’, ‘doc’, ‘package’, ‘loader’, ‘spec’, ‘path’, ‘file’, ‘cached’, ‘builtins’, ‘annotations’, ‘os’, ‘Optional’, ‘error’, ‘util’, ‘version’, ‘openai_response’, ‘api_requestor’, ‘openai_object’, ‘api_resources’, ‘Answer’, ‘Classification’, ‘Completion’, ‘Customer’, ‘Edit’, ‘Deployment’, ‘Embedding’, ‘Engine’, ‘ErrorObject’, ‘File’, ‘FineTune’, ‘Image’, ‘Model’, ‘Moderation’, ‘Search’, ‘APIError’, ‘InvalidRequestError’, ‘OpenAIError’, ‘api_key’, ‘api_key_path’, ‘organization’, ‘api_base’, ‘api_type’, ‘api_version’, ‘verify_ssl_certs’, ‘proxy’, ‘app_info’, ‘enable_telemetry’, ‘ca_bundle_path’, ‘debug’, ‘log’, ‘all’]

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 27 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Don’t name your python file openai.py!

running the command:

pip install --upgrade openai Alternatively, to install it manually, do the following:

  1. Download openai-0.27.0-py3-none-any.whl from https://pypi.org/project/openai/#files
  2. Run pip install openai-0.27.0-py3-none-any.whl

Please bump your openai v0.26.5 to v0.27.0.

in Jupyter notebook, you have to restart the kernel after upgrading openai for it to work

I wasted a lot of time to try to get it working. Maybe this helps someone:

AFTER updating with pip install --upgrade openai

Go here:

In pycharm go to settings > project blabla > python interpreter > click the + sign > find openai > click "Specify version: 0.27.0" VERY IMPORTANT > click install package. Restart pycharm.

It should work now.

Restarting Python itself may solve the problem. It worked for me.

use jupyter

  1. pip uninstall openai
  2. close kernel (shut down kernel)
  3. open notebook
  4. !pip install --upgrade openai
  5. its run

I solved the issue by upgrading the openai v27.0 by

pip install --upgrade openai Example Usage:

ChatGPT API has been added in Promptify; you can use it like this.

from promptify import OpenAI
from promptify import Prompter

sentence     =  """The patient is a 93-year-old female with a medical                   
                history of chronic right hip pain, osteoporosis,                    
                hypertension, depression, and chronic atrial                        
                fibrillation admitted for evaluation and management                
                of severe nausea and vomiting and urinary tract                
                infection"""

model        = OpenAI('key', model = "gpt-3.5-turbo")
nlp_prompter = Prompter(model)


result       = nlp_prompter.fit('ner.jinja',
                          domain      = 'medical',
                          text_input  = sentence, 
                          labels      = None)

eval(result['text'])

Output

[{'E': '93-year-old', 'T': 'Age'},
 {'E': 'chronic right hip pain', 'T': 'Medical Condition'},
 {'E': 'osteoporosis', 'T': 'Medical Condition'},
 {'E': 'hypertension', 'T': 'Medical Condition'},
 {'E': 'depression', 'T': 'Medical Condition'},
 {'E': 'chronic atrial fibrillation', 'T': 'Medical Condition'},
 {'E': 'severe nausea and vomiting', 'T': 'Symptom'},
 {'E': 'urinary tract infection', 'T': 'Medical Condition'},
 {'Branch': 'Internal Medicine', 'Group': 'Geriatrics'}]

Don’t name your python file openai.py!

thanks, it solved my problem

  1. Check your python-version it needs to be >= 3.7
  2. Check your openai version it needs to v27.0 or greater.
  3. If above points doesn’t help you make sure you don’t have file named openai.py
  4. Lastly, if you’re using conda and tried to install using both conda and pip commands. Uninstall openai from your conda env and try to run it again.

I did this in PowerShell, My ‘venv’ virtual environment and windows CMD. This was the only thing that worked for me.

python3 -m pip install --upgrade openai

Folks, this is a common Python issue, it is not an issue on the OpenAI package side, if you create a new virtual env and install the package in there, it will work without issue. But the global python package version issue is likely causing this problem for everyone.

Please open new issues if this does not work using a virtual env!

I wasted a lot of time to try to get it working. Maybe this helps someone:

AFTER updating with pip install --upgrade openai

Go here:

In pycharm go to settings > project blabla > python interpreter > click the + sign > find openai > click "Specify version: 0.27.0" VERY IMPORTANT > click install package. Restart pycharm.

It should work now.

Do you have any idea how to get it working if you’re not using Pycharm? Im having the same issue despite updating openai.