openai python not giving response

Viewed 23

I am trying to create a discord.py bot. However when I am sending a message with Human: it is not giving any response. No error message is being shown either. Also, when I am trying to print response or answer it is not printing them either.

Here is my code:

import discord
import openai
intents = discord.Intents(messages=True)
client = discord.Client(intents=intents)
openai.api_key = openAPIKey
start_sequence = "AI:"
restart_sequence = "Human:"


@client.event
async def on_ready():
    print("Logged in")


@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith(f"{restart_sequence}"):
        msg = message.content
        response = openai.Completion.create(
            engine="text-davinci-002",
            prompt= f"AI is a chatbot that reluctantly answers questions with sarcastic responses:{msg}, {start_sequence}",
            temperature=0.5,
            max_tokens=60,
            top_p=0.3,
            frequency_penalty=0.5,
            presence_penalty=0.5,
            stop=[" Human:", " AI:"],
        )
        print(response)
        #answer = response.choices[0].text
        await message.channel.send(f"AI:{response}")


client.run(
    discordBotKey)

Note: To hide by Discord and OpenAI keys I replaced them with discordBotKey and openAPIKey respectively. In the real code, they are filled with the correct keys

0 Answers
Related