Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sneezeparty opened this issue Jan 9, 2023 · 2 comments
Open

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

sneezeparty opened this issue Jan 9, 2023 · 2 comments

Comments

@sneezeparty
Copy link

sneezeparty commented Jan 9, 2023

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')
@Chownie
Copy link

Chownie commented Jan 11, 2023

return_as_list=False

I think you want this to be true, else generate doesn't return anything.

@sneezeparty
Copy link
Author

return_as_list=False

I think you want this to be true, else generate doesn't return anything.

This fixed the issue. I can't believe myself. Thanks so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants