I am trying to connect my telegram bot with OpenAI and get an error message: “Invalid API key provided: sk -***. You can find your API key at https://platform.openai.com/account/api-keys “. Checked 5 times, I’m sure, that API_key is OK. Are there any other errors leading to this?
Checked api_key, there are no extra spaces for sure. I also changed the OpenAI version from 1.35 to 0.28. Noticed that if the key is used immediately after re-generation, it works. Used two ways to request OpenAI, through requests and OpenAI itself:
First one, openai version is 0.28
async def ask_gpt(question):
openai.api_key = gpt_config['api_key']
message = [
{'role': 'system', 'content': 'You are an assistant'},
{'role': 'user', 'content': question}
]
response = openai.ChatCompletion.create(
model=gpt_config['model']
, messages=message
, temperature=gpt_config['temperature']
)
return response.choices[0].message['content']
Second one,
async def ask_gpt(question):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
data = {
'model': gpt_config['model'],
'prompt': question,
'max_tokens': gpt_config['max_tokens'],
'temperature': gpt_config['temperature']
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
response_data = response.json()
generated_text = response_data['choices'][0]['text']
print('Answer is:', generated_text)
else:
print(f'Something went wrong, {response.status_code}')
print(response.text)
WhyMe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.