题意:发生错误:模块 'openai' 没有属性 'ChatCompletion'
问题背景:
I'm trying to build a discord bot that uses the GPT-4 API to function as a chatbot on discord. I have the most recent version of the OpenAI library but when I run my code it tells me "An error occurred: module 'openai' has no attribute 'ChatCompletion'"
我正在尝试构建一个使用 GPT-4 API 的 Discord 机器人,使其在 Discord 上作为聊天机器人运行。我已经安装了最新版的 OpenAI 库,但当我运行代码时,它提示我“发生错误:模块 'openai' 没有属性 'ChatCompletion'”
I tried uninstalling and reinstalling the OpenAI library, I tried using the completions endpoint and got the error "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
我尝试卸载并重新安装 OpenAI 库,也尝试使用 completions 端点,但收到错误提示:“这是一个聊天模型,不支持 v1/completions 端点。你是否打算使用 v1/chat/completions?”
This is the snippet of code that's giving me issues:
这是导致我问题的代码片段:
async def get_gpt_response(prompt, history):
history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
completions = openai.ChatCompletion.create(
engine=config["model"],
prompt=chat_prompt,
max_tokens=config["max_tokens"],
n=1,
temperature=config["temperature"],
)
return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()
问题解决:
Make sure you don’t have a file called “openai.py”
确保你没有一个名为 “openai.py” 的文件