changeprompt

This commit is contained in:
2025-08-22 00:21:37 -07:00
parent 8c750b2fbb
commit ad9c069993
3 changed files with 25 additions and 7 deletions

16
bot.py
View File

@@ -86,7 +86,7 @@ async def on_message(message):
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'{channel.id}> {message.author.name}: {user_message}')
media = [] media = []
if message.attachments: if message.attachments:
@@ -165,7 +165,21 @@ async def newchat(interaction: discord.Interaction, prompt: str = None):
f'Starting a new chat with {conversation.bot_name}: "{prompt}"' f'Starting a new chat with {conversation.bot_name}: "{prompt}"'
) )
@bot.tree.command(
name="changeprompt",
description="Change the current chat's system prompt."
)
async def newchat(interaction: discord.Interaction, prompt: str):
await interaction.response.defer()
channel_id = interaction.channel_id
conversation = await Conversation.get(channel_id, args.base_url, bot.db)
await conversation.update_prompt(prompt)
await interaction.followup.send(
f'Now chatting with {conversation.bot_name}: "{prompt}"'
)
# --- Running the Bot --- # --- Running the Bot ---
if __name__ == "__main__": if __name__ == "__main__":
bot.run(args.discord_token) bot.run(args.discord_token)

View File

@@ -6,11 +6,10 @@ class Database:
self.db_path = db_path self.db_path = db_path
self.conn = None self.conn = None
@staticmethod @classmethod
async def get(db_path='conversations.db'): async def get(cls, db_path='conversations.db'):
""" """Asynchronously creates and returns a connected Database instance."""
Asynchronously creates and returns a connected Database instance. print(f"Initializing DB connection to: {db_path}")
"""
db = Database(db_path) db = Database(db_path)
db.conn = await aiosqlite.connect(db.db_path) db.conn = await aiosqlite.connect(db.db_path)
await db._create_table() await db._create_table()

View File

@@ -77,6 +77,10 @@ class Conversation:
self.history = self.history[:-2] self.history = self.history[:-2]
await self.save() await self.save()
async def update_prompt(self, prompt):
self.history[0] = {"role": "system", "content": prompt}
self.bot_name = await self.get_name(self.client, prompt)
async def generate(self, text, media=tuple()): async def generate(self, text, media=tuple()):
# prepare text part # prepare text part
if text: if text:
@@ -119,3 +123,4 @@ class Conversation:
response = llm_response.choices[0].message.content response = llm_response.choices[0].message.content
self.history[-1] = {"role": "assistant", "content": response} self.history[-1] = {"role": "assistant", "content": response}
return response return response