“You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0“

题意:OpenAI API 错误:“尝试访问 openai.ChatCompletion,但在 openai>=1.0.0 版本中已不再支持此功能”

问题背景:

I am currently working on a chatbot, and as I am using Windows 11 it does not let me migrate to newer OpenAI library or downgrade it. Could I replace the ChatCompletion function with something else to work on my version?

我目前正在开发一个聊天机器人,由于我使用的是Windows 11系统,它不允许我迁移到更新的OpenAI库或将其降级。我能否用其他东西替换ChatCompletion函数,以便在我的当前版本上工作?

This is the code:        这是代码

import openai

openai.api_key = "private"

def chat_gpt(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": p
C:\Users\Administrator\.conda\envs\pytorch\python.exe "D:/Program Files/JetBrains/PyCharm 2024.1.3/plugins/python/helpers/pydev/pydevconsole.py" --mode=client --host=127.0.0.1 --port=65099 import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['C:\\Users\\Administrator\\PycharmProjects\\pythonProject']) PyDev console: starting. Python 3.11.9 | packaged by Anaconda, Inc. | (main, Apr 19 2024, 16:40:41) [MSC v.1916 64 bit (AMD64)] on win32 >>> runfile('C:\\Users\\Administrator\\PycharmProjects\\pythonProject\\multi-modual classifiacation\\multi-modual.py', wdir='C:\\Users\\Administrator\\PycharmProjects\\pythonProject\\multi-modual classifiacation') Traceback (most recent call last): File "D:\Program Files\JetBrains\PyCharm 2024.1.3\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode coro = func() ^^^^^^ File "<input>", line 1, in <module> File "D:\Program Files\JetBrains\PyCharm 2024.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Program Files\JetBrains\PyCharm 2024.1.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:\Users\Administrator\PycharmProjects\pythonProject\multi-modual classifiacation\multi-modual.py", line 26, in <module> response = openai.Completion.create( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\.conda\envs\pytorch\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__ raise APIRemovedInV1(symbol=self._symbol) openai.lib._old_api.APIRemovedInV1: You tried to access openai.Completion, 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
03-08
### 解决方案 当遇到 `APIRemovedInV1: You tried to access openai.Completion, but this is no longer supported in openai>=1.0.0` 错误时,表明所使用的 API 版本已更新至 v1.0 或更高版本,在这些新版本中,某些旧的方法已被移除或更改。为了使代码能够正常工作并保持兼容性,应当按照新的 API 接口标准调整代码。 在新版 OpenAI Python SDK 中,创建补全请求的方式发生了变化。现在推荐使用 `openai.ChatCompletion.create()` 方法来替代原来的 `openai.Completion.create()`[^1]。以下是具体的修改建议: #### 修改前(适用于老版SDK) ```python response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=60 ) ``` #### 修改后(适用于v1及以上版本SDK) ```python from openai import ChatCompletion response = ChatCompletion.create( model="gpt-3.5-turbo", # 使用model参数指定模型名称而非engine messages=[{"role": "user", "content": prompt}], # 将prompt转换成messages列表形式 temperature=0.7, # 可选:设置temperature控制回复随机程度,默认值为1 max_tokens=60 # 设置最大返回token数量 ) ``` 需要注意的是,在新版接口中,不再使用 `engine` 参数而是改用了 `model` 来指明要调用的具体模型;同时输入提示也需要被封装在一个消息对象数组内传递给 `messages` 参数[^2]。 此外,如果项目依赖于特定的老版本功能,则可以考虑暂时锁定 pip 安装包的版本号以防止自动升级造成影响,直到完成必要的迁移工作为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值