奇怪的错误:无法使用Python和Visual Studio Code或PyCharm访问OpenAI API

问题描述 投票:0回答:1

使用 Visual Studio Code 和 PyCharm,安装 openai (pip install openai) 后,一个奇怪的错误让我绝望!请帮忙!例如,如果我写...

import openai

openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

lista_de_modelos = openai.Model.list()
print(lista_de_modelos)

失败并给我:

PS C:\\proyectoVS_Python\> & "C:/Users/kitkatuser/AppData/Local/Programs/Python/Python312/python.exe" "c:/proyectoVS_Python/import os.py"
Traceback (most recent call last):
File "c:\\proyectoVS_Python\\import os.py", line 5, in \<module\>
lista_de_modelos = openai.Model.list()
^^^^^^^^^^^^^^^^^
File "C:\\Users\\kitkatuser\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\openai_utils_proxy.py", line 22, in __getattr__
return getattr(self.__get_proxied__(), attr)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Users\\kitkatuser\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\openai_utils_proxy.py", line 43, in __get_proxied__  
return self.__load__()
^^^^^^^^^^^^^^^
File "C:\\Users\\kitkatuser\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\openai\\lib_old_api.py", line 33, in __load__
raise APIRemovedInV1(symbol=self.\_symbol)
openai.lib.\_old_api.APIRemovedInV1:

You tried to access openai.Model, but this is no longer supported in openai\>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

PS C:\\proyectoVS_Python\>

我做错了什么?为什么我无法访问openAI,我尝试了几个密钥,相同的程序和安装过程与其他朋友一起运行良好。使用 Pycharm 显示类似。我用了几个程序来尝试但总是相似的反应!我没有找到解决方案或类似问题!我真的很困惑!请帮忙

python visual-studio-code error-handling configuration openai-api
1个回答
0
投票

您使用了错误的方法名称。

OpenAI Python SDK

v1.2
使用以下内容:

import os
from openai import OpenAI
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')

client.models.list()
© www.soinside.com 2019 - 2024. All rights reserved.