Adding Chat history review

This commit is contained in:
Josh Knapp 2024-12-30 21:00:17 -08:00
parent 7046eb2576
commit e7f59ce55d

View File

@ -18,13 +18,20 @@ intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents) 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: try:
response = client.chat.completions.create( 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 model=os.getenv('MODEL_NAME') or "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[ messages=messages,
{"role": "user", "content": prompt}
],
temperature=0.7, temperature=0.7,
max_tokens=500 max_tokens=500
) )
@ -33,13 +40,7 @@ async def get_ai_response(prompt):
print(f"Error getting AI response: {e}") print(f"Error getting AI response: {e}")
return "Sorry, I encountered an error while processing your request." return "Sorry, I encountered an error while processing your request."
@bot.event
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
@bot.event
async def on_message(message): async def on_message(message):
# Ignore messages from the bot itself # Ignore messages from the bot itself
if message.author == bot.user: if message.author == bot.user:
@ -60,13 +61,18 @@ async def on_message(message):
# Show typing indicator while processing # Show typing indicator while processing
async with message.channel.typing(): async with message.channel.typing():
# Get response from OpenWebUI # Get chat history
response = await get_ai_response(prompt) 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 # Send the response
# Split long messages if they exceed Discord's character limit # Split long messages if they exceed Discord's character limit
if len(response) > 2000: 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)] chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
for chunk in chunks: for chunk in chunks:
await message.channel.send(chunk) await message.channel.send(chunk)