So, im making a discord bot in python, i wanted to add the chatGPT api to it so my users can talk to it in the discord. When i send a message in the discord with the appropriate command, it receives back an error code of 400 from the bot in the chat instead of a proper response. Here is the on_message function,
# Event handler for when a message is sent in a Discord channel
@client.event
async def on_message(message):
# Check if the message starts with the !gen command
if message.content.startswith("!gen"):
# Extract the prompt from the command
prompt = message.content[5:]
# Define the model name
model = "text-davinci-002"
# Set up the headers for the API request
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# Define the payload for the API request
data = {
"prompt": prompt,
"model": model,
}
# Make the API request
response = requests.post("https://api.openai.com/v1/engines/text-davinci/jobs", headers=headers, data=json.dumps(data))
# Check the response status code
if response.status_code == 200:
# If the request was successful, parse the response JSON
response_json = response.json()
# Extract the generated text
generated_text = response_json["choices"][0]["text"]
# Send the generated text as a message in the Discord channel
await message.channel.send(generated_text)
else:
# If the request was unsuccessful, send an error message in the Discord channel
await message.channel.send(f"Request failed with status code {response.status_code}")