refactor and add last_messages

This commit is contained in:
2025-08-21 00:20:12 -07:00
parent 7b052e0ff9
commit 4261991022

25
bot.py
View File

@@ -63,6 +63,7 @@ class Conversation:
def __init__(self, prompt, name): def __init__(self, prompt, name):
self.history = [{"role": "system", "content": prompt}] self.history = [{"role": "system", "content": prompt}]
self.bot_name = name self.bot_name = name
self.last_messages = []
def add_message_pair(self, user, assistant): def add_message_pair(self, user, assistant):
self.history.extend([ self.history.extend([
@@ -98,12 +99,14 @@ class Conversation:
# send request to openai api and return response # send request to openai api and return response
request = self.history + [{"role": "user", "content": openai_content}] request = self.history + [{"role": "user", "content": openai_content}]
response = await client.chat.completions.create( llm_response = await client.chat.completions.create(
model=MODEL, messages=request, model=MODEL, messages=request,
) )
bot_response = response.choices[0].message.content response = llm_response.choices[0].message.content
self.add_message_pair(openai_content, bot_response) self.add_message_pair(openai_content, response)
return bot_response
# Split into chunks for discord to prevent message too long
return [response[i:i+2000] for i in range(0, len(response), 2000)]
# --- Data Storage --- # --- Data Storage ---
@@ -129,8 +132,9 @@ async def on_ready():
async def on_message(message): async def on_message(message):
if message.author == bot.user: if message.author == bot.user:
return return
if not bot.user.mentioned_in(message):
return
if bot.user.mentioned_in(message):
bot_tag = f'<@{bot.user.id}>' bot_tag = f'<@{bot.user.id}>'
channel = message.channel channel = message.channel
conversation = conversation_history[channel.id] conversation = conversation_history[channel.id]
@@ -140,7 +144,6 @@ async def on_message(message):
user_message = user_message.replace(bot_tag, conversation.bot_name).strip() user_message = user_message.replace(bot_tag, conversation.bot_name).strip()
print(f'> {message.author.name}: {user_message}') print(f'> {message.author.name}: {user_message}')
media = [] media = []
if message.attachments: if message.attachments:
for attachment in message.attachments: for attachment in message.attachments:
@@ -148,20 +151,20 @@ async def on_message(message):
try: try:
async with channel.typing(): async with channel.typing():
response = await conversation.send(user_message, media) chunks = await conversation.send(user_message, media)
# Split into chunks for discord to prevent message too long conversation.last_messages = []
chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
for chunk in chunks: for chunk in chunks:
if channel.guild: if channel.guild:
hook = await webhook(channel) hook = await webhook(channel)
await hook.send( sent_message = await hook.send(
content=chunk, content=chunk,
username=conversation.bot_name, username=conversation.bot_name,
avatar_url=DEFAULT_AVATAR, avatar_url=DEFAULT_AVATAR,
wait=True, wait=True,
) )
else: else:
await channel.send(content=chunk) sent_message = await channel.send(content=chunk)
conversation.last_messages.append(sent_message)
except Exception as e: except Exception as e:
print(f"An error occurred: {e}") print(f"An error occurred: {e}")
await message.reply("Sorry, I had a little hiccup. Baka!") await message.reply("Sorry, I had a little hiccup. Baka!")