Support different system messages for different channels and own history for those channels.
This commit is contained in:
parent
227b1af986
commit
d23e780248
@ -32,15 +32,16 @@ class AIResponse(AIMessageBase):
|
|||||||
|
|
||||||
|
|
||||||
class AIResponder(object):
|
class AIResponder(object):
|
||||||
def __init__(self, config: Dict[str, Any]) -> None:
|
def __init__(self, config: Dict[str, Any], channel: Optional[str] = None) -> None:
|
||||||
self.config = config
|
self.config = config
|
||||||
self.history: List[Dict[str, Any]] = []
|
self.history: List[Dict[str, Any]] = []
|
||||||
|
self.channel = channel if channel is not None else 'system'
|
||||||
openai.api_key = self.config['openai-token']
|
openai.api_key = self.config['openai-token']
|
||||||
|
|
||||||
def _message(self, message: AIMessage, limit: Optional[int] = None) -> List[Dict[str, Any]]:
|
def _message(self, message: AIMessage, limit: Optional[int] = None) -> List[Dict[str, Any]]:
|
||||||
messages = []
|
messages = []
|
||||||
system = self.config["system"].replace('{date}', time.strftime('%Y-%m-%d'))\
|
system = self.config[self.channel].replace('{date}', time.strftime('%Y-%m-%d'))\
|
||||||
.replace('{time}', time.strftime('%H:%M:%S'))
|
.replace('{time}', time.strftime('%H:%M:%S'))
|
||||||
messages.append({"role": "system", "content": system})
|
messages.append({"role": "system", "content": system})
|
||||||
if limit is None:
|
if limit is None:
|
||||||
history = self.history[:]
|
history = self.history[:]
|
||||||
|
|||||||
@ -32,6 +32,9 @@ class FjerkroaBot(commands.Bot):
|
|||||||
self.observer.start()
|
self.observer.start()
|
||||||
|
|
||||||
self.airesponder = AIResponder(self.config)
|
self.airesponder = AIResponder(self.config)
|
||||||
|
self.aichannels = {}
|
||||||
|
for chan_name in self.config['additional-responders']:
|
||||||
|
self.aichannels[chan_name] = AIResponder(self.config, chan_name)
|
||||||
|
|
||||||
super().__init__(command_prefix="!", case_insensitive=True, intents=intents)
|
super().__init__(command_prefix="!", case_insensitive=True, intents=intents)
|
||||||
|
|
||||||
@ -73,8 +76,12 @@ class FjerkroaBot(commands.Bot):
|
|||||||
except Exception:
|
except Exception:
|
||||||
channel_name = str(channel.id)
|
channel_name = str(channel.id)
|
||||||
logging.info(f"handle message {str(message)} for channel {channel_name}")
|
logging.info(f"handle message {str(message)} for channel {channel_name}")
|
||||||
|
if channel_name in self.aichannels:
|
||||||
|
airesponder = self.aichannels[channel_name]
|
||||||
|
else:
|
||||||
|
airesponder = self.airesponder
|
||||||
async with channel.typing():
|
async with channel.typing():
|
||||||
response = await self.airesponder.send(message)
|
response = await airesponder.send(message)
|
||||||
if response.hack:
|
if response.hack:
|
||||||
logging.warning(f"User {message.user} tried to hack the system.")
|
logging.warning(f"User {message.user} tried to hack the system.")
|
||||||
if response.staff is None:
|
if response.staff is None:
|
||||||
@ -85,7 +92,7 @@ class FjerkroaBot(commands.Bot):
|
|||||||
if not response.answer_needed:
|
if not response.answer_needed:
|
||||||
return
|
return
|
||||||
if response.picture is not None:
|
if response.picture is not None:
|
||||||
images = [discord.File(fp=await self.airesponder.draw(response.picture), filename="image.png")]
|
images = [discord.File(fp=await airesponder.draw(response.picture), filename="image.png")]
|
||||||
await channel.send(response.answer, files=images)
|
await channel.send(response.answer, files=images)
|
||||||
else:
|
else:
|
||||||
await channel.send(response.answer)
|
await channel.send(response.answer)
|
||||||
|
|||||||
@ -28,6 +28,7 @@ class TestBotBase(unittest.IsolatedAsyncioTestCase):
|
|||||||
"frequency-penalty": 1.0,
|
"frequency-penalty": 1.0,
|
||||||
"history-limit": 10,
|
"history-limit": 10,
|
||||||
"system": "You are an smart AI",
|
"system": "You are an smart AI",
|
||||||
|
"additional-responders": [],
|
||||||
}
|
}
|
||||||
self.history_data = []
|
self.history_data = []
|
||||||
with patch.object(FjerkroaBot, 'load_config', new=lambda s, c: self.config_data), \
|
with patch.object(FjerkroaBot, 'load_config', new=lambda s, c: self.config_data), \
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user