reaction to regenerate
This commit is contained in:
69
bot.py
69
bot.py
@@ -71,7 +71,7 @@ class Conversation:
|
|||||||
{"role": "assistant", "content": assistant},
|
{"role": "assistant", "content": assistant},
|
||||||
])
|
])
|
||||||
|
|
||||||
async def send(self, text, media=tuple()):
|
async def generate(self, text, media=tuple()):
|
||||||
# prepare text part
|
# prepare text part
|
||||||
if text:
|
if text:
|
||||||
openai_content = [{"type": "text", "text": text}]
|
openai_content = [{"type": "text", "text": text}]
|
||||||
@@ -106,6 +106,32 @@ class Conversation:
|
|||||||
self.add_message_pair(openai_content, response)
|
self.add_message_pair(openai_content, response)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
async def regenerate(self):
|
||||||
|
llm_response = await client.chat.completions.create(
|
||||||
|
model=MODEL, messages=self.history[:-1]
|
||||||
|
)
|
||||||
|
response = llm_response.choices[0].message.content
|
||||||
|
self.history[-1] = {"role": "assistant", "content": response}
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
async def discord_send(channel, text, name, avatar=DEFAULT_AVATAR):
|
||||||
|
chunks = [text[i:i+2000] for i in range(0, len(text), 2000)]
|
||||||
|
messages = []
|
||||||
|
for chunk in chunks:
|
||||||
|
if channel.guild:
|
||||||
|
hook = await webhook(channel)
|
||||||
|
message = await hook.send(
|
||||||
|
content=chunk,
|
||||||
|
username=name,
|
||||||
|
avatar_url=avatar,
|
||||||
|
wait=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
message = await channel.send(content=chunk)
|
||||||
|
messages.append(message)
|
||||||
|
return messages
|
||||||
|
|
||||||
|
|
||||||
# --- Data Storage ---
|
# --- Data Storage ---
|
||||||
# Keyed by channel ID
|
# Keyed by channel ID
|
||||||
@@ -149,22 +175,33 @@ async def on_message(message):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
async with channel.typing():
|
async with channel.typing():
|
||||||
response = await conversation.send(user_message, media)
|
response = await conversation.generate(user_message, media)
|
||||||
# Split into chunks for discord to prevent message too long
|
conversation.last_messages = await discord_send(
|
||||||
chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
|
channel, response, conversation.bot_name,
|
||||||
conversation.last_messages = []
|
)
|
||||||
for chunk in chunks:
|
except Exception as e:
|
||||||
if channel.guild:
|
print(f"An error occurred: {e}")
|
||||||
hook = await webhook(channel)
|
await message.reply("Sorry, I had a little hiccup. Baka!")
|
||||||
sent_message = await hook.send(
|
|
||||||
content=chunk,
|
@bot.event
|
||||||
username=conversation.bot_name,
|
async def on_reaction_add(reaction, user):
|
||||||
avatar_url=DEFAULT_AVATAR,
|
if reaction.emoji != "🔁":
|
||||||
wait=True,
|
return
|
||||||
|
message = reaction.message
|
||||||
|
channel = message.channel
|
||||||
|
conversation = conversation_history[channel.id]
|
||||||
|
if message not in conversation.last_messages:
|
||||||
|
return
|
||||||
|
print(f"_ {user}: {reaction}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with channel.typing():
|
||||||
|
for message in conversation.last_messages:
|
||||||
|
await message.delete()
|
||||||
|
response = await conversation.regenerate()
|
||||||
|
conversation.last_messages = await discord_send(
|
||||||
|
channel, response, conversation.bot_name,
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
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!")
|
||||||
|
Reference in New Issue
Block a user