Adding the ability to look at history but not including it every time

This commit is contained in:
Josh Knapp 2024-12-30 21:37:29 -08:00
parent e38a7532c3
commit b0258d2a1f

View File

@ -18,51 +18,90 @@ 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): # 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, include_history=False, history_limit=DEFAULT_HISTORY_LIMIT):
try: try:
messages = []
# If history is requested and exists for this channel, include it
if include_history and channel_id in conversation_histories:
messages = conversation_histories[channel_id]
# Add the current prompt
messages.append({"role": "user", "content": prompt})
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
) )
# 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}
])
# Limit history based on specified or default limit
if len(conversation_histories[channel_id]) > history_limit * 2: # multiply by 2 because each exchange has 2 messages
conversation_histories[channel_id] = conversation_histories[channel_id][-(history_limit * 2):]
return response.choices[0].message.content return response.choices[0].message.content
except Exception as e: except Exception as e:
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
async def on_ready():
print(f'{bot.user} has connected to Discord!')
@bot.event @bot.event
async def on_message(message): async def on_message(message):
# Ignore messages from the bot itself
if message.author == bot.user: if message.author == bot.user:
return return
# Check if the bot is mentioned in the message
if bot.user in message.mentions: if bot.user in message.mentions:
# Remove the bot mention and get the actual prompt
prompt = message.content.replace(f'<@{bot.user.id}>', '').strip() prompt = message.content.replace(f'<@{bot.user.id}>', '').strip()
# If there's no prompt after mentioning the bot
if not prompt: if not prompt:
await message.channel.send("Hello! How can I help you?") await message.channel.send("Hello! How can I help you?")
return return
# Show typing indicator while processing # Check if the message includes a request for history
include_history = False
history_limit = DEFAULT_HISTORY_LIMIT
# Check for "with history", "with X history", or "X lines of chat" patterns
import re
if "with history" in prompt.lower() or re.search(r"\d+\s*lines of chat", prompt.lower()):
include_history = True
# Check for specific history limit
if match := re.search(r"with (\d+) history", prompt.lower()):
requested_limit = int(match.group(1))
history_limit = min(requested_limit, MAX_HISTORY_LIMIT)
prompt = re.sub(r"with \d+ history", "", prompt, flags=re.IGNORECASE)
elif match := re.search(r"(\d+)\s*lines of chat", prompt.lower()):
requested_limit = int(match.group(1))
history_limit = min(requested_limit, MAX_HISTORY_LIMIT)
prompt = re.sub(r"\d+\s*lines of chat", "", prompt, flags=re.IGNORECASE)
else:
prompt = prompt.lower().replace("with history", "")
prompt = prompt.strip()
async with message.channel.typing(): async with message.channel.typing():
# Get response from OpenWebUI response = await get_ai_response(
response = await get_ai_response(prompt) prompt,
channel_id=str(message.channel.id),
include_history=include_history,
history_limit=history_limit
)
# Send the response
# 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)
@ -71,6 +110,15 @@ async def on_message(message):
await bot.process_commands(message) await bot.process_commands(message)
@bot.command(name='clearhistory')
async def clear_history(ctx):
channel_id = str(ctx.channel.id)
if channel_id in conversation_histories:
conversation_histories[channel_id] = []
await ctx.send("Conversation history has been cleared.")
else:
await ctx.send("No conversation history exists for this channel.")
def main(): def main():
# Get the Discord token from environment variables # Get the Discord token from environment variables
discord_token = os.getenv('DISCORD_TOKEN') discord_token = os.getenv('DISCORD_TOKEN')