1
0
Fork 0
forked from zovos/bot_tg
bot_tg/games/fortune.py

57 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import aiohttp
import os
logger = logging.getLogger(__name__)
LLAMA_API_URL = os.getenv("LLAMA_API_URL", "https://mirror.porno4free.ru/zovos-ai/")
FORTUNE_PROMPT = (
"Ты — уличный гадатель с района, базаришь на фене. "
"Пользователь хочет погадать на тему. Выдай шуточное гадание, "
"коротко и дерзко, 2-3 предложения максимум. "
"В конце ОБЯЗАТЕЛЬНО напиши на отдельной строке в формате:\n"
"MEM_UP: <текст сверху для мема, максимум 4 слова>\n"
"MEM_DOWN: <текст снизу для мема, максимум 4 слова>\n"
"Тексты для мема должны быть смешными и короткими."
)
async def generate_fortune(topic: str) -> tuple[str, str, str]:
url = f"{LLAMA_API_URL.rstrip('/')}/v1/chat/completions"
payload = {
"messages": [
{"role": "system", "content": FORTUNE_PROMPT},
{"role": "user", "content": f"Погадай мне на: {topic}"},
],
"max_tokens": 200,
"temperature": 0.9,
"top_p": 0.9,
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload, timeout=aiohttp.ClientTimeout(total=120)) as resp:
data = await resp.json()
text = data["choices"][0]["message"]["content"].strip()
up_text = ""
down_text = ""
fortune_lines = []
for line in text.split("\n"):
stripped = line.strip()
if stripped.upper().startswith("MEM_UP:"):
up_text = stripped.split(":", 1)[1].strip()
elif stripped.upper().startswith("MEM_DOWN:"):
down_text = stripped.split(":", 1)[1].strip()
else:
fortune_lines.append(line)
fortune_text = "\n".join(fortune_lines).strip()
if not up_text:
up_text = "Гадание"
if not down_text:
down_text = "по фене"
return fortune_text, up_text, down_text