diff --git a/fjerkroa_bot/ai_responder.py b/fjerkroa_bot/ai_responder.py index 74b5c81..731cceb 100644 --- a/fjerkroa_bot/ai_responder.py +++ b/fjerkroa_bot/ai_responder.py @@ -136,7 +136,14 @@ class AIResponder(object): result = await openai.ChatCompletion.acreate(model=self.config["fix-model"], messages=messages, temperature=0.2) - return result['choices'][0]['message']['content'] + logging.info(f"got this message as fix:\n{pformat(result['choices'][0]['message']['content'])}") + response = result['choices'][0]['message']['content'] + start, end = response.find("```"), response.rfind("```") + if start == -1 or end == -1 or (start + 3) >= end: + return answer + response = response[start + 3, end] + logging.info(f"fixed answer:\n{pformat(response)}") + return response except Exception as err: logging.warning(f"failed to execute a fix for the answer: {repr(err)}") return answer @@ -146,9 +153,9 @@ class AIResponder(object): self.history.append(answer) 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) + if self.history_file is not None: + with open(self.history_file, 'wb') as fd: + pickle.dump(self.history, fd) async def send(self, message: AIMessage) -> AIResponse: limit = self.config["history-limit"]