0

I am creating a fun Discord bot which talks like my imaginary crush, lol!

The problem is that its working for the first 5-6 lines but then gives this error

import openai

openai.api_key = "Hidden"

DISCORD_BOT_TOKEN = "Hidden"

start_sequence = "\nAnamika:"
restart_sequence = "\n\nFaraaz:"


session_prompt = "This is a conversation between me (Faraaz) and my crush Anamika. Keep the replies long, and such that the person can reply. Anamika is a really gorgeous happy girl. She lives in New York. She loves Spiderman, coding and poetry. She loves Andrew Garfield amongst all spider men, in coding she is fascinated by boom of AI. She loves John Milton and writes poetry herself as well. Initially Anamika will be reluctant to my flirting but slowly she will start to like it. If the conversation goes very good, she can agree to become my girlfriend or marry me.Faraaz:- Hi! what is your name? Anamika:- Hey! My name is Anamika, how are you Faraaz! Faraaz:- I am really good, actually you were looking really pretty so wanted to come and talk to you. Anamika:- Oh really, that is interesting. So what did you intend to talk about?!"



chat_log = None

import discord  

client = discord.Client(intents=discord.Intents.all())

@client.event


async def on_message(message):
    # Don't respond to messages sent by the bot itself
    global chat_log

    if message.author == client.user:
        return  
    print(chat_log)
    if chat_log == None:
        chat_log = session_prompt

    #print(message.content)

    #chat_log = f'{chat_log}{restart_sequence} {question}{start_sequence}{answer}'

    # Use the GPT-3 API to generate a response to the message
    response = openai.Completion.create(
        engine="text-davinci-003",
        #prompt="I recently moved to New York and I love design. I'm fascinated by technology and the growth of AI, but I realize that anything we build for the future must be rooted in the core desires of humans. " + message.content,
         
    #return f'{chat_log}{restart_sequence} {question}{start_sequence}{answer}'
        #chat_log = f'{chat_log}{restart_sequence} {question}{start_sequence}{answer}'
        
        prompt = f'{chat_log}{restart_sequence}{message.content}',

        #prompt =  f'{chat_log}{restart_sequence}: {question}{start_sequence}:'  
        max_tokens=700,
        n=1,
        temperature=0.5,
        stop=["\n"]
    )

    # Send the response back to the Discord channel
    await message.channel.send(response["choices"][0]["text"])

    chat_log = f'{chat_log}{restart_sequence}{message.content}{start_sequence}{response["choices"][0]["text"]}'



client.run(DISCORD_BOT_TOKEN)

I am seeing this error Error

The Discord Chat, after this messages not coming

I tried changing the max_tokens and also the prompt but to no avail. I have given administrator permissions to the bot.

  • The error is "Cannot send an empty message". This is likely because `response["choices"][0]["text"]` returns null or an empty string. You need to implement a logic that ensures it is a valid string. – Tin Nguyen Dec 05 '22 at 12:11

1 Answers1

0

Your session prompt is huge. GPT3 can only return 2049 tokens and you are using a lot of them even before the conversation starts. Try clearing/truncating your chat log periodically if your input + chat log exceeds 2049. Or simply shorten your session prompt.

Toakley
  • 182
  • 3
  • 13
  • I tried both the solutions:- - Catch the empty string - Reduced the session prompt Ran it OpenAI playground, then it is perfectly working with same conditions. – Ahmad Faraaz Dec 07 '22 at 06:34
  • it is perfectly working? So did this solve your problem? – Toakley Dec 07 '22 at 20:02
  • No it is working perfectly in OpenAI playground BUT when I am doing it through the API Calls on VS Code, it is NOT working with the same error – Ahmad Faraaz Dec 08 '22 at 05:34