OpenAI API错误:”您试图访问openai.Model,但这在openai>=1.0.0中不再受支持”

使用Visual Studio Code和PyCharm,在安装openai(pip install openai)后,我遇到了一个奇怪的错误 – 请帮助我解决这个问题。

例如,如果我编写以下代码:

import openaiopenai.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/742PS C:\\proyectoVS_Python>

我做错了什么?为什么我无法访问OpenAI?我已经尝试了几个密钥,同样的程序和安装过程在其他朋友那里运行得很好。使用Pycharm也显示类似的错误。我尝试了几个程序,但总是得到类似的回应!我找不到解决方案或类似的问题!我真的很困惑!请帮助我


回答:

问题

您尝试使用的函数名与OpenAI Python SDK版本1.0.0或更高版本不兼容。

旧版SDK(即版本0.28)使用以下函数名:

client.Model.list

新版SDK(即版本1.0.0或更高版本)使用以下函数名:

client.models.list

注意:API对大小写敏感(即client.Models.list在新版SDK中将无法工作)。

解决方案

尝试以下代码:

import osfrom openai import OpenAIclient = OpenAI()OpenAI.api_key = os.getenv('OPENAI_API_KEY')client.models.list()

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注