|
题意:OpenAIAPI错误:“尝试访问openai.ChatCompletion,但在openai>=1.0.0版本中已不再支持此功能”问题背景:Iamcurrentlyworkingonachatbot,andasIamusingWindows11itdoesnotletmemigratetonewerOpenAIlibraryordowngradeit.CouldIreplacethe ChatCompletion functionwithsomethingelsetoworkonmyversion?我目前正在开发一个聊天机器人,由于我使用的是Windows11系统,它不允许我迁移到更新的OpenAI库或将其降级。我能否用其他东西替换ChatCompletion函数,以便在我的当前版本上工作?Thisisthecode: 这是代码importopenaiopenai.api_key="private"defchat_gpt(prompt):response=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role":"user","content":prompt}])returnresponse.choices[0].message['content'].strip()if__name__=="__main__":whileTrue:user_input=input("You:")ifuser_input.lower()in["quit","exit","bye"]:breakresponse=chat_gpt(user_input)print("Bot:",response)Andthisisthefullerror: 这是所有的错误:...Youtriedtoaccessopenai.ChatCompletion,butthisisnolongersupportedinopenai>=1.0.0-seetheREADMEat GitHub-openai/openai-python:TheofficialPythonlibraryfortheOpenAIAPI fortheAPI.Youcanrun openaimigrate toautomaticallyupgradeyourcodebasetousethe1.0.0interface.Alternatively,youcanpinyourinstallationtotheoldversion,e.g.
Adetailedmigrationguideisavailablehere: v1.0.0MigrationGuide·openai/openai-python·Discussion#742·GitHubItriedbothupgradinganddowngradingthroughpip.我尝试了通过pip进行升级和降级。问题解决:Tryupdatingtothelatestandusing: 尝试升级到最新版本并使用:fromopenaiimportOpenAIclient=OpenAI(#defaultstoos.environ.get("OPENAI_API_KEY")api_key="private",)defchat_gpt(prompt):response=client.chat.completions.create(model="gpt-3.5-turbo",messages=[{"role":"user","content":prompt}])returnresponse.choices[0].message.content.strip()EDIT: message.['content'] -> message.content onthereturnofthisfunction,asa messageobjectisnotsubscriptable erroristhrownwhileusing message.['content'].Also,updatelinkfrompointingtothe README (subjecttochange)tomigrationguidespecifictothiscode.编辑:在这个函数的返回值中,将 message.['content'] 更改为 message.content,因为当使用 message.['content'] 时会抛出“message对象不可下标”的错误。此外,更新链接,从指向可能更改的 README 文件改为指向针对此代码特定的迁移指南。
|
|