Files
Jogo3/dados_ia.py
T
2026-10-10 08:27:19 -03:00

71 lines
2.4 KiB
Python

import json
import os
import logging
import httpx
log = logging.getLogger(__name__)
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
OPENROUTER_MODEL = os.getenv("OPENROUTER_MODEL", "deepseek/deepseek-v4.1-flash")
OPENROUTER_BASE_URL = os.getenv("OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1")
OPENROUTER_TIMEOUT = int(os.getenv("OPENROUTER_TIMEOUT", "60"))
OPENROUTER_MAX_TOKENS = int(os.getenv("OPENROUTER_MAX_TOKENS", "2048"))
OPENROUTER_MAX_LINHAS = int(os.getenv("OPENROUTER_MAX_LINHAS", "0"))
def carregar_prompt_base() -> str:
caminho = os.getenv("PROMPT_IA_ARQUIVO", "prompt_ia.json")
caminho = os.path.join(os.path.dirname(__file__), caminho)
with open(caminho, "r", encoding="utf-8") as f:
dados = json.load(f)
if isinstance(dados, list) and len(dados) > 0:
return dados[0]
return ""
async def gerar_ordem_ia() -> str:
prompt_base = carregar_prompt_base()
if not OPENROUTER_API_KEY:
log.error("OPENROUTER_API_KEY nao configurada no .env")
return "[ERRO: API key nao configurada. Configure OPENROUTER_API_KEY no .env]"
user_msg = "Gere uma ordem de humilhação agora, completa e detalhada, como se fosse o Mestre DeepSeek falando. Nao se repita. Seja criativa e picante."
if OPENROUTER_MAX_LINHAS > 0:
user_msg += f" A ordem deve ter no maximo {OPENROUTER_MAX_LINHAS} linhas."
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json",
}
payload = {
"model": OPENROUTER_MODEL,
"messages": [
{
"role": "system",
"content": prompt_base,
},
{
"role": "user",
"content": user_msg,
},
],
"max_tokens": OPENROUTER_MAX_TOKENS,
"temperature": 0.9,
}
try:
async with httpx.AsyncClient(timeout=OPENROUTER_TIMEOUT) as client:
resp = await client.post(
f"{OPENROUTER_BASE_URL}/chat/completions",
headers=headers,
json=payload,
)
resp.raise_for_status()
data = resp.json()
ordem = data["choices"][0]["message"]["content"].strip()
return ordem
except Exception as e:
log.error("Erro ao gerar ordem com IA: %s", e)
return f"[ERRO ao gerar ordem: {e}]"