This commit is contained in:
artie 2025-02-01 19:28:36 +01:00
parent ec436bd63a
commit c8f5d986a1
2 changed files with 34 additions and 2 deletions

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio import asyncio
import logging import logging
from pathlib import Path
import re import re
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -101,9 +102,16 @@ class Chat(commands.Cog):
self.bot: Artemis = bot self.bot: Artemis = bot
self.memory: list[dict] = [] self.memory: list[dict] = []
self.model = "google/gemma-2-2b-it" self.model = "google/gemma-2-2b-it"
self.prompt = self.read_prompt()
self.client = AsyncInferenceClient(api_key=self.bot.secrets.huggingface) self.client = AsyncInferenceClient(api_key=self.bot.secrets.huggingface)
self.lock = asyncio.Lock() self.lock = asyncio.Lock()
def read_prompt(self):
return Path("data/prompt").read_text()
def write_prompt(self, prompt: str):
Path("data/prompt").write_text(prompt)
def replace_emojis(self, text: str) -> str: def replace_emojis(self, text: str) -> str:
return EMOJI_RE.sub(lambda match: emoji_map[match.group(0)], text) return EMOJI_RE.sub(lambda match: emoji_map[match.group(0)], text)
@ -116,7 +124,7 @@ class Chat(commands.Cog):
return content return content
def add_memory(self, role: str, message: str): def add_memory(self, role: str, message: str):
prompt = "You're Artemis, a bot hanging out in this Discord server, you're friendly and can answer anything, following is a user chat message directed at you.\n\n" prompt = self.prompt + "Following is a user chat message directed at you." + "\n\n"
if len(self.memory) == 0: if len(self.memory) == 0:
message = prompt + message message = prompt + message
if len(self.memory) >= 15: if len(self.memory) >= 15:
@ -150,7 +158,7 @@ class Chat(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_message(self, message: discord.Message): async def on_message(self, message: discord.Message):
if message.author.bot: if message.author.bot or message.content.startswith(config.prefix):
return return
reference = message.reference reference = message.reference
@ -184,6 +192,29 @@ class Chat(commands.Cog):
except aiohttp.client_exceptions.ClientResponseError as err: except aiohttp.client_exceptions.ClientResponseError as err:
await message.reply(f"{self.model} error: {err.status} {err.message}") await message.reply(f"{self.model} error: {err.status} {err.message}")
@commands.group(name="chat")
async def _chat(self, ctx: commands.Context):
"""LLM chat management."""
if ctx.invoked_subcommand is None:
await ctx.send("Invalid subcommand passed.")
@_chat.command()
async def reset(self, ctx: commands.Context):
"""Reset chat memory."""
self.memory = []
await ctx.send("Chat memory reset.")
@_chat.command(name="prompt")
async def _prompt(self, ctx: commands.Context, *, prompt: str = None):
"""Get or set system prompt and reset chat memory."""
if not prompt:
await ctx.send(self.prompt)
return
self.prompt = prompt
self.write_prompt(prompt)
self.memory = []
await ctx.send("Prompt updated.")
async def setup(bot: Artemis): async def setup(bot: Artemis):
await bot.add_cog(Chat(bot)) await bot.add_cog(Chat(bot))

1
data/prompt Normal file
View File

@ -0,0 +1 @@
You're Artemis, a bot hanging out in this Discord server, you're friendly and can answer anything.