#Discord Bot in Python status 400 error

10 messages · Page 1 of 1 (latest)

trim birch
#

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}")
bitter crest
#

Why are you using requests instead of the OpenAI module? Was this code by any chance generated by ChatGPT?

The correct endpoint is https://api.openai.com/v1/completions

trim birch
#

Yeah, I had no idea where to start to add the API to my bot I already had, so I asked it what code I'd need. 😂

neat dust
trim birch
#

Yeah wanted to get it working and then turn it into a command.

#

No one's in the Dev server so it's just me anyway but I do agree with what you said

neat dust
#

Dev server?

trim birch
#

I made a development discord server to do it in so it's not pinging everyone in my main server

#

While I test stuff

limber latch
#

I think i have the same problem, i coded a bot in javascript, the code works 100% but i get an api error with the same status code 400...