diff --git a/fjerkroa_bot/ai_responder.py b/fjerkroa_bot/ai_responder.py index 731cceb..2a24b10 100644 --- a/fjerkroa_bot/ai_responder.py +++ b/fjerkroa_bot/ai_responder.py @@ -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