“OpenAI Python API 密钥错误:‘必须设置 api_key 客户端选项’

问题描述 投票:0回答:1

我正在尝试使用 Python 中的 OpenAI API 创建聊天完成,但我不断遇到与 API 密钥相关的错误。这是我的代码:


import os
from openai import OpenAI

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"),
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-3.5-turbo",
)

错误信息:

openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
python environment-variables operating-system openai-api
1个回答
0
投票

似乎您尚未在

OPEN_API_KEY
文件中设置
.env
变量。您可以尝试按照以下步骤设置环境变量:https://stackoverflow.com/a/64881524/12248084

© www.soinside.com 2019 - 2024. All rights reserved.