添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hello.

I cant access gpt-3.5-turbo with python because I seem to be getting the error " AttributeError: module ‘openai’ has no attribute ‘ChatCompletion’"

I have updated to openai v0.27.0, as well as tried using new API keys just incase that was causing the issue. Ive double checked that I actually have updated to the new version of openai 0.27.0 by running “pip list”. Ive also tried " pip install --upgrade openai " and “pip install openai-0.27.0-py3-none-any.whl”

Im not sure what else I can try, does anyone have any ideas?
Thanks.

Conversational models such as gpt-3.5-turbo can be called using the chat completions endpoint.

import openai openai.api_key = "sk-..." # supply your API key however you choose 
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world!"}]) 
print(completion.choices[0].message.content)

@theemilydyson please share the code you’re using in proper markdown.

Version: 0.27.0

Summary: Python client library for the OpenAI API

Home-page: GitHub - openai/openai-python: The OpenAI Python library provides convenient access to the OpenAI API from applications written in the Python language.

Author: OpenAI

Author-email: [email protected]

License:

Location: /usr/local/lib/python3.11/site-packages

Requires: aiohttp, requests, tqdm

Required-by:
Screenshot 2023-03-02 at 3.09.51 pm1170×696 168 KB

import openai openai.api_key = "sk-..." # supply your API key however you choose 
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world!"}]) 
print(completion.choices[0].message.content)

Hey @theemilydyson and @tamalon1

I am back to my desk and ran some tests.

I am currently on python 3.7.4 and using the the same openai module, I was able to call the chat completion endpoint.

And as it turns out the package required python 3.7.1 and upwards, it’s mentioned in the end of docs on the python package index page I shared above.

Requirements

  • Python 3.7.1+
  • In general, we want to support the versions of Python that our customers are using. If you run into problems with any version issues, please let us know at on our support page.

    @sps Did you mean it only works with python 3.7.x?

    theemilydyson had python 3.11? /usr/local/lib/python3.11/site-packages

    I got the same error when trying this

    import os
    import openai
    openai.api_key = os.getenv("OPENAI_API_KEY")
    response = openai.ChatCompletion.create(
                    model="gpt-3.5-turbo",
                    messages= [
                        {'role': 'user', 'content': 'Translate the following English text to French: '}
    

    This is my pip show openai output

    ame: openai
    Version: 0.27.0
    Summary: Python client library for the OpenAI API
    Home-page: https://github.com/openai/openai-python
    Author: OpenAI
    Author-email: [email protected]
    License: 
    Location: /Users/c/Projects/chat/venv/lib/python3.10/site-packages
    Requires: tqdm, requests, aiohttp
    Required-by: 
    

    I could find ChatCompletion in the installed package though
    Screen Shot 2023-03-03 at 5.20.54 PM1232×1068 91.5 KB

    There’s no straightforward answer to the problem AFAIK. Could be multiple versions of python, could be improperly installed package.

    Can you share the output of the following:

  • pip --version
  • python -m pip --version
  • pip3 --version
  • It worked after I deleted venv and reinstalled openai. Thanks

    Btw, this was what I got before deleting venv.

    pip 21.2.4 from /Users/j/Projects/chat/venv/lib/python3.10/site-packages/pip (python 3.10)
    Screen Shot 2023-03-03 at 10.20.51 PM1136×312 59.9 KB
                  

    I was getting this problem as well. The example worked using curl but not python. But I was able to get it working in my virtual environment by updating to openapi version 0.27.0. I probably updated in a roundabout way.

  • Checked Python client library for the OpenAI API version with pip show openai. It was Version: 0.19.0.
  • In requirements.txt file, updated the openapi version to openai==0.27.0.
  • Ran pip install -r requirements.txt.
  • Ran pip show openai again. It showed version 0.27.0 now.
  • Ran the python sample again and didn’t get the no attribute ‘ChatCompletion’ error.
  • Subsequently updated requirements.txt file to openai>=0.27.0.
  •