mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39:29 +00:00
68 lines
1.4 KiB
Python
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
|