From 7046eb25767108b458b8542b08d5251103c866f5 Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Mon, 30 Dec 2024 19:54:17 -0800 Subject: [PATCH 1/2] Responding to DMs --- scripts/discordbot.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/discordbot.py b/scripts/discordbot.py index ccbb80e..f161fca 100644 --- a/scripts/discordbot.py +++ b/scripts/discordbot.py @@ -38,18 +38,22 @@ async def get_ai_response(prompt): async def on_ready(): print(f'{bot.user} has connected to Discord!') + @bot.event async def on_message(message): # Ignore messages from the bot itself if message.author == bot.user: return - # Check if the bot is mentioned in the message - if bot.user in message.mentions: - # Remove the bot mention and get the actual prompt - prompt = message.content.replace(f'<@{bot.user.id}>', '').strip() + # Respond to DMs or when mentioned in a server + if isinstance(message.channel, discord.DMChannel) or bot.user in message.mentions: + # For mentions, remove the bot mention from the message + if bot.user in message.mentions: + prompt = message.content.replace(f'<@{bot.user.id}>', '').strip() + else: + prompt = message.content.strip() - # If there's no prompt after mentioning the bot + # If there's no prompt if not prompt: await message.channel.send("Hello! How can I help you?") return From e7f59ce55df9aa2283cbb949123e16363a8adbc5 Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Mon, 30 Dec 2024 21:00:17 -0800 Subject: [PATCH 2/2] Adding Chat history review --- scripts/discordbot.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/scripts/discordbot.py b/scripts/discordbot.py index f161fca..77787b9 100644 --- a/scripts/discordbot.py +++ b/scripts/discordbot.py @@ -18,13 +18,20 @@ intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents) -async def get_ai_response(prompt): +async def get_chat_history(channel, limit=50): + messages = [] + async for message in channel.history(limit=limit): + # Skip bot's own messages + if message.author == bot.user: + continue + messages.append({"role": "user", "content": message.content}) + return list(reversed(messages)) # Return in chronological order + +async def get_ai_response(messages): try: response = client.chat.completions.create( - model=os.getenv('MODEL_NAME') or "us.anthropic.claude-3-5-sonnet-20241022-v2:0", # OpenWebUI should handle this regardless - messages=[ - {"role": "user", "content": prompt} - ], + model=os.getenv('MODEL_NAME') or "us.anthropic.claude-3-5-sonnet-20241022-v2:0", + messages=messages, temperature=0.7, max_tokens=500 ) @@ -33,13 +40,7 @@ async def get_ai_response(prompt): print(f"Error getting AI response: {e}") return "Sorry, I encountered an error while processing your request." - -@bot.event -async def on_ready(): - print(f'{bot.user} has connected to Discord!') - - -@bot.event +@bot.event async def on_message(message): # Ignore messages from the bot itself if message.author == bot.user: @@ -60,13 +61,18 @@ async def on_message(message): # Show typing indicator while processing async with message.channel.typing(): - # Get response from OpenWebUI - response = await get_ai_response(prompt) + # Get chat history + chat_history = await get_chat_history(message.channel) + + # Add current message to history + chat_history.append({"role": "user", "content": prompt}) + + # Get response from OpenWebUI with chat history context + response = await get_ai_response(chat_history) # Send the response # Split long messages if they exceed Discord's character limit if len(response) > 2000: - # Split the response into chunks of 2000 characters chunks = [response[i:i+2000] for i in range(0, len(response), 2000)] for chunk in chunks: await message.channel.send(chunk)