OpenWebUI-Discordbot/scripts/discordbot.py
Josh Knapp 43f40981db
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 59s
Include History for Chatbot
2024-12-31 19:18:31 -08:00

111 lines
3.8 KiB
Python

import discord
from discord.ext import commands
from openai import OpenAI
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure OpenAI client to point to OpenWebUI
client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY'),
base_url=os.getenv('OPENWEBUI_API_BASE') # e.g., "http://localhost:8080/v1"
)
# Initialize Discord bot
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
# Add a dictionary to store conversation histories
conversation_histories = {}
DEFAULT_HISTORY_LIMIT = 50
MAX_HISTORY_LIMIT = 200
async def get_ai_response(prompt, channel_id=None):
try:
messages = []
# Always include history if it exists for this channel
if channel_id in conversation_histories:
history = conversation_histories[channel_id][-400:] # Get last 400 messages (200 exchanges)
# Format the history as a context block
history_text = "\n".join([
f"{'User: ' if msg['role'] == 'user' else 'Assistant: '}{msg['content']}"
for msg in history
])
formatted_prompt = f"{prompt}\n##CONTEXT##\n{history_text}\n##ENDCONTEXT##"
else:
formatted_prompt = prompt
response = client.chat.completions.create(
model=os.getenv('MODEL_NAME') or "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": formatted_prompt}],
temperature=0.7,
max_tokens=500
)
# Store the conversation history if channel_id is provided
if channel_id:
if channel_id not in conversation_histories:
conversation_histories[channel_id] = []
conversation_histories[channel_id].extend([
{"role": "user", "content": prompt},
{"role": "assistant", "content": response.choices[0].message.content}
])
# Always keep last 400 messages (200 exchanges)
if len(conversation_histories[channel_id]) > 400:
conversation_histories[channel_id] = conversation_histories[channel_id][-400:]
return response.choices[0].message.content
except Exception as e:
print(f"Error getting AI response: {e}")
return "Sorry, I encountered an error while processing your request."
@bot.event
async def on_message(message):
if message.author == bot.user:
return
# 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 not prompt:
await message.channel.send("Hello! How can I help you?")
return
async with message.channel.typing():
response = await get_ai_response(
prompt,
channel_id=str(message.channel.id)
)
if len(response) > 2000:
chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
for chunk in chunks:
await message.channel.send(chunk)
else:
await message.channel.send(response)
await bot.process_commands(message)
def main():
# Get the Discord token from environment variables
discord_token = os.getenv('DISCORD_TOKEN')
if not discord_token:
raise ValueError("Discord token not found in environment variables")
# Run the bot
bot.run(discord_token)
if __name__ == "__main__":
main()