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)
Don’t name your python file
openai.py
!running the command:
pip install --upgrade openai
Alternatively, to install it manually, do the following: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
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.
Output
More examples: Question-Answering, Relation-Extraction, Classification, NER etc in three lines of code
If you are still getting issues while trying out ChatGPT API, check this Google-Colab notebook
thanks, it solved my problem
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!
Do you have any idea how to get it working if you’re not using Pycharm? Im having the same issue despite updating openai.