-2

I'm new to using APIs. I found myself interested inn the new OpenAI product, GPT-3 (I know, it's not that new. But I just found out about it). I'm trying to use the API key in Python, but it seems the key is invalid.

This is my code (I can't put my API key here for obvious reasons):

import requests 
prompt = 'Tell me the history of Europe in summary'
model = 'davinci'
url = 'https://api.openai.com/v1/engines/davinci/jobs'

headers = {
    'content-type': 'application/json',
    'Authorization': 'Bearer MY_API_KEY',
}

data = {
    'prompt': prompt,
    'max-tokens': 100,
    'temperature': 0.5,
}

response = requests.post(url,headers=headers, json=data)
response_json = response.json()
print(response_json)

I keep receiving this error: {'error': {'message': 'Unknown endpoint for this model.', 'type': 'invalid_request_error', 'param': None, 'code': None}}

I have tried using a new API key several times but it doesn't work. How can I find out why my key is invalid?

Rok Benko
  • 14,265
  • 2
  • 24
  • 49
  • 1
    Note that that API is not ChatGPT. It's an older model. – Cerbrus Feb 13 '23 at 11:59
  • Welcome to Stack Overflow. "I keep receiving this error:" - okay, did you read the error? Do you understand it? For example, do you know what an "endpoint" is, in this context? When the message tells you "Unknown endpoint for this model", what do you suppose this means? If you "keep receiving" the error, did you **try to change anything**? (If you keep using the same code, why should it stop giving you the same error?) Did you try to read the documentation, in order to understand what endpoints are available for the model? – Karl Knechtel Feb 13 '23 at 12:34

2 Answers2

1

All Engines endpoints are deprecated.

Deprecated

Change this...

https://api.openai.com/v1/engines/davinci/jobs

...to this.

https://api.openai.com/v1/completions

Working example

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

This is indeed a test

test.py

import json
import requests

data = {
    'prompt': 'Say this is a test',
    'model': 'text-davinci-003',
    'max_tokens': 7,
    'temperature': 0
}

response = requests.post('https://api.openai.com/v1/completions', json=data, headers={
    'Content-Type': 'application/json',
    'Authorization': f'Bearer <OPENAI_API_KEY>'
})

response_json = json.loads(response.text)

print(response_json['choices'][0]['text'])

EDIT

If you got the following error, see this answer.

{'error': {'message': 'You exceeded your current quota, please check your plan and billing details.', 'type': 'insufficient_quota', 'param': None, 'code': None}
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
  • I did this, and now there's a new error: {'error': {'message': 'You exceeded your current quota, please check your plan and billing details.', 'type': 'insufficient_quota', 'param': None, 'code': None} – arad ramezani Feb 13 '23 at 12:00
  • @aradramezani "I did this, and now there's a new error" - okay, so this is even more clear, and it would also make a separate question. Please read [ask] and take the [tour], and note well that Stack Overflow is **not a discussion forum**, and does not provide technical support for third-party services. – Karl Knechtel Feb 13 '23 at 12:36
1

OpenAI has a python implementation. I suggest you check it out.

import os
import openai

openai.api_key = "MY_API_KEY"

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="Tell me the history of Europe in summary",
  temperature=0,
  max_tokens=100,
  top_p=1,
  frequency_penalty=0.0,
  presence_penalty=0.0,
  stop=["\n"]
)

You will be better of using their python package.

Kjobber
  • 176
  • 1
  • 9