feat: Add promt generation from webserver

Refs: OPS-22
This commit is contained in:
Alivecow 2025-02-28 21:33:09 +01:00
parent 91e4567bf6
commit 7e5d61663a
6 changed files with 250 additions and 9 deletions

41
senju/ai_gen.py Normal file
View file

@ -0,0 +1,41 @@
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 helpful AI agent whos task it is to generate Haikus from user input.
Here is the definition of a Haiku:
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.
You must return only the poem and nothing else.
You must return the poem in a json format in the following format:
{
line1: First line of the poem,
line2: Second line of the poem,
line3: Third line of the poem,
}
Your input is as follows:
"""
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
}
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"]])
return haiku

View file

@ -1,6 +1,10 @@
from dataclasses import dataclass
import json
@dataclass
class Haiku:
lines: list[str]
def get_json(self):
return json.dumps(self.lines)

View file

@ -1,6 +1,8 @@
from pathlib import Path
from flask import Flask, redirect, render_template, url_for
from flask import Flask, redirect, render_template, url_for, request
from senju import haiku
from senju.ai_gen import request_haiku
from senju.haiku import Haiku
from senju.store_manager import StoreManager
@ -45,3 +47,13 @@ def prompt_view():
"prompt.html",
title="Haiku generation"
)
@app.route("/generate_haiku", methods=['POST'])
def generate_haiku():
prompt = "a"
print("Generation function")
if request.method == 'POST':
json_data = request.get_json()
prompt = json_data["prompt"]
haiku = request_haiku(prompt)
return haiku.get_json()

View file

@ -1,4 +1,4 @@
{% extends "base.jinja" %}
{% extends "base.html" %}
{% block content %}
<div class="flex flex-col items-center justify-center min-h-screen bg-violet-900 text-white p-6">
@ -48,10 +48,24 @@ document.getElementById("submit-btn").addEventListener("click", function() {
responseText.textContent = "🤖 AI is thinking...";
responseBox.classList.remove("opacity-0");
// Simulated AI response delay
setTimeout(() => {
responseText.textContent = `"${userInput}" sounds interesting! Let's explore more! 🌟`;
}, 1500);
console.log(userInput );
fetch('/generate_haiku', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({'prompt': userInput})
})
.then(response => response.json())
.then(data => {
console.log(data);
responseText.innerHTML = data[0] + "<br>" + data[1] + "<br>" + data[2];
})
.catch(error => {
document.getElementById('result').innerHTML = '<strong>Error:</strong> ' + error.message;
});
}
});
</script>