mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 15:49:29 +00:00
refactor: Addressing PR comments
Refs: OPS-12 Consolidated AI gen module Changed API endpoint
This commit is contained in:
parent
edf145c8de
commit
76ad52d3a9
4 changed files with 73 additions and 79 deletions
|
|
@ -3,6 +3,48 @@ from __future__ import annotations
|
|||
import json
|
||||
from dataclasses import dataclass
|
||||
|
||||
import requests
|
||||
|
||||
AI_BASE_URL: str = "http://ollama:11434/api"
|
||||
AI_GEN_ENDPOINT: str = "/generate"
|
||||
|
||||
AI_GEN_SYS_PROMPT = """
|
||||
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 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
|
||||
|
||||
OUTPUT RULES:
|
||||
1. ONLY respond with a valid JSON object in this exact format:
|
||||
{
|
||||
"line1": "First line of haiku",
|
||||
"line2": "Second line of haiku",
|
||||
"line3": "Third line of haiku"
|
||||
}
|
||||
|
||||
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:
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class Haiku:
|
||||
|
|
@ -10,3 +52,31 @@ class Haiku:
|
|||
|
||||
def get_json(self):
|
||||
return json.dumps(self.lines)
|
||||
|
||||
@staticmethod
|
||||
def request_haiku(seed: str) -> Haiku:
|
||||
"""This function prompts the ai to generate
|
||||
the hauku based on the user input"""
|
||||
|
||||
ai_gen_request = {
|
||||
"model": "llama3.2:1b",
|
||||
"prompt": f"{AI_GEN_SYS_PROMPT}{seed}",
|
||||
"stream": False
|
||||
}
|
||||
|
||||
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 json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
return haiku
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue