Advertisement

OpenAI API error: “This is a chat model and not supported in the v1/completions endpoint“

阅读量:

题意 :OpenAI API 错误:“这是一个聊天模型,不支持在 v1/completions 端点中使用”

问题背景:

复制代码
 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)

I try to use diferent way to access the API key, including adding to enviroment variables.

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

What else can I try or where I'm going wrong, pretty new to programming. Error message:

我还可以尝试什么,或者我哪里做错了?我对编程还比较陌生。错误消息是:

openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions.

EDIT

I solved "No API key provided" error. Now I get the following error message:

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

openai.error.InvalidRequestError: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?

问题解决:

Regarding This is a chat model and not supported in the v1/completions endpoint error

关于“这是一个聊天模型,不支持在 v1/completions 端点中使用”的错误

The code you posted above would work immediately if you changed just one thing: gpt-3.5-turbo to text-davinci-003. This gives you an answer as to why you're getting this error. It's because you used the code that works with the GPT-3 API endpoint, but wanted to use the GPT-3.5 model (i.e., gpt-3.5-turbo). See model endpoint compatibility.

如果你只修改一件事:将 gpt-3.5-turbo 改为 text-davinci-003,那么你上面发布的代码将立即工作。这解释了为什么你会遇到这个错误。这是因为你使用了与 GPT-3 API 端点兼容的代码,但你想要使用的是 GPT-3.5 模型(即 gpt-3.5-turbo)。请参考模型端点兼容性文档。

API endpoint Model group Model name

| /v1/chat/completions| • GPT-4
• GPT-3.5| • gpt-4 and dated model releases
gpt-4-32k and dated model releases
gpt-4-1106-preview
gpt-4-vision-preview
gpt-3.5-turbo and dated model releases
gpt-3.5-turbo-16k and dated model releases
• fine-tuned versions of gpt-3.5-turbo |
| /v1/completions (Legacy)| • GPT-3.5
• GPT base| • gpt-3.5-turbo-instruct
babbage-002
davinci-002 |
| /v1/assistants| | All models except gpt-3.5-turbo-0301 supported.
Retrieval tool requires gpt-4-1106-preview or gpt-3.5-turbo-1106. |

/v1/audio/transcriptions Whisper whisper-1

| /v1/audio/speech| TTS| • tts-1
tts-1-hd |
| /v1/fine_tuning/jobs| • GPT-3.5
• GPT base| • gpt-3.5-turbo
babbage-002
davinci-002 |
|/v1/embeddings|Embeddings|• text-embedding-ada-002|

| /v1/moderations| Moderations| • text-moderation-stable
text-moderation-latest |

If you want to use the gpt-3.5-turbo model, then you need to write the code that works with the GPT-3.5 API endpoint (i.e., the Chat Completions API endpoint).

如果你想要使用 gpt-3.5-turbo 模型,那么你需要编写与 GPT-3.5 API 端点(即“聊天补全 API”端点)兼容的代码。

As you can see in the table above, there are API endpoints listed. If you're using the OpenAI SDK (like you are), then you need to use the appropriate method. See the table below.

如上面的表格所示,列出了 API 端点。如果你正在使用 OpenAI SDK(就像你一样),那么你需要使用适当的方法。请参阅下面的表格。

Note: Pay attention, because you have to use the method that is compatible with your OpenAI SDK version.

注意:请留意,因为你需要使用与你当前 OpenAI SDK 版本兼容的方法。

| API endpoint| Method for the
Python SDK v0.28.1| Method for the
Python SDK >=v1.0.0| Method for the
Node.js SDK v3.3.0| Method for the
Node.js SDK >=v4.0.0 |

--- --- --- --- ---
/v1/completions (Legacy) openai.Completion.create openai.completions.create openai.createCompletion openai.completions.create
/v1/assistants / openai.beta.assistants.create / openai.beta.assistants.create
/v1/audio/transcriptions openai.Audio.transcribe openai.audio.transcriptions.create openai.createTranscription openai.audio.transcriptions.create
/v1/audio/translations openai.Audio.translate openai.audio.translations.create openai.createTranslation openai.audio.translations.create
/v1/audio/speech / openai.audio.speech.create / openai.audio.speech.create
/v1/fine_tuning/jobs / openai.fine_tuning.jobs.create / openai.fineTuning.jobs.create
/v1/embeddings openai.Embedding.create openai.embeddings.create openai.createEmbedding openai.embeddings.create
/v1/moderations openai.Moderation.create openai.moderations.create openai.createModeration openai.moderations.create

You need to adjust the whole code. See comments in the working example below.

你需要调整整个代码。请参考下面工作示例中的注释。

Python SDK v1.0.0 working example for the gpt-3.5-turbo model

针对 gpt-3.5-turbo 模型的 Python SDK v1.0.0 工作示例

If you run test.py, the OpenAI API will return the following completion:

如果你运行 test.py,OpenAI API 将返回以下补全内容:

Hello! How can I assist you today?

test.py

复制代码
 import os

    
 from openai import OpenAI
    
  
    
 client = OpenAI(
    
     api_key = os.getenv("OPENAI_API_KEY"),
    
 )
    
  
    
 completion = client.chat.completions.create( # Change the method
    
   model = "gpt-3.5-turbo",
    
   messages = [ # Change the prompt parameter to messages parameter
    
     {"role": "system", "content": "You are a helpful assistant."},
    
     {"role": "user", "content": "Hello!"},
    
   ]
    
 )
    
  
    
 print(completion.choices[0].message.content.strip()) # Change message content retrieval

Regarding No API key provided error

关于“未提供 API 密钥”错误

Change this... 将以下语句

复制代码
    os.environ.get('OPENAI_API_KEY')

...to this. 修改成

复制代码
    os.getenv('OPENAI_API_KEY')

全部评论 (0)

还没有任何评论哟~