From defe598651ae9d71b82184e3e1e8bc4f63de1825 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Wed, 12 Apr 2023 12:47:53 +0200 Subject: [PATCH] Improve handling of wrong keys in response --- fjerkroa_bot/ai_responder.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fjerkroa_bot/ai_responder.py b/fjerkroa_bot/ai_responder.py index 178ac90..167123a 100644 --- a/fjerkroa_bot/ai_responder.py +++ b/fjerkroa_bot/ai_responder.py @@ -117,12 +117,12 @@ class AIResponder(object): raise RuntimeError(f"Failed to generate image {repr(description)} after multiple retries") async def post_process(self, message: AIMessage, response: Dict[str, Any]) -> AIResponse: - for fld in ('answer', 'staff', 'picture'): - if str(response[fld]).strip().lower() in \ + for fld in ('answer', 'staff', 'picture', 'hack'): + if str(response.get(fld)).strip().lower() in \ ('none', '', 'null', '"none"', '"null"', "'none'", "'null'"): response[fld] = None for fld in ('answer_needed', 'hack'): - if str(response[fld]).strip().lower() == 'true': + if str(response.get(fld)).strip().lower() == 'true': response[fld] = True else: response[fld] = False @@ -243,7 +243,8 @@ class AIResponder(object): logging.error(f"failed to parse the fixed answer: {pp(err)}\n{repr(answer['content'])}") retries -= 1 continue - if 'hack' not in response or type(response.get('picture', None)) not in (type(None), str): + if type(response.get('picture')) not in (type(None), str): + logging.warning(f"picture key is wrong in response: {pp(response)}") retries -= 1 continue answer_message = await self.post_process(message, response)