mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39: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
|
|
@ -1,75 +0,0 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
from senju.haiku import Haiku
|
||||
|
||||
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:
|
||||
"""
|
||||
|
||||
|
||||
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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ from pathlib import Path
|
|||
|
||||
from flask import Flask, redirect, render_template, request, url_for
|
||||
|
||||
from senju.ai_gen import request_haiku
|
||||
from senju.haiku import Haiku
|
||||
from senju.store_manager import StoreManager
|
||||
|
||||
|
|
@ -51,13 +50,13 @@ def prompt_view():
|
|||
)
|
||||
|
||||
|
||||
@app.route("/generate_haiku", methods=['POST'])
|
||||
@app.route("/api/v1/haiku", methods=['POST'])
|
||||
def generate_haiku():
|
||||
prompt = "a"
|
||||
if request.method == 'POST':
|
||||
json_data = request.get_json()
|
||||
prompt = json_data["prompt"]
|
||||
haiku = request_haiku(prompt)
|
||||
haiku = Haiku.request_haiku(prompt)
|
||||
id = store.save_haiku(haiku)
|
||||
return str(id)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ document.getElementById("submit-btn").addEventListener("click", function() {
|
|||
|
||||
console.log(userInput );
|
||||
|
||||
fetch('/generate_haiku', {
|
||||
fetch('/api/v1/haiku', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue