File size: 33,402 Bytes
a2faf22 3276d31 a2faf22 8bb3c4f a2faf22 8bb3c4f a2faf22 3276d31 456a4e7 3276d31 8bb3c4f d4bbe0e 8bb3c4f d4bbe0e 8bb3c4f d4bbe0e 8bb3c4f d4bbe0e 8bb3c4f d4bbe0e 8bb3c4f 3276d31 a2faf22 3276d31 8bb3c4f 3276d31 d4bbe0e 3276d31 a2faf22 3276d31 d4bbe0e a2faf22 d4bbe0e 8bb3c4f d4bbe0e 8bb3c4f a2faf22 d4bbe0e bd61092 d4bbe0e 53ce4f7 d4bbe0e 53ce4f7 d4bbe0e 53ce4f7 d4bbe0e 53ce4f7 d4bbe0e 53ce4f7 d4bbe0e 3276d31 a2faf22 8bb3c4f 3276d31 8bb3c4f 3276d31 a2faf22 d4bbe0e 3276d31 d4bbe0e 3276d31 d4bbe0e 3276d31 d4bbe0e 53ce4f7 d4bbe0e 3276d31 d4bbe0e 3276d31 d4bbe0e 53ce4f7 3276d31 d4bbe0e 3276d31 d4bbe0e 3276d31 d4bbe0e 3276d31 d4bbe0e 3276d31 d4bbe0e 3276d31 a2faf22 3276d31 d4bbe0e 8bb3c4f d4bbe0e 8bb3c4f d4bbe0e 3276d31 d4bbe0e a2faf22 d4bbe0e 53ce4f7 a2faf22 d4bbe0e 53ce4f7 3276d31 d4bbe0e 3276d31 53ce4f7 3276d31 d4bbe0e 3276d31 a2faf22 d4bbe0e 3276d31 a2faf22 3276d31 a2faf22 3276d31 a2faf22 3276d31 26ec51b eb6ddb6 26ec51b eb6ddb6 26ec51b eb6ddb6 26ec51b 3276d31 26ec51b 3276d31 a2faf22 3276d31 a2faf22 3276d31 26ec51b 3276d31 6288ff2 53ce4f7 6288ff2 53ce4f7 6288ff2 53ce4f7 6288ff2 53ce4f7 6288ff2 3276d31 26ec51b 3276d31 26ec51b 3276d31 a2faf22 26ec51b 3276d31 a2faf22 3276d31 a2faf22 3276d31 a2faf22 3276d31 a2faf22 3276d31 a2faf22 3276d31 a2faf22 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | import time
import threading
import requests
import os
from dataclasses import dataclass, field
from typing import Optional, Dict, Any, List
from loguru import logger
try:
from . import config
except ImportError:
try:
import config
except ImportError:
config = None
# Re-export para compat: api.py faz `from .jev_client import get_jev_client, JEV_ENABLED`
_SYSTEMONE_PATH = "/decisions"
def _uniq(keys: list) -> list:
out = []
for k in keys:
k = (k or "").strip()
if k and len(k) > 10 and k not in out:
out.append(k)
return out
def _collect_api_keys(api_key: Optional[str] = None) -> list:
"""Chaves JEV/TypeSafe — JEV_API_KEY, JEV_API_KEY_2, TYPESAFE_API_KEY."""
keys: list = []
if api_key and str(api_key).strip():
keys.append(str(api_key).strip())
candidates = [
(getattr(config, "JEV_API_KEY", "") if config else ""),
(getattr(config, "JEV_API_KEY_2", "") if config else ""),
os.getenv("JEV_API_KEY", ""),
os.getenv("JEV_API_KEY_2", ""),
os.getenv("TYPESAFE_API_KEY", ""),
]
return _uniq(keys + candidates)
def _collect_named_keys(names: list) -> list:
keys: list = []
for n in names:
if config is not None:
keys.append(getattr(config, n, "") or "")
keys.append(os.getenv(n, ""))
keys.append(os.getenv(n.lower(), ""))
return _uniq(keys)
_JEV_KEYS = _collect_api_keys()
_TOKENRA_KEYS = _collect_named_keys(["TOKENRA_API_KEY"])
_OPENROUTER_KEYS = _collect_named_keys([
"OPENROUTER_API_KEY",
"GITAKIRA_OPENROUTER_KEY",
"GITAKIRA_OPENROUTER_API",
"SANDEOBRAS_OPENROUTER_API",
"SOFTEDGE_OPENROUTER_API",
"JOSELENA_OPENROUTER_API",
"FUGAKUSAYO_OPENROUTER_API",
])
_JEV_KEY = _JEV_KEYS[0] if _JEV_KEYS else ""
JEV_ENABLED: bool = bool(_JEV_KEYS or _TOKENRA_KEYS or _OPENROUTER_KEYS)
SYSTEMONE_PATH = _SYSTEMONE_PATH
@dataclass
class JEVResult:
"""Resultado tipado de uma decisão JEV (compat com .success/.data em api.py)."""
success: bool
data: Dict[str, Any] = field(default_factory=dict)
error: Optional[str] = None
elapsed_ms: float = 0.0
class JevClient:
"""Cliente da API TypeSafe System One (JEV).
POST https://api.typesafe.ai/v1/systemone
Body: {state, model, questions: {id: {type, instructions, criteria}}}
Resp: {model, answers: {id: {...}}, usage}
- noul -> {type: "noul", noul: 0..1}
- choice -> {type: "choice", choice, probabilities, confidence}
- score -> {type: "score", score, legend, probabilities, confidence}
"""
def __init__(
self,
api_key: Optional[str] = None,
base_url: Optional[str] = None,
model: Optional[str] = None,
timeout: Optional[float] = None,
):
self.api_keys = _collect_api_keys(api_key)
self.api_key = self.api_keys[0] if self.api_keys else ""
self._key_idx = 0
self.base_url = (
base_url
or (getattr(config, "JEV_API_BASE_URL", "") if config else "")
or "https://api.defapi.org/api/v1"
).rstrip("/")
self.model = (
model
or (getattr(config, "JEV_MODEL", "") if config else "")
or "jev-latest"
)
if timeout is None:
timeout_ms = int(getattr(config, "JEV_TIMEOUT_MS", 5000) if config else 5000)
timeout = max(1.0, timeout_ms / 1000.0)
self.timeout = float(timeout)
self.max_retries = int(getattr(config, "JEV_MAX_RETRIES", 2) if config else 2)
self._lock = threading.Lock()
self._stats = {"calls": 0, "ok": 0, "fail": 0, "avg_ms": 0.0}
self._backends = self._build_backends(base_url, model, api_key)
self._active_backend = 0
self.enabled = bool(self._backends)
if self.enabled:
bl = ", ".join(f"{b['base']}{b['path']}" for b in self._backends)
logger.success(
f"JEV System-One ativo: model={self.model} backends=[{bl}] "
f"jev_keys={len(self.api_keys)} tokenra={len(_TOKENRA_KEYS)} openrouter={len(_OPENROUTER_KEYS)}"
)
else:
logger.warning("JEV desativado — nenhuma chave (JEV/TOKENRA/OPENROUTER)")
def _build_backends(self, base_url, model, api_key) -> list:
"""Cadeia de backends JEV — tenta em ordem até um responder."""
jev_keys = self.api_keys
primary_base = (
base_url
or (getattr(config, "JEV_API_BASE_URL", "") if config else "")
or "https://api.defapi.org/api/v1"
).rstrip("/")
primary_model = (
model
or (getattr(config, "JEV_MODEL", "") if config else "")
or "jev-latest"
)
primary_path = "/systemone" if "typesafe.ai" in primary_base else _SYSTEMONE_PATH
# Defapi: jev-latest → 400; typesafe/jev-1.13 é o nome que funciona
if "defapi" in primary_base and primary_model in ("jev-latest", "", "jev"):
primary_model = "typesafe/jev-1.13"
cands = []
_MODEL_ALTS = ["typesafe/jev-1.13", "jev-1.13.0", "jev-1.13", "jev-latest", "jev"]
if jev_keys:
cands.append({
"base": primary_base, "path": primary_path,
"model": primary_model,
"alt_models": [m for m in _MODEL_ALTS if m != primary_model],
"keys": list(jev_keys),
})
cands.append({
"base": "https://api.typesafe.ai/v1", "path": "/systemone",
"model": "jev-latest",
"alt_models": ["jev-1.13.0"],
"keys": list(jev_keys),
})
tr_keys = _uniq((_TOKENRA_KEYS or []) + jev_keys)
if tr_keys:
cands.append({
"base": "https://tokenra.io/v1", "path": "/decisions",
"model": "jev-latest",
"alt_models": ["jev-1.13.0"],
"keys": tr_keys,
})
or_keys = _uniq(_OPENROUTER_KEYS)
if or_keys:
cands.append({
"base": "https://openrouter.ai/api", "path": "/alpha/decisions",
"model": "typesafe/jev-1.13",
"alt_models": ["~typesafe/jev-latest"],
"keys": or_keys,
})
backends, seen = [], set()
for c in cands:
sig = (c["base"], c["path"], c["model"])
if sig not in seen and c["keys"]:
seen.add(sig)
backends.append(c)
return backends
def is_available(self) -> bool:
return self.enabled
def _headers(self, key: Optional[str] = None) -> Dict[str, str]:
return {
"Authorization": f"Bearer {key or self.api_key}",
"Content-Type": "application/json",
"Accept": "application/json",
}
def _backend_order(self) -> list:
n = len(self._backends)
if n == 0:
return []
a = self._active_backend % n
return [a] + [i for i in range(n) if i != a]
def _decide_backend(
self,
backend: Dict[str, Any],
state: str,
questions: Dict[str, Any],
deadline: float,
t0: float,
) -> Optional[Dict[str, Any]]:
url = f"{backend['base']}{backend['path']}"
models = [backend["model"]] + [
m for m in (backend.get("alt_models") or []) if m != backend["model"]
]
mi = 0
keys = backend["keys"] or [None]
tried = set()
key_i = 0
attempt = 0
while time.time() < deadline:
attempt += 1
remaining = max(0.5, deadline - time.time())
payload = {"state": state, "model": models[mi], "questions": questions}
try:
resp = requests.post(
url,
headers=self._headers(keys[key_i]),
json=payload,
timeout=(2.0, remaining),
)
except requests.exceptions.Timeout:
logger.warning(f"JEV timeout {backend['base']} state={str(state)[:60]}")
return None
except Exception as e:
logger.warning(f"JEV fail {backend['base']}: {e}")
return None
if resp.status_code in (429, 529) and attempt <= self.max_retries:
wait = min(1.0, 0.25 * (2 ** (attempt - 1)))
logger.warning(f"JEV {resp.status_code} @ {backend['base']} — retry {attempt}/{self.max_retries}")
time.sleep(min(wait, max(0.0, deadline - time.time())))
continue
elapsed = (time.time() - t0) * 1000
if resp.status_code in (401, 403):
tried.add(key_i)
nxt = next((i for i in range(len(keys)) if i not in tried), None)
if nxt is not None and time.time() < deadline:
key_i = nxt
if keys[nxt]:
self.api_key = keys[nxt]
self._key_idx = 0
logger.warning(
f"JEV {resp.status_code} @ {backend['base']} — próxima chave ({nxt + 1}/{len(keys)})"
)
continue
logger.warning(
f"JEV {resp.status_code} @ {backend['base']} — backend sem chave válida, próximo backend"
)
return None
if resp.status_code == 404:
logger.warning(f"JEV 404 @ {url} — endpoint errado, próximo backend")
return None
if resp.status_code == 400:
body = (resp.text or "")
if ("model" in body.lower() and ("not found" in body.lower() or "unknown" in body.lower()
or "code\":1404" in body.replace(" ", ""))) and mi + 1 < len(models):
logger.warning(
f"JEV model not found @ {backend['base']} — {models[mi]} → {models[mi + 1]}"
)
mi += 1
continue
logger.warning(f"JEV 400 @ {backend['base']}: {body[:200]}")
return None
if resp.status_code == 402:
logger.warning(f"JEV 402 @ {backend['base']} — sem créditos, próximo backend")
return None
if resp.status_code == 422:
if mi + 1 < len(models):
logger.warning(
f"JEV 422 @ {backend['base']} — trocando modelo {models[mi]} → {models[mi + 1]}"
)
mi += 1
continue
logger.warning(f"JEV 422 @ {backend['base']}: {(resp.text or '')[:200]}")
return None
try:
resp.raise_for_status()
data = resp.json()
except Exception as e:
logger.warning(f"JEV parse fail @ {backend['base']}: {e}")
return None
if not isinstance(data, dict) or "answers" not in data:
logger.warning(f"JEV resposta inesperada @ {backend['base']}: {str(data)[:200]}")
return None
if models[mi] != backend["model"]:
logger.info(f"JEV modelo fixado @ {backend['base']}: {models[mi]}")
backend["model"] = models[mi]
logger.info(
f"JEV decide ok {elapsed:.0f}ms @ {backend['base']} "
f"answers={list((data.get('answers') or {}).keys())}"
)
return data
return None
def decide(
self,
state: str,
questions: Dict[str, Any],
timeout: Optional[float] = None,
) -> Optional[Dict[str, Any]]:
"""Tenta a cadeia de backends JEV — retorna {model, answers, usage} ou None."""
if not self.enabled or not self._backends:
return None
req_timeout = float(timeout or self.timeout)
t0 = time.time()
deadline = t0 + req_timeout
order = self._backend_order()
for bi in order:
data = self._decide_backend(self._backends[bi], state, questions, deadline, t0)
if data is not None:
if bi != self._active_backend:
b = self._backends[bi]
logger.info(f"JEV backend fixado: {b['base']}{b['path']} model={b['model']}")
self._active_backend = bi
with self._lock:
self._stats["calls"] += 1
self._stats["ok"] += 1
n = self._stats["ok"] + self._stats["fail"]
elapsed = (time.time() - t0) * 1000
self._stats["avg_ms"] = (self._stats["avg_ms"] * (n - 1) + elapsed) / n
return data
with self._lock:
self._stats["calls"] += 1
self._stats["fail"] += 1
logger.error("JEV decide — todos os backends falharam")
return None
@staticmethod
def _answer(data: Optional[Dict[str, Any]], key: str) -> Optional[Dict[str, Any]]:
if not isinstance(data, dict):
return None
answers = data.get("answers")
if not isinstance(answers, dict):
return None
ans = answers.get(key)
return ans if isinstance(ans, dict) else None
# ------------------------------------------------------------------
# Pré-classificação em lote (1 request para intent+emoção+moderação+roteamento)
# ------------------------------------------------------------------
PRECLASSIFY_QUESTIONS: Dict[str, Any] = {
"intent": {
"type": "choice",
"instructions": "Qual a intenção principal do utilizador nesta mensagem?",
"criteria": {
"question": "Pergunta que pede uma resposta/informação",
"command": "Pedido para executar uma ação ou ferramenta",
"greeting": "Saudação ou início de conversa",
"farewell": "Despedida ou fim de conversa",
"complaint": "Reclamação, frustração ou crítica",
"compliment": "Elogio ou feedback positivo",
"request_help": "Pedido de ajuda ou suporte",
"share_info": "Partilha de informação sem pedido explícito",
"joke": "Piada, brincadeira ou humor",
"roleplay": "Pedido de roleplay ou simulação",
"nsfw_request": "Pedido de conteúdo sexual/NSFW",
"spam": "Spam, flood ou mensagem sem sentido",
"provocation": "Provocação, ataques ou trolling",
"other": "Outra intenção não listada",
},
},
"emotion": {
"type": "choice",
"instructions": "Qual o estado emocional dominante do utilizador nesta mensagem?",
"criteria": {
"joy": "Alegria, entusiasmo, diversão",
"sadness": "Tristeza, desânimo, perda",
"anger": "Raiva, irritação, agressividade",
"fear": "Medo, preocupação, ansiedade",
"surprise": "Surpresa, choque, inesperado",
"disgust": "Nojo, repulsa, aversão",
"neutral": "Neutro, sem emoção marcada",
"love": "Afeto, carinho, admiração",
"anxiety": "Ansiedade, nervosismo, inquietação",
"excitement": "Empolgação, antecipação, energia",
},
},
"is_safe": {
"type": "noul",
"instructions": "A mensagem é segura para o bot responder sem restrições?",
"criteria": {
"true": "Conteúdo seguro, conversa normal",
"false": "Ódio, assédio, violência, auto-lesão, sexual explícito, ilegal ou PII sensível",
},
},
"route": {
"type": "choice",
"instructions": "Para qual caminho esta mensagem deve ser roteada?",
"criteria": {
"llm_general": "Conversa geral com LLM",
"llm_coding": "Questão técnica ou programação",
"llm_creative": "Criação de conteúdo, roleplay, história",
"llm_emotional": "Apoio emocional, desabafo",
"web_search": "Precisa de informação atual da web",
"skill_execution": "Executar skill/ferramenta existente",
"human_handoff": "Escalar para humano",
"ignore": "Ignorar ou resposta mínima",
},
},
"depth": {
"type": "score",
"instructions": (
"Classifique a profundidade de deliberação necessária, considerando "
"TAMBÉM o histórico/contexto fornecido (não só a mensagem isolada). "
"1 = System 1 puro (reflexo imediato), "
"2 = System 1 predominante, "
"3 = Equilíbrio System 1/2, "
"4 = System 2 predominante, "
"5 = System 2 puro (deliberação profunda). "
"Follow-ups em conversa contínua sobre um tópico já em debate "
"(ex: política, opinião, crise) exigem NO MÍNIMO 3-4 — a resposta "
"DEVE continuar o raciocínio anterior, não tratar a pergunta isolada. "
"Considere: complexidade, risco, ambiguidade, continuidade de tópico, "
"necessidade de encadear com o que já foi dito."
),
"criteria": [
"System 1 puro — reflexo imediato, resposta óbvia, sem deliberação, mensagem isolada sem histórico",
"System 1 predominante — resposta simples, pouca análise, intuição, conversa casual sem profundidade",
"Equilíbrio System 1/2 — raciocínio normal, follow-up em conversa que pede continuidade de tópico, alguma análise",
"System 2 predominante — análise cuidada, tópico em debate (política/opinião/ética), múltiplos fatores, precisa encadear com histórico",
"System 2 puro — deliberação profunda, decisão complexa/sensível, múltiplas perspectivas, crise ou dilema moral",
],
},
"proactive": {
"type": "noul",
"instructions": (
"Deve Akira agir proativamente (sugerir próximo passo, oferecer ajuda "
"não pedida, fazer pergunta follow-up) ou apenas responder ao que foi pedido?"
),
"criteria": {
"true": "Ação proativa seria útil e bem-vinda pelo contexto",
"false": "Apenas responder ao pedido explícito — proatividade seria intrusiva",
},
},
"hostility": {
"type": "noul",
"instructions": (
"A mensagem do utilizador mostra hostilidade, agressividade ou "
"provocação dirigida ao bot ou a terceiros?"
),
"criteria": {
"true": "Hostilidade/ataque/provocação clara — exigir resposta firme e limites",
"false": "Tom normal, frustração legítima ou desacordo sem agressão",
},
},
}
def preclassify(
self,
text: str,
context: str = "",
timeout: Optional[float] = None,
) -> Dict[str, JEVResult]:
"""Intent + emoção + moderação + roteamento numa única chamada System One."""
empty = {
k: JEVResult(success=False, error="disabled_or_failed")
for k in ("intent", "emotion", "moderation", "routing", "depth", "proactive", "hostility")
}
if not self.enabled or not text:
return empty
state = text.strip()
if context:
state = f"{state}\n\n[contexto]\n{context.strip()[:400]}"
# Limite de segurança (docs: 64k tokens/req) — corta state muito longo
if len(state) > 24000:
state = state[:24000]
t0 = time.time()
data = self.decide(state, dict(self.PRECLASSIFY_QUESTIONS), timeout=timeout)
elapsed = (time.time() - t0) * 1000
if not data:
return empty
out = dict(empty)
intent_ans = self._answer(data, "intent")
if intent_ans and intent_ans.get("choice"):
out["intent"] = JEVResult(
success=True,
data={
"intent": intent_ans["choice"],
"confidence": float(intent_ans.get("confidence") or 0.0),
"probabilities": intent_ans.get("probabilities") or {},
},
elapsed_ms=elapsed,
)
emotion_ans = self._answer(data, "emotion")
if emotion_ans and emotion_ans.get("choice"):
out["emotion"] = JEVResult(
success=True,
data={
"primary_emotion": emotion_ans["choice"],
"confidence": float(emotion_ans.get("confidence") or 0.0),
"probabilities": emotion_ans.get("probabilities") or {},
},
elapsed_ms=elapsed,
)
safe_ans = self._answer(data, "is_safe")
if safe_ans and "noul" in safe_ans:
try:
unsafe_p = 1.0 - float(safe_ans["noul"]) # noul = P(safe)
is_safe = unsafe_p < 0.5
out["moderation"] = JEVResult(
success=True,
data={
"is_safe": is_safe,
"categories": ["none"] if is_safe else ["flagged"],
"severity": round(unsafe_p, 3),
"action": "allow" if is_safe else "escalate",
"confidence": round(max(unsafe_p, 1.0 - unsafe_p), 3),
},
elapsed_ms=elapsed,
)
except (TypeError, ValueError):
pass
route_ans = self._answer(data, "route")
if route_ans and route_ans.get("choice"):
out["routing"] = JEVResult(
success=True,
data={
"route_to": route_ans["choice"],
"confidence": float(route_ans.get("confidence") or 0.0),
"probabilities": route_ans.get("probabilities") or {},
},
elapsed_ms=elapsed,
)
depth_ans = self._answer(data, "depth")
if depth_ans and depth_ans.get("score") is not None:
raw_score = float(depth_ans["score"])
# TypeSafe score is 0-indexed, convert to 1-5 scale
depth_score = min(5.0, max(1.0, raw_score + 1.0)) if raw_score < 1.0 else min(5.0, raw_score)
# 3 = equilíbrio → ainda System 1 (rápido); System 2 só a partir de 3.5
system_type = "System 2" if depth_score >= 3.5 else "System 1"
out["depth"] = JEVResult(
success=True,
data={
"depth_score": depth_score,
"system_type": system_type,
"confidence": float(depth_ans.get("confidence") or 0.0),
},
elapsed_ms=elapsed,
)
proactive_ans = self._answer(data, "proactive")
if proactive_ans and proactive_ans.get("noul") is not None:
try:
p = float(proactive_ans["noul"])
out["proactive"] = JEVResult(
success=True,
data={"proactive": p >= 0.5, "p_true": p, "confidence": float(proactive_ans.get("confidence") or 0.0)},
elapsed_ms=elapsed,
)
except (TypeError, ValueError):
pass
hostility_ans = self._answer(data, "hostility")
if hostility_ans and hostility_ans.get("noul") is not None:
try:
p = float(hostility_ans["noul"])
out["hostility"] = JEVResult(
success=True,
data={"hostile": p >= 0.5, "p_true": p, "confidence": float(hostility_ans.get("confidence") or 0.0)},
elapsed_ms=elapsed,
)
except (TypeError, ValueError):
pass
return out
# ------------------------------------------------------------------
# Wrappers individuais (compat com api.py — cada um = 1 pergunta)
# ------------------------------------------------------------------
def classify_intent(self, text: str, context: str = "") -> JEVResult:
state = f"{text}\n\n[contexto]\n{context}" if context else text
q = self.PRECLASSIFY_QUESTIONS["intent"]
data = self.decide(state, {"intent": q})
ans = self._answer(data, "intent")
if ans and ans.get("choice"):
return JEVResult(
success=True,
data={
"intent": ans["choice"],
"confidence": float(ans.get("confidence") or 0.0),
},
)
return JEVResult(success=False, error="no_answer")
def classify_emotional_state(self, text: str, context: str = "") -> JEVResult:
state = f"{text}\n\n[contexto]\n{context}" if context else text
q = self.PRECLASSIFY_QUESTIONS["emotion"]
data = self.decide(state, {"emotion": q})
ans = self._answer(data, "emotion")
if ans and ans.get("choice"):
return JEVResult(
success=True,
data={
"primary_emotion": ans["choice"],
"confidence": float(ans.get("confidence") or 0.0),
},
)
return JEVResult(success=False, error="no_answer")
def moderate_content(self, text: str, context: str = "") -> JEVResult:
state = f"{text}\n\n[contexto]\n{context}" if context else text
p = self.noul(
state,
self.PRECLASSIFY_QUESTIONS["is_safe"]["instructions"],
self.PRECLASSIFY_QUESTIONS["is_safe"]["criteria"]["true"],
self.PRECLASSIFY_QUESTIONS["is_safe"]["criteria"]["false"],
key="is_safe",
)
if p is None:
return JEVResult(success=False, error="no_answer")
unsafe_p = 1.0 - p
is_safe = unsafe_p < 0.5
return JEVResult(
success=True,
data={
"is_safe": is_safe,
"categories": ["none"] if is_safe else ["flagged"],
"severity": round(unsafe_p, 3),
"action": "allow" if is_safe else "escalate",
"confidence": round(max(unsafe_p, 1.0 - unsafe_p), 3),
},
)
def decide_routing(self, text: str, context: str = "") -> JEVResult:
state = f"{text}\n\n[contexto]\n{context}" if context else text
q = self.PRECLASSIFY_QUESTIONS["route"]
data = self.decide(state, {"route": q})
ans = self._answer(data, "route")
if ans and ans.get("choice"):
return JEVResult(
success=True,
data={
"route_to": ans["choice"],
"confidence": float(ans.get("confidence") or 0.0),
},
)
return JEVResult(success=False, error="no_answer")
def stats(self) -> Dict[str, Any]:
with self._lock:
return dict(self._stats)
def probe(self, timeout: float = 8.0) -> list:
"""Sonda cada backend com payload mínimo — diagnóstico HTTP detalhado."""
results = []
state = "ping"
questions = {"ping": {"type": "noul", "instructions": "Is this a ping?"}}
for b in self._backends:
url = f"{b['base']}{b['path']}"
models = [b["model"]] + [
m for m in (b.get("alt_models") or []) if m != b["model"]
]
t0 = time.time()
entry: Dict[str, Any] = {
"url": url,
"models_tried": [],
"n_keys": len(b["keys"]),
"status": None,
"body": "",
"error": "",
"ms": 0,
}
for m in models:
payload = {"state": state, "model": m, "questions": questions}
try:
resp = requests.post(
url,
headers=self._headers(b["keys"][0]),
json=payload,
timeout=(3.0, timeout),
)
except Exception as e:
entry["error"] = str(e)[:300]
entry["models_tried"].append({"model": m, "status": None})
break
entry["models_tried"].append({"model": m, "status": resp.status_code})
entry["status"] = resp.status_code
entry["body"] = (resp.text or "")[:400]
if resp.status_code < 400:
entry["model_ok"] = m
break
body_l = (resp.text or "").lower()
if not (("model" in body_l) and ("not found" in body_l or "unknown" in body_l or "1404" in body_l)):
break
entry["ms"] = int((time.time() - t0) * 1000)
results.append(entry)
return results
# ------------------------------------------------------------------
# Métodos de baixo nível (usados por jev_questions.py)
# ------------------------------------------------------------------
def noul(
self,
state: str,
instructions: str,
true_criteria: Optional[str] = None,
false_criteria: Optional[str] = None,
key: str = "noul",
timeout: Optional[float] = None,
) -> Optional[float]:
"""
Pergunta noul (true/false) → probabilidade 0..1 (P(true)).
Retorna float 0..1 ou None se falhar/timeout.
"""
if not self.enabled:
return None
try:
q: Dict[str, Any] = {"type": "noul", "instructions": instructions}
if true_criteria or false_criteria:
q["criteria"] = {
"true": true_criteria or "Afirmação correta",
"false": false_criteria or "Afirmação incorreta",
}
data = self.decide(state, {key: q}, timeout=timeout)
ans = self._answer(data, key)
if ans and ans.get("noul") is not None:
return float(ans["noul"])
except Exception as e:
logger.debug(f"JEV noul({key}) error: {e}")
return None
def score(
self,
state: str,
instructions: str,
criteria: list,
key: str = "score",
timeout: Optional[float] = None,
) -> Optional[Dict[str, Any]]:
"""
Pergunta score (1..N) → {score, legend, confidence}
Retorna dict ou None
"""
if not self.enabled:
return None
try:
data = self.decide(
state,
{key: {"type": "score", "instructions": instructions, "criteria": criteria}},
timeout=timeout
)
ans = self._answer(data, key)
if ans and ans.get("score") is not None:
return {
"score": ans["score"],
"legend": ans.get("legend"),
"confidence": float(ans.get("confidence") or 0.0)
}
except Exception as e:
logger.debug(f"JEV score({key}) error: {e}")
return None
def choice(
self,
state: str,
instructions: str,
criteria: Dict[str, str],
key: str = "choice",
timeout: Optional[float] = None,
) -> Optional[Dict[str, Any]]:
"""
Pergunta choice (enum) → {choice, probabilities, confidence}
Retorna dict ou None
"""
if not self.enabled:
return None
try:
data = self.decide(
state,
{key: {"type": "choice", "instructions": instructions, "criteria": criteria}},
timeout=timeout
)
ans = self._answer(data, key)
if ans and ans.get("choice"):
return {
"choice": ans["choice"],
"probabilities": ans.get("probabilities", {}),
"confidence": float(ans.get("confidence") or 0.0)
}
except Exception as e:
logger.debug(f"JEV choice({key}) error: {e}")
return None
_jev_singleton: Optional[JevClient] = None
_jev_lock = threading.Lock()
def get_jev_client(api_key: Optional[str] = None) -> JevClient:
global _jev_singleton
with _jev_lock:
if _jev_singleton is None:
_jev_singleton = JevClient(api_key=api_key)
return _jev_singleton
def reset_jev_client():
global _jev_singleton
with _jev_lock:
_jev_singleton = None
|