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

营赢盈英 2024-08-23 10:01:01 阅读 56

题意: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 <code>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",code>

messages=[{"role": "user", "content": prompt}]

)

return response.choices[0].message['content'].strip()

if __name__ == "__main__":

while True:

user_input = input("You: ")

if user_input.lower() in ["quit", "exit", "bye"]:

break

response = chat_gpt(user_input)

print("Bot:", response)

And this is the full error:        这是所有的错误:

... You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at GitHub - openai/openai-python: The official Python library for the OpenAI API 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: v1.0.0 Migration Guide · openai/openai-python · Discussion #742 · GitHub

I tried both upgrading and downgrading through pip.

我尝试了通过pip进行升级和降级。

问题解决:

Try updating to the latest and using:        尝试升级到最新版本并使用:

from openai import OpenAI

client = OpenAI(

# defaults to os.environ.get("OPENAI_API_KEY")

api_key="private",code>

)

def chat_gpt(prompt):

response = client.chat.completions.create(

model="gpt-3.5-turbo",code>

messages=[{"role": "user", "content": prompt}]

)

return response.choices[0].message.content.strip()

EDIT: message.['content'] -> message.content on the return of this function, as a message object is not subscriptable error is thrown while using message.['content']. Also, update link from pointing to the README (subject to change) to migration guide specific to this code.

编辑:在这个函数的返回值中,将 message.['content'] 更改为 message.content,因为当使用 message.['content'] 时会抛出“message 对象不可下标”的错误。此外,更新链接,从指向可能更改的 README 文件改为指向针对此代码特定的迁移指南。



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。