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
}
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