fix: request_haiku method did not work with a simple 3 line haiku

Refs: OPS-70
This commit is contained in:
Christoph J. Scherr 2025-03-21 16:44:49 +01:00
parent 897bf80e38
commit 3889809d07
No known key found for this signature in database
GPG key ID: 9EB784BB202BB7BB

View file

@ -29,23 +29,32 @@ class Haiku:
"eval_count": 20 "eval_count": 20
} }
tries = 0
while True: while True:
tries += 1
try: try:
r = requests.post(url=url, r = requests.post(url=url,
json=ai_gen_request) json=ai_gen_request)
ai_response = str(r.json()["response"]) ai_response = str(r.json()["response"])
logging.warning(ai_response) logging.debug(f"ai response: {ai_response}")
lines = ai_response.split("\n") lines = ai_response.split("\n")
while len(lines) != 3: while len(lines) != 3:
lines.pop() lines.pop()
logging.warning(lines) logging.info(f"lines for haiku: {lines}")
if len(lines) != 3: if len(lines) < 3:
continue 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( haiku = Haiku(
[ [
@ -55,8 +64,9 @@ class Haiku:
]) ])
break break
except json.JSONDecodeError: except json.JSONDecodeError as e:
continue logging.error(f"error while reading json from LLM: {e}")
raise e
return haiku return haiku