Openai API Error (Gpt3.5 turbo)

Hi, I need help with the openai api I want to send a simple text prompt with the api (gtp3.5 turbo) and get the response from chatgpt. The apikey works and the two libraries are also installed, but I still get an error message (I use Pycharm).

import requests
import json


API_KEY = '#############'
URL = 'https://api.openai.com/v1/engines/davinci-codex/completions'


def send_request(prompt):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {API_KEY}'
    }
    data = {
        'prompt': prompt,
        'max_tokens': 50,
        'model': 'gpt-3.5-turbo'
    }
    response = requests.post(URL, headers=headers, json=data)
    if response.status_code == 200:
        response_data = response.json()
        choices = response_data.get('choices', [])
        if choices:
            return choices[0]['text'].strip()
    else:
        print(f"Error: {response.status_code}")
    return ''

response = send_request(prompt="Hi")
print(response)

The Error:

“C:\Users\user\PycharmProjects\ki assistend test\venv\Scripts\python.exe” “C:\Users\user\PycharmProjects\ki assistend test\main.py”
Error: 401

Process finished with exit code 0

401 means your authentication credentials are invalid, are you sure the bearer token is correct?

I generated a new Key and new this message appear:

“C:\Users\user\PycharmProjects\ki assistend test\venv\Scripts\python.exe” “C:\Users\user\PycharmProjects\ki assistend test\main.py”
Error: 400

Process finished with exit code 0

I don’t know the API so I cannot help you much further, but an HTTP 400 Error means your request was malformed/bad in some way

There may be useful information in the response body on the error path.
In the else you can print the response body as well as the error code.