refactor and add last_messages
This commit is contained in:
59
bot.py
59
bot.py
@@ -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,42 +132,42 @@ 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]
|
user_message = message.content
|
||||||
user_message = message.content
|
if user_message.startswith(bot_tag):
|
||||||
if user_message.startswith(bot_tag):
|
user_message = user_message[len(bot_tag):]
|
||||||
user_message = user_message[len(bot_tag):]
|
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 = []
|
||||||
|
if message.attachments:
|
||||||
|
for attachment in message.attachments:
|
||||||
|
media.append((attachment.content_type, attachment.url))
|
||||||
|
|
||||||
media = []
|
try:
|
||||||
if message.attachments:
|
async with channel.typing():
|
||||||
for attachment in message.attachments:
|
chunks = await conversation.send(user_message, media)
|
||||||
media.append((attachment.content_type, attachment.url))
|
conversation.last_messages = []
|
||||||
|
|
||||||
try:
|
|
||||||
async with channel.typing():
|
|
||||||
response = await conversation.send(user_message, media)
|
|
||||||
# Split into chunks for discord to prevent message too long
|
|
||||||
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)
|
||||||
except Exception as e:
|
conversation.last_messages.append(sent_message)
|
||||||
print(f"An error occurred: {e}")
|
except Exception as e:
|
||||||
await message.reply("Sorry, I had a little hiccup. Baka!")
|
print(f"An error occurred: {e}")
|
||||||
|
await message.reply("Sorry, I had a little hiccup. Baka!")
|
||||||
|
|
||||||
|
|
||||||
# --- Slash Commands ---
|
# --- Slash Commands ---
|
||||||
|
Reference in New Issue
Block a user