senju/senju/haiku.py
Guts 440aaa2bd4
Merge pull request #29 from senju1337/feat/OPS-61
feat/OPS-61: auto documentation
2025-03-20 11:42:14 +01:00

68 lines
1.4 KiB
Python

from __future__ import annotations
import json
import logging
from dataclasses import dataclass
import requests
AI_BASE_URL: str = "http://ollama:11434/api"
AI_GEN_ENDPOINT: str = "/generate"
def foobar():
"""WE KNOW"""
a = 3
b = 3
return a + b
@dataclass
class Haiku:
lines: list[str]
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": "haiku",
"prompt": f"{seed}",
"stream": False,
"eval_count": 20
}
while True:
try:
r = requests.post(url=AI_BASE_URL + AI_GEN_ENDPOINT,
json=ai_gen_request)
ai_response = str(r.json()["response"])
logging.warning(ai_response)
lines = ai_response.split("\n")
for _ in range(0, 2):
lines.pop()
logging.warning(lines)
if len(lines) != 3:
continue
haiku = Haiku(
[
lines[0],
lines[1],
lines[2]
])
break
except json.JSONDecodeError:
continue
return haiku