小编Ant*_*ony的帖子

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

我正在尝试构建一个 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)

python openai-api gpt-4

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

gpt-4 ×1

openai-api ×1

python ×1