Compare commits

...

2 Commits

Author SHA1 Message Date
46b25d9a67 prompt 2025-02-02 16:10:50 +01:00
af202e8014 dum 2025-02-02 15:59:15 +01:00

View File

@ -2,7 +2,6 @@ from __future__ import annotations
import asyncio
import logging
from pathlib import Path
import re
from typing import TYPE_CHECKING
@ -12,6 +11,7 @@ from discord.ext import commands
from huggingface_hub import AsyncInferenceClient
from artemis.utils import config
from artemis.utils.constants import TEMP_DIR
if TYPE_CHECKING:
@ -107,10 +107,12 @@ class Chat(commands.Cog):
self.lock = asyncio.Lock()
def read_prompt(self):
return Path("temp/prompt").read_text() if Path("temp/prompt").exists() else ""
path = TEMP_DIR / "prompt"
return path.read_text() if path.exists() else ""
def write_prompt(self, prompt: str):
Path("temp/prompt").write_text(prompt)
path = TEMP_DIR / "prompt"
path.write_text(prompt)
def replace_emojis(self, text: str) -> str:
return EMOJI_RE.sub(lambda match: emoji_map[match.group(0)], text)
@ -124,18 +126,17 @@ class Chat(commands.Cog):
return content
def add_memory(self, role: str, message: str):
prompt = (
self.prompt
+ "The following is a user chat message directed at you, the format will be the same for subsequent messages, respond with only the message content, without specyfing actions."
+ "\n\n"
)
prompt = self.prompt + "\n\n"
if len(self.memory) == 0:
message = prompt + message
if len(self.memory) >= 15:
if len(self.memory) >= 20:
del self.memory[0]
del self.memory[0]
self.memory[0] = {"role": "user", "content": prompt + self.memory[0]["content"]}
self.memory.append({"role": role, "content": message})
self.memory[0] = {
"role": "user",
"content": (prompt + self.memory[0]["content"]).strip(),
}
self.memory.append({"role": role, "content": message.strip()})
def add_user_memory(self, message: str):
self.add_memory("user", message)
@ -187,8 +188,6 @@ class Chat(commands.Cog):
if not content:
return
content = f"[USERNAME]: {message.author.display_name}\n[MESSAGE]: {content}"
try:
async with message.channel.typing():
async with self.lock:
@ -213,8 +212,12 @@ class Chat(commands.Cog):
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
await ctx.send(repr(self.prompt) if len(self.prompt) > 0 else "No prompt set.")
elif prompt == "reset":
self.prompt = ""
self.write_prompt(self.prompt)
await ctx.send("Prompt reset.")
else:
self.prompt = prompt
self.write_prompt(prompt)
self.memory = []