diff --git a/senju/haiku.py b/senju/haiku.py index b86951c..969eec0 100644 --- a/senju/haiku.py +++ b/senju/haiku.py @@ -29,23 +29,32 @@ class Haiku: "eval_count": 20 } + tries = 0 while True: + tries += 1 try: r = requests.post(url=url, json=ai_gen_request) ai_response = str(r.json()["response"]) - logging.warning(ai_response) + logging.debug(f"ai response: {ai_response}") lines = ai_response.split("\n") while len(lines) != 3: lines.pop() - logging.warning(lines) + logging.info(f"lines for haiku: {lines}") - if len(lines) != 3: - continue + if len(lines) < 3: + if tries < 20: + logging.warning(f"too few lines, trying again") + logging.debug(lines) + continue + else: + logging.warning(f"too many tries, aborting") + raise Exception( + "Generating the haiku took too many tries") haiku = Haiku( [ @@ -55,8 +64,9 @@ class Haiku: ]) break - except json.JSONDecodeError: - continue + except json.JSONDecodeError as e: + logging.error(f"error while reading json from LLM: {e}") + raise e return haiku