Improve history handling

- Try to keep at least 3 messages from each channel in the history
- Use post processed messages for the history, instead of the raw
  messages from the openai API
This commit is contained in:
OK
2023-04-12 12:19:07 +02:00
parent d136b0af21
commit 2db983c462
2 changed files with 68 additions and 9 deletions
+20 -9
View File
@@ -97,13 +97,12 @@ class AIResponder(object):
system = system.replace('{date}', time.strftime('%Y-%m-%d'))\
.replace('{time}', time.strftime('%H:%M:%S'))
messages.append({"role": "system", "content": system})
if limit is None:
history = self.history[:]
else:
history = self.history[-limit:]
history.append({"role": "user", "content": str(message)})
for msg in history:
if limit is not None:
while len(self.history) > limit:
self.shrink_history_by_one()
for msg in self.history:
messages.append(msg)
messages.append({"role": "user", "content": str(message)})
return messages
async def draw(self, description: str) -> BytesIO:
@@ -202,11 +201,22 @@ class AIResponder(object):
logging.warning(f"failed to execute a fix for the answer: {repr(err)}")
return answer
def shrink_history_by_one(self, index: int = 0) -> None:
if index >= len(self.history):
del self.history[0]
else:
current = self.history[index]
count = sum(1 for item in self.history[index:] if item.get('channel') == current.get('channel'))
if count > 3:
del self.history[index]
else:
self.shrink_history_by_one(index + 1)
def update_history(self, question: Dict[str, Any], answer: Dict[str, Any], limit: int) -> None:
self.history.append(question)
self.history.append(answer)
if len(self.history) > limit:
self.history = self.history[-limit:]
while len(self.history) > limit:
self.shrink_history_by_one()
if self.history_file is not None:
with open(self.history_file, 'wb') as fd:
pickle.dump(self.history, fd)
@@ -236,8 +246,9 @@ class AIResponder(object):
if 'hack' not in response or type(response.get('picture', None)) not in (type(None), str):
retries -= 1
continue
self.update_history(messages[-1], answer, limit)
answer_message = await self.post_process(message, response)
answer['content'] = str(answer_message)
self.update_history(messages[-1], answer, limit)
logging.info(f"got this answer:\n{str(answer_message)}")
return answer_message
raise RuntimeError("Failed to generate answer after multiple retries")