Skip to content

Response generated in console, but can't retrieve it? #306

Description

@sneezeparty

I'm using the below code to try and set up a simple Discord bot. The bot runs, appears in the correct channel, and so on. When I @tag the bot, it seems to generate a response. In the console window, a response is visible that seems to have been generated by my finetuned model. My dataset is formatted correctly, I think. And again, the confusing part of this is that a response is displayed in the console where I've launched the bot.

However, I get the error "Sorry, I was unable to generate a response" which, according to the code, means that gpt2.generate has returned "None."

Any help would be greatly appreciated. I'm not really a developer, I'm just doing this for fun.

import discord
import gpt_2_simple as gpt2

# Create a TensorFlow session and load the fine-tuned GPT-2 model
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess=sess, model_name="D:\git\GPT2\gpt-2-simple\checkpoint\discord-short")

# Replace YOUR_BOT_TOKEN_HERE with your Discord bot token
client = discord.Client(intents=discord.Intents.all(), token='MYKEY')

# This event handler will be called whenever a message is sent in a server that the bot is a member of
@client.event
async def on_message(message):
    # Only generate a response if the bot is @mentioned
    if message.mention_everyone or client.user in message.mentions:
        # Retrieve the last 10 messages from the chat
        history = []
        async for m in message.channel.history(limit=10):
            history.append(m)
        # Reverse the list of messages so that the first message is the oldest
        history = list(reversed(history))
        # Use the messages as context when generating the response
        context = "\n".join([m.content for m in history])
        response = gpt2.generate(sess,
                                 run_name='discord-short',
                                 temperature=0.8,
                                 nsamples=1,
                                 batch_size=1,
                                 top_k=20,
                                 top_p=0.5,
                                 prefix="<|startoftext|>",
                                 truncate="<|endoftext|>",
                                 length=50,
                                 include_prefix=False,
                                 return_as_list=False
                                 )
    # Check if the response is None
        if response is not None:
            # send the response
            await message.channel.send(response)
        else:
            # handle the "none" error
            await message.channel.send("Sorry, I was unable to generate a response.")
        
# Start the bot
client.run('MYKEY')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions