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