OpenAI ChatGPT (GPT-3.5) API 错误:“这是一个聊天模型,v1/completions 端点不支持”

问题描述 投票:0回答:2
import discord
import openai
import os


openai.api_key = os.environ.get("OPENAI_API_KEY")

#Specify the intent
intents = discord.Intents.default()
intents.members = True

#Create Client
client = discord.Client(intents=intents)

async def generate_response(message):
    prompt = f"{message.author.name}: {message.content}\nAI:"
    response = openai.Completion.create(
        engine="gpt-3.5-turbo",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )
    return response.choices[0].text.strip()

@client.event
async def on_ready():
    print(f"We have logged in as {client.user}")
    
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    response = await generate_response(message)
    await message.channel.send(response)

discord_token = 'DiscordToken'


client.start(discord_token)  

我尝试使用不同的方式来访问 API 密钥,包括添加到环境变量中。

我还能尝试什么或哪里出错了,这对编程来说还很陌生。 错误信息:

openai.error.AuthenticationError:未提供 API 密钥。您可以使用“openai.api_key =”在代码中设置您的 API 密钥,或者您可以设置环境变量 OPENAI_API_KEY=)。如果您的 API 密钥存储在文件中,您可以使用“openai.api_key_path =”将 openai 模块指向它。您可以在 OpenAI Web 界面中生成 API 密钥。有关详细信息,请参阅 https://onboard.openai.com,如果您有任何问题,请发送电子邮件至 [email protected]


编辑

我解决了“未提供 API 密钥”错误。现在我收到以下错误消息:

openai.error.InvalidRequestError: This is a chat model and not 在 v1/completions 端点中受支持。你是不是想用 v1/聊天/完成?

python discord openai-api chatgpt-api
2个回答
1
投票

关于
openai.error.AuthenticationError: No API key provided

改变这个...

openai.api_key = os.environ.get('OPENAI_API_KEY')

...到这个。

openai.api_key = os.getenv('OPENAI_API_KEY')

关于
openai.error.InvalidRequestError: This is a chat model and not supported in the v1/completions endpoint

如果您只更改一件事,代码将立即运行:

gpt-3.5-turbo
text-davinci-003
。这为您提供了为什么会出现此错误的答案。这是因为您使用了适用于 GPT-3 API 的代码,但想使用
gpt-3.5-turbo
模型。

如果你想使用

gpt-3.5.-turbo
模型,那么你需要编写适用于 GPT-3.5 API(即 ChatGPT API)的代码。

你需要调整整个代码。请参阅下面脚本中的注释。

工作示例

如果你运行

test.py
OpenAI API 将返回以下完成:

你好!今天我能为您提供什么帮助?

test.py

import openai
import os

openai.api_key = os.getenv('OPENAI_API_KEY')

completion = openai.ChatCompletion.create( # 1. Change the function Completion to ChatCompletion
  model = 'gpt-3.5-turbo',
  messages = [ # 2. Change the prompt parameter to the messages parameter
    {'role': 'user', 'content': 'Hello!'}
  ],
  temperature = 0  
)

print(completion['choices'][0]['message']['content']) # 3. Change how you access the message content

0
投票

端点 /chat/completions 不支持模型

model = 'gpt-3.5-turbo'
它需要
/v1/chat/completions
端点 相应地改变你的代码并且它有效 如果您还有任何问题,请告诉我们 您可以参考所有各种端点及其各自端点的文档 官方文档

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