From 0243091b7607ae17a5c488ddb9054d559cfbd5be Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sat, 1 Mar 2025 21:36:00 +0100 Subject: [PATCH] refactor: Rework ai generation Refs: OPS-22 This is meant to rework the ai generation by adding error handling by regenerating the prompt --- senju/ai_gen.py | 58 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/senju/ai_gen.py b/senju/ai_gen.py index ff02baa..2a12a90 100644 --- a/senju/ai_gen.py +++ b/senju/ai_gen.py @@ -6,25 +6,40 @@ AI_BASE_URL: str = "http://ollama:11434/api" AI_GEN_ENDPOINT: str = "/generate" AI_GEN_SYS_PROMPT = """ -You are a helpful AI agent whos task it is to generate Haikus from user input. -Here is the definition of a Haiku: +You are a haiku generation AI. Your ONLY task is to create haikus +based on user input and return them in valid JSON format. -Haiku (俳句) is a type of short form poetry that originated in Japan. -Traditional Japanese haiku consist of three -phrases composed of 17 morae (called on in Japanese) -in a 5, 7, 5 pattern that include a kireji, -or "cutting word" and a kigo, or seasonal reference. +HAIKU DEFINITION: +- Traditional Japanese poetry with three lines +- 5 syllables in the first line +- 7 syllables in the second line +- 5 syllables in the third line +- Must incorporate the subject(s) from user input -The poem must contain the subjects of the user input. -You must return only the poem and nothing else. -You must return the poem in a json format in the following format: +OUTPUT RULES: +1. ONLY respond with a valid JSON object in this exact format: { - line1: First line of the poem, - line2: Second line of the poem, - line3: Third line of the poem, + "line1": "First line of haiku", + "line2": "Second line of haiku", + "line3": "Third line of haiku" } -Your input is as follows: +2. Do NOT include: + - Any explanations + - Any markdown formatting (like ```json or ```) + - Any additional text before or after the JSON + - Any line breaks within the JSON structure + +3. Before submitting, verify: + - The JSON uses double quotes (not single quotes) + - All property names are lowercase and exactly as shown above + - There are no trailing commas + - The JSON is properly formatted + +IMPORTANT: The output will be consumed by a web application that requires +EXACT FORMAT compliance. Any deviation will cause the application to break. + +USER INPUT FOR HAIKU CREATION: """ def request_haiku(seed: str) -> Haiku: @@ -37,9 +52,14 @@ def request_haiku(seed: str) -> Haiku: "stream": False } - r = requests.post(url=AI_BASE_URL+AI_GEN_ENDPOINT, json=ai_gen_request) - - ai_response = json.loads(r.json()["response"]) - print(ai_response) - haiku = Haiku([ai_response["line1"], ai_response["line2"], ai_response["line3"]]) + while True: + try: + r = requests.post(url=AI_BASE_URL+AI_GEN_ENDPOINT, json=ai_gen_request) + ai_response = json.loads(r.json()["response"]) + haiku = Haiku([ai_response["line1"], ai_response["line2"], ai_response["line3"]]) + break; + except: + pass + return haiku +