发生错误:模块“openai”没有属性“ChatCompletion”

Ant*_*ony 8 python openai-api gpt-4

我正在尝试构建一个 Discord 机器人,它使用 GPT-4 API 作为 Discord 上的聊天机器人。我有最新版本的 OpenAI 库,但当我运行代码时,它告诉我“发生错误:模块‘openai’没有属性‘ChatCompletion’”

我尝试卸载并重新安装 OpenAI 库,尝试使用完成端点并收到错误“这是一个聊天模型,v1/completions 端点不支持。您是否打算使用 v1/chat/completions?”

这是给我带来问题的代码片段:

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()
Run Code Online (Sandbox Code Playgroud)

Ahm*_*avi 9

确保您没有 \xe2\x80\x99t 有一个名为 \xe2\x80\x9copenai.py\xe2\x80\x9d 的文件

\n


小智 1

确保您拥有最新的 OpenAI 库。我有同样的问题并通过将 openai 26.5 升级到 27.2 版本解决了它。