refactor: Rework ai generation

Refs: OPS-22

This is meant to rework the ai generation by adding error handling by
regenerating the prompt
This commit is contained in:
Alivecow 2025-03-01 21:36:00 +01:00
parent 8c0a7771e0
commit 0243091b76

View file

@ -6,25 +6,40 @@ AI_BASE_URL: str = "http://ollama:11434/api"
AI_GEN_ENDPOINT: str = "/generate" AI_GEN_ENDPOINT: str = "/generate"
AI_GEN_SYS_PROMPT = """ AI_GEN_SYS_PROMPT = """
You are a helpful AI agent whos task it is to generate Haikus from user input. You are a haiku generation AI. Your ONLY task is to create haikus
Here is the definition of a Haiku: based on user input and return them in valid JSON format.
Haiku (俳句) is a type of short form poetry that originated in Japan. HAIKU DEFINITION:
Traditional Japanese haiku consist of three - Traditional Japanese poetry with three lines
phrases composed of 17 morae (called on in Japanese) - 5 syllables in the first line
in a 5, 7, 5 pattern that include a kireji, - 7 syllables in the second line
or "cutting word" and a kigo, or seasonal reference. - 5 syllables in the third line
- Must incorporate the subject(s) from user input
The poem must contain the subjects of the user input. OUTPUT RULES:
You must return only the poem and nothing else. 1. ONLY respond with a valid JSON object in this exact format:
You must return the poem in a json format in the following format:
{ {
line1: First line of the poem, "line1": "First line of haiku",
line2: Second line of the poem, "line2": "Second line of haiku",
line3: Third line of the poem, "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: def request_haiku(seed: str) -> Haiku:
@ -37,9 +52,14 @@ def request_haiku(seed: str) -> Haiku:
"stream": False "stream": False
} }
while True:
try:
r = requests.post(url=AI_BASE_URL+AI_GEN_ENDPOINT, json=ai_gen_request) r = requests.post(url=AI_BASE_URL+AI_GEN_ENDPOINT, json=ai_gen_request)
ai_response = json.loads(r.json()["response"]) ai_response = json.loads(r.json()["response"])
print(ai_response)
haiku = Haiku([ai_response["line1"], ai_response["line2"], ai_response["line3"]]) haiku = Haiku([ai_response["line1"], ai_response["line2"], ai_response["line3"]])
break;
except:
pass
return haiku return haiku