Save history on short patch.

This commit is contained in:
OK 2023-03-27 14:13:10 +02:00
parent 2cd66b55ee
commit 2ee2b092d6

View File

@ -101,7 +101,11 @@ class AIResponder(object):
user_ma = re.match(user_re, message.user)
if chan_ma and user_ma:
self.history.append({"role": "user", "content": str(message)})
self.history = self.history[-limit:]
if len(self.history) > limit:
self.history = self.history[-limit:]
if self.history_file is not None:
with open(self.history_file, 'wb') as fd:
pickle.dump(self.history, fd)
return True
return False
@ -119,6 +123,7 @@ class AIResponder(object):
return answer, limit
except openai.error.InvalidRequestError as err:
if 'maximum context length is' in str(err) and limit > 4:
logging.warning(f"context length exceeded, reduce the limit {limit}: {str(err)}")
limit -= 1
return None, limit
raise err