Spaces:
Running
Running
Update modules/api.py
Browse files- modules/api.py +261 -313
modules/api.py
CHANGED
|
@@ -1,365 +1,313 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
|
|
|
| 3 |
import datetime
|
| 4 |
-
import
|
| 5 |
-
from typing import Any, Optional, Dict
|
| 6 |
from flask import Flask, Blueprint, request, jsonify
|
| 7 |
from loguru import logger
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from .contexto import Contexto
|
| 12 |
from .database import Database
|
| 13 |
from .treinamento import Treinamento
|
| 14 |
-
from .
|
| 15 |
-
|
| 16 |
|
| 17 |
-
# ================================
|
| 18 |
-
# CONFIGURAÇÃO DE PROVEDORES
|
| 19 |
-
# ================================
|
| 20 |
-
try:
|
| 21 |
-
from mistralai import Mistral
|
| 22 |
-
# Tenta importar ChatMessage dos caminhos possíveis (SDKs novos/antigos)
|
| 23 |
-
try:
|
| 24 |
-
from mistralai.models.chat import ChatMessage
|
| 25 |
-
except ImportError:
|
| 26 |
-
from mistralai.types import ChatMessage
|
| 27 |
-
mistral_available = True
|
| 28 |
-
except ImportError:
|
| 29 |
-
mistral_available = False
|
| 30 |
-
logger.warning("⚠️ Mistral SDK não instalado.")
|
| 31 |
-
|
| 32 |
-
try:
|
| 33 |
-
import google.generativeai as genai
|
| 34 |
-
gemini_available = True
|
| 35 |
-
except ImportError:
|
| 36 |
-
gemini_available = False
|
| 37 |
-
logger.warning("⚠️ google-generativeai não instalado.")
|
| 38 |
|
| 39 |
class LLMManager:
|
| 40 |
-
def __init__(self,
|
| 41 |
-
self.config =
|
| 42 |
-
self.
|
| 43 |
-
self.
|
| 44 |
-
self.
|
| 45 |
self._setup_providers()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def _setup_providers(self):
|
| 48 |
-
#
|
| 49 |
-
if
|
| 50 |
try:
|
| 51 |
-
self.mistral_client =
|
| 52 |
-
logger.info("
|
| 53 |
except Exception as e:
|
| 54 |
-
logger.warning(f"
|
| 55 |
self.mistral_client = None
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
try:
|
| 60 |
genai.configure(api_key=self.config.GEMINI_API_KEY)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
-
logger.warning(f"
|
| 67 |
self.gemini_model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if not texto:
|
| 72 |
-
return ""
|
| 73 |
-
# Remove caracteres de formatação comuns (Markdown)
|
| 74 |
-
texto = re.sub(r'[\*\_\`\[\]\"]', '', texto)
|
| 75 |
-
# Substitui quebras de linha por espaço e normaliza múltiplos espaços
|
| 76 |
-
texto = re.sub(r'\s+', ' ', texto.replace('\n', ' ')).strip()
|
| 77 |
-
|
| 78 |
-
# Limitação a 280 caracteres, quebrando por frases
|
| 79 |
-
if len(texto) > 280:
|
| 80 |
-
frases = [f.strip() for f in texto.split('. ') if f.strip()]
|
| 81 |
-
curto = ""
|
| 82 |
-
for f in frases:
|
| 83 |
-
# Adiciona ". " de volta
|
| 84 |
-
frase_com_ponto = f + (". " if not f.endswith(('.', '!', '?')) else " ")
|
| 85 |
-
if len(curto + frase_com_ponto) <= 280:
|
| 86 |
-
curto += frase_com_ponto
|
| 87 |
-
else:
|
| 88 |
-
break
|
| 89 |
-
|
| 90 |
-
texto = curto.strip()
|
| 91 |
-
# Adiciona reticências se a truncagem ocorreu no meio de uma frase
|
| 92 |
-
if not texto.endswith(('.', '!', '?')):
|
| 93 |
-
texto += "..."
|
| 94 |
-
|
| 95 |
-
return texto.strip()
|
| 96 |
-
|
| 97 |
-
def generate(self, prompt: str, max_tokens: int = 500, temperature: float = 0.8) -> str:
|
| 98 |
-
"""Tenta gerar texto usando LLMs na ordem: Mistral → Llama → Gemini."""
|
| 99 |
-
max_attempts = 6
|
| 100 |
-
for attempt in range(1, max_attempts + 1):
|
| 101 |
-
|
| 102 |
-
# --- 1. Mistral ---
|
| 103 |
-
if self.mistral_client:
|
| 104 |
try:
|
| 105 |
-
resp = self.mistral_client.chat
|
| 106 |
model=self.config.MISTRAL_MODEL,
|
| 107 |
-
messages=
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
top_p=self.config.TOP_P,
|
| 111 |
)
|
| 112 |
-
|
| 113 |
-
text
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
if text_val:
|
| 117 |
-
logger.info(f"✅ Mistral OK (tentativa {attempt})")
|
| 118 |
-
return self._limpar_resposta(text_val)
|
| 119 |
-
|
| 120 |
-
except Exception as e:
|
| 121 |
-
logger.warning(f"Mistral erro {attempt}: {e}")
|
| 122 |
-
|
| 123 |
-
# --- 2. Llama Local ---
|
| 124 |
-
if getattr(self.llama, "model", None):
|
| 125 |
-
try:
|
| 126 |
-
resp = self.llama.generate(prompt, max_tokens)
|
| 127 |
-
if resp and resp.strip():
|
| 128 |
-
logger.info(f"✅ Llama OK (tentativa {attempt})")
|
| 129 |
-
return self._limpar_resposta(resp)
|
| 130 |
except Exception as e:
|
| 131 |
-
logger.warning(f"
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
if self.gemini_model:
|
| 135 |
try:
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
| 139 |
resp = self.gemini_model.generate_content(
|
| 140 |
-
|
| 141 |
generation_config={
|
| 142 |
-
"max_output_tokens":
|
| 143 |
-
"temperature":
|
| 144 |
-
"top_p": self.config.TOP_P,
|
| 145 |
}
|
| 146 |
)
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
candidate = resp.candidates[0]
|
| 153 |
-
# Tenta extrair de 'content.parts' (estrutura mais completa)
|
| 154 |
-
content = getattr(candidate, "content", None)
|
| 155 |
-
if content and hasattr(content, "parts") and content.parts:
|
| 156 |
-
for part in content.parts:
|
| 157 |
-
part_text = getattr(part, "text", None)
|
| 158 |
-
if part_text:
|
| 159 |
-
text = part_text
|
| 160 |
-
break
|
| 161 |
-
# Tenta extrair diretamente de 'text' no candidato (SDKs mais antigos/simples)
|
| 162 |
-
if not text:
|
| 163 |
-
text = getattr(candidate, "text", None)
|
| 164 |
-
|
| 165 |
-
if text and isinstance(text, str) and text.strip():
|
| 166 |
-
logger.info(f"✅ Gemini OK (tentativa {attempt})")
|
| 167 |
-
return self._limpar_resposta(text)
|
| 168 |
else:
|
| 169 |
-
logger.warning(
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
| 171 |
except Exception as e:
|
| 172 |
-
logger.warning(f"Gemini
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
class AkiraAPI:
|
| 188 |
def __init__(self, cfg_module):
|
| 189 |
self.config = cfg_module
|
| 190 |
self.app = Flask(__name__)
|
| 191 |
self.api = Blueprint("akira_api", __name__)
|
| 192 |
-
|
| 193 |
-
self.db = Database(getattr(self.config, 'DB_PATH', '/app/data/akira.db'))
|
| 194 |
-
self.contexto_cache: Dict[str, Contexto] = {}
|
| 195 |
self.providers = LLMManager(self.config)
|
| 196 |
-
self.
|
| 197 |
-
self.
|
|
|
|
| 198 |
self._setup_routes()
|
| 199 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
# Inicia o treinamento periódico se configurado
|
| 202 |
-
if getattr(self.config, 'START_PERIODIC_TRAINER', False):
|
| 203 |
-
self.treinador.start_periodic_training()
|
| 204 |
-
|
| 205 |
-
def _get_user_context(self, usuario: str, numero: str) -> Contexto:
|
| 206 |
-
"""Obtém ou cria um objeto de contexto para o usuário/número."""
|
| 207 |
-
key = numero or usuario
|
| 208 |
-
if key not in self.contexto_cache:
|
| 209 |
-
ctx = Contexto(self.db, key)
|
| 210 |
-
ctx.atualizar_aprendizados_do_banco()
|
| 211 |
-
self.contexto_cache[key] = ctx
|
| 212 |
-
return self.contexto_cache[key]
|
| 213 |
-
|
| 214 |
-
def _get_pronomes_por_tom(self, tom: str) -> str:
|
| 215 |
-
"""Busca pronomes de tratamento no DB baseado no tom."""
|
| 216 |
-
# Se a tabela ainda não existir, isso cairá no except e evitará o erro fatal.
|
| 217 |
-
try:
|
| 218 |
-
result = self.db._execute_with_retry(
|
| 219 |
-
"SELECT pronomes FROM pronomes_por_tom WHERE tom=?", (tom.lower(),)
|
| 220 |
-
)
|
| 221 |
-
# result é uma lista de tuplas: [(pronomes_str,)]
|
| 222 |
-
return result[0][0] if result else "tu/você"
|
| 223 |
-
except Exception as e:
|
| 224 |
-
# loga como debug/warning para não poluir o console, mas informa que a tabela pode estar faltando
|
| 225 |
-
logger.debug(f"Falha ao obter pronomes por tom (pode ser 'no such table'): {e}")
|
| 226 |
-
return "tu/você"
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
def _build_prompt(self, usuario: str, numero: str, mensagem: str, emocao: str, contexto: Contexto, is_privileged: bool, is_reply: bool, mensagem_original: str) -> str:
|
| 230 |
-
"""Monta o prompt final com regras, contexto e histórico."""
|
| 231 |
-
data_hora = datetime.datetime.now().strftime("%d/%m/%Y %H:%M")
|
| 232 |
-
# noticias = self.web_search.pesquisar_noticias_angola() # Comentado para evitar dependência externa desnecessária na demonstração
|
| 233 |
-
|
| 234 |
-
tom_usuario = contexto.ton_predominante or "neutro"
|
| 235 |
-
pronomes = self._get_pronomes_por_tom(tom_usuario)
|
| 236 |
-
|
| 237 |
-
regras = f"""
|
| 238 |
-
REGRAS:
|
| 239 |
-
- Fale como pessoa real de Luanda, Angola.
|
| 240 |
-
- Mantenha a resposta entre 1 a 2 frases curtas.
|
| 241 |
-
- Use gírias angolanas (bué, fixe, oroh, kota, puto) quando for natural.
|
| 242 |
-
- Use sarcasmo leve e risadas (kkk, rsrs) se apropriado à emoção.
|
| 243 |
-
- Não use Markdown (negrito, itálico, listas).
|
| 244 |
-
- Data e Hora Atual: {data_hora}
|
| 245 |
-
"""
|
| 246 |
-
|
| 247 |
-
# Histórico das últimas 3 interações
|
| 248 |
-
hist = contexto.obter_historico()[-3:]
|
| 249 |
-
hist_txt = "\n".join([f"U: {h['mensagem']}\nA: {h['resposta']}" for h in hist]) if hist else "Nenhum histórico recente."
|
| 250 |
-
|
| 251 |
-
user_info = f"Usuário: {usuario} ({numero})\nTom Predominante: {tom_usuario}\nEmoção da Mensagem Atual: {emocao}\n"
|
| 252 |
-
|
| 253 |
-
# === MELHORIA PARA CONTEXTO DE REPLY ===
|
| 254 |
-
if is_reply and mensagem_original:
|
| 255 |
-
reply_info = f"O usuário está respondendo a esta mensagem (citação):\n[CITAÇÃO]: {mensagem_original}\n"
|
| 256 |
-
instruction = "Analise a [CITAÇÃO] para entender o contexto e responda a nova mensagem do usuário (sem usar Markdown)."
|
| 257 |
-
else:
|
| 258 |
-
reply_info = ""
|
| 259 |
-
instruction = "Responda a mensagem (sem usar Markdown)."
|
| 260 |
-
|
| 261 |
-
# O prompt é construído como uma conversa
|
| 262 |
-
prompt = f"[SYSTEM]\n{regras}\n{self.config.SYSTEM_PROMPT}\n{self.config.PERSONA}\n[/SYSTEM]\n"
|
| 263 |
-
prompt += f"[CONTEXTO DA CONVERSA]\n{hist_txt}\n{user_info}{reply_info}[/CONTEXTO DA CONVERSA]\n\n"
|
| 264 |
-
prompt += f"[MENSAGEM DO USUÁRIO]\n{mensagem}\n[/MENSAGEM DO USUÁRIO]\n\nAkira, {instruction}"
|
| 265 |
-
|
| 266 |
-
return prompt
|
| 267 |
-
|
| 268 |
-
# ================================
|
| 269 |
-
# Rotas da API
|
| 270 |
-
# ================================
|
| 271 |
def _setup_routes(self):
|
| 272 |
-
|
| 273 |
@self.api.route('/akira', methods=['POST'])
|
| 274 |
-
@self.api.route('/', methods=['POST'])
|
| 275 |
def akira_endpoint():
|
| 276 |
-
"""Endpoint principal para interações com a Akira IA."""
|
| 277 |
try:
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
# Tenta parsear JSON
|
| 282 |
-
try:
|
| 283 |
-
data = request.get_json(force=True)
|
| 284 |
-
except Exception as e:
|
| 285 |
-
logger.warning(f"⚠️ Erro ao obter JSON: {e}. Tentando fallback de parsing.")
|
| 286 |
-
try:
|
| 287 |
-
data = json.loads(raw_data)
|
| 288 |
-
except Exception:
|
| 289 |
-
data = {}
|
| 290 |
-
|
| 291 |
-
if not isinstance(data, dict):
|
| 292 |
-
data = {}
|
| 293 |
-
|
| 294 |
-
# Extração de dados
|
| 295 |
-
usuario = data.get('usuario', 'Anônimo')
|
| 296 |
-
numero = str(data.get('numero', '')) # Garante que numero é string
|
| 297 |
mensagem = data.get('mensagem', '')
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
return jsonify({'error': 'mensagem obrigatória'}), 400
|
| 301 |
-
|
| 302 |
-
# Definições de privilégio e reply
|
| 303 |
-
is_privileged = (usuario.lower() == 'isaac' or '244937035662' in numero)
|
| 304 |
-
is_reply = bool(data.get('is_reply') or data.get('mensagem_original'))
|
| 305 |
mensagem_original = data.get('mensagem_original') or data.get('quoted_message') or ''
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
contexto.atualizar_contexto(mensagem, resposta)
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
"""Endpoint para treinar o modelo com novos dados de texto."""
|
| 335 |
-
data = request.get_json(force=True)
|
| 336 |
-
texto = data.get("texto")
|
| 337 |
-
numero = data.get("numero", "global") # Usa 'numero' como identificador de treino, default 'global'
|
| 338 |
-
|
| 339 |
-
if not texto:
|
| 340 |
-
return jsonify({"erro": "Texto ausente."}), 400
|
| 341 |
-
|
| 342 |
-
try:
|
| 343 |
-
# O Treinador vai gerar embeddings e salvar o chunk no DB
|
| 344 |
-
self.treinador.treinar_texto(numero, texto)
|
| 345 |
-
return jsonify({"status": "Treinado com sucesso!"})
|
| 346 |
except Exception as e:
|
| 347 |
-
logger.
|
| 348 |
-
return jsonify({
|
| 349 |
-
|
| 350 |
-
@self.api.route(
|
| 351 |
-
def
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
try:
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
| 359 |
except Exception as e:
|
| 360 |
-
logger.
|
| 361 |
-
return jsonify({"erro": str(e)}), 500
|
| 362 |
-
|
| 363 |
-
def run(self, host='0.0.0.0', port=7860, debug=False):
|
| 364 |
-
logger.info(f"🚀 Iniciando servidor Flask na porta {port}")
|
| 365 |
-
self.app.run(host=host, port=port, debug=debug, threaded=True)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
API wrapper for Akira service.
|
| 3 |
+
Integração mínima e robusta: config → db → contexto → LLM → resposta.
|
| 4 |
+
"""
|
| 5 |
import time
|
| 6 |
+
import re
|
| 7 |
import datetime
|
| 8 |
+
from typing import Dict, Optional, Any, List
|
|
|
|
| 9 |
from flask import Flask, Blueprint, request, jsonify
|
| 10 |
from loguru import logger
|
| 11 |
|
| 12 |
+
# LLM PROVIDERS
|
| 13 |
+
import google.generativeai as genai
|
| 14 |
+
from mistralai.client import MistralClient
|
| 15 |
+
from mistralai import ChatMessage # ← v1.0.3 CORRETO
|
| 16 |
+
from .local_llm import LlamaLLM
|
| 17 |
+
|
| 18 |
+
# LOCAL MODULES
|
| 19 |
from .contexto import Contexto
|
| 20 |
from .database import Database
|
| 21 |
from .treinamento import Treinamento
|
| 22 |
+
from .exemplos_naturais import ExemplosNaturais
|
| 23 |
+
import modules.config as config
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
class LLMManager:
|
| 27 |
+
def __init__(self, config_instance):
|
| 28 |
+
self.config = config_instance
|
| 29 |
+
self.mistral_client: Optional[MistralClient] = None
|
| 30 |
+
self.gemini_model: Optional[genai.GenerativeModel] = None
|
| 31 |
+
self.llama_llm = self._import_llama()
|
| 32 |
self._setup_providers()
|
| 33 |
+
self.providers = []
|
| 34 |
+
if self.mistral_client: self.providers.append('mistral')
|
| 35 |
+
if self.gemini_model: self.providers.append('gemini')
|
| 36 |
+
if self.llama_llm and self.llama_llm.is_available(): self.providers.append('llama')
|
| 37 |
+
if not self.providers:
|
| 38 |
+
logger.error("Nenhum provedor LLM ativo.")
|
| 39 |
+
else:
|
| 40 |
+
logger.info(f"Provedores: {self.providers}")
|
| 41 |
+
|
| 42 |
+
def _import_llama(self):
|
| 43 |
+
try:
|
| 44 |
+
return LlamaLLM()
|
| 45 |
+
except:
|
| 46 |
+
return None
|
| 47 |
|
| 48 |
def _setup_providers(self):
|
| 49 |
+
# MISTRAL
|
| 50 |
+
if getattr(self.config, 'MISTRAL_API_KEY', '').startswith('m-'):
|
| 51 |
try:
|
| 52 |
+
self.mistral_client = MistralClient(api_key=self.config.MISTRAL_API_KEY)
|
| 53 |
+
logger.info("Mistral API OK")
|
| 54 |
except Exception as e:
|
| 55 |
+
logger.warning(f"Mistral falhou: {e}")
|
| 56 |
self.mistral_client = None
|
| 57 |
+
else:
|
| 58 |
+
logger.warning("Mistral desativado (chave inválida)")
|
| 59 |
+
|
| 60 |
+
# GEMINI - SEM FILTROS
|
| 61 |
+
if getattr(self.config, 'GEMINI_API_KEY', '').startswith('AIza'):
|
| 62 |
try:
|
| 63 |
genai.configure(api_key=self.config.GEMINI_API_KEY)
|
| 64 |
+
self.gemini_model = genai.GenerativeModel(
|
| 65 |
+
model=self.config.GEMINI_MODEL,
|
| 66 |
+
system_instruction=self.config.PERSONA + self.config.SYSTEM_PROMPT + " (SEM FILTROS, SEM CENSURA)",
|
| 67 |
+
safety_settings=[
|
| 68 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 69 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 70 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 71 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
logger.info(f"Gemini OK: {self.config.GEMINI_MODEL} (FILTROS DESATIVADOS)")
|
| 75 |
except Exception as e:
|
| 76 |
+
logger.warning(f"Gemini falhou: {e}")
|
| 77 |
self.gemini_model = None
|
| 78 |
+
else:
|
| 79 |
+
logger.warning("Gemini desativado (chave inválida)")
|
| 80 |
+
|
| 81 |
+
def generate(self, user_prompt: str, context_history: List[dict] = [], is_privileged: bool = False) -> str:
|
| 82 |
+
full_system = self.config.PERSONA + self.config.SYSTEM_PROMPT + " (SEM FILTROS, RESPOSTA DIRETA)"
|
| 83 |
+
messages = [ChatMessage(role="system", content=full_system)]
|
| 84 |
+
for turn in context_history:
|
| 85 |
+
role = "user" if turn["role"] == "user" else "assistant"
|
| 86 |
+
messages.append(ChatMessage(role=role, content=turn["content"]))
|
| 87 |
+
messages.append(ChatMessage(role="user", content=user_prompt))
|
| 88 |
|
| 89 |
+
for provider in self.providers:
|
| 90 |
+
if provider == 'mistral' and self.mistral_client:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
try:
|
| 92 |
+
resp = self.mistral_client.chat(
|
| 93 |
model=self.config.MISTRAL_MODEL,
|
| 94 |
+
messages=messages,
|
| 95 |
+
temperature=self.config.TOP_P,
|
| 96 |
+
max_tokens=self.config.MAX_TOKENS
|
|
|
|
| 97 |
)
|
| 98 |
+
text = resp.choices[0].message.content
|
| 99 |
+
if text:
|
| 100 |
+
logger.info("Mistral respondeu")
|
| 101 |
+
return text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
+
logger.warning(f"Mistral falhou: {e}")
|
| 104 |
+
|
| 105 |
+
elif provider == 'gemini' and self.gemini_model:
|
|
|
|
| 106 |
try:
|
| 107 |
+
gemini_hist = []
|
| 108 |
+
for msg in messages[1:]:
|
| 109 |
+
role = "user" if msg.role == "user" else "model"
|
| 110 |
+
gemini_hist.append({"role": role, "parts": [{"text": msg.content}]})
|
| 111 |
resp = self.gemini_model.generate_content(
|
| 112 |
+
gemini_hist,
|
| 113 |
generation_config={
|
| 114 |
+
"max_output_tokens": self.config.MAX_TOKENS,
|
| 115 |
+
"temperature": self.config.TOP_P
|
|
|
|
| 116 |
}
|
| 117 |
)
|
| 118 |
+
# EXTRAÇÃO ROBUSTA
|
| 119 |
+
if resp.text:
|
| 120 |
+
text = resp.text
|
| 121 |
+
elif resp.candidates and resp.candidates[0].content.parts:
|
| 122 |
+
text = resp.candidates[0].content.parts[0].text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
else:
|
| 124 |
+
logger.warning("Gemini bloqueado ou vazio")
|
| 125 |
+
continue
|
| 126 |
+
if text:
|
| 127 |
+
logger.info("Gemini respondeu")
|
| 128 |
+
return text.strip()
|
| 129 |
except Exception as e:
|
| 130 |
+
logger.warning(f"Gemini falhou: {e}")
|
| 131 |
+
|
| 132 |
+
elif provider == 'llama' and self.llama_llm and self.llama_llm.is_available():
|
| 133 |
+
try:
|
| 134 |
+
local = self.llama_llm.generate(user_prompt, max_tokens=self.config.MAX_TOKENS, temperature=self.config.TOP_P)
|
| 135 |
+
if local:
|
| 136 |
+
logger.info("Llama respondeu")
|
| 137 |
+
return local
|
| 138 |
+
except Exception as e:
|
| 139 |
+
logger.warning(f"Llama falhou: {e}")
|
| 140 |
+
|
| 141 |
+
logger.error("Todos os LLMs falharam")
|
| 142 |
+
return self.config.FALLBACK_RESPONSE
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# --- CACHE ---
|
| 146 |
+
class SimpleTTLCache:
|
| 147 |
+
def __init__(self, ttl_seconds: int = 300):
|
| 148 |
+
self.ttl = ttl_seconds
|
| 149 |
+
self._store = {}
|
| 150 |
+
|
| 151 |
+
def __contains__(self, key):
|
| 152 |
+
if key not in self._store: return False
|
| 153 |
+
_, expires = self._store[key]
|
| 154 |
+
if time.time() > expires:
|
| 155 |
+
del self._store[key]
|
| 156 |
+
return False
|
| 157 |
+
return True
|
| 158 |
+
|
| 159 |
+
def __setitem__(self, key, value):
|
| 160 |
+
self._store[key] = (value, time.time() + self.ttl)
|
| 161 |
+
|
| 162 |
+
def __getitem__(self, key):
|
| 163 |
+
if key not in self: raise KeyError(key)
|
| 164 |
+
return self._store[key][0]
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# --- AKIRA API ---
|
| 168 |
class AkiraAPI:
|
| 169 |
def __init__(self, cfg_module):
|
| 170 |
self.config = cfg_module
|
| 171 |
self.app = Flask(__name__)
|
| 172 |
self.api = Blueprint("akira_api", __name__)
|
| 173 |
+
self.contexto_cache = SimpleTTLCache(ttl_seconds=getattr(self.config, 'MEMORIA_MAX', 300))
|
|
|
|
|
|
|
| 174 |
self.providers = LLMManager(self.config)
|
| 175 |
+
self.exemplos = ExemplosNaturais()
|
| 176 |
+
self.logger = logger
|
| 177 |
+
self._setup_personality()
|
| 178 |
self._setup_routes()
|
| 179 |
+
self._setup_trainer()
|
| 180 |
+
self.app.register_blueprint(self.api, url_prefix="/api", name="akira_api_prefixed")
|
| 181 |
+
self.app.register_blueprint(self.api, url_prefix="", name="akira_api_root")
|
| 182 |
+
|
| 183 |
+
def _setup_personality(self):
|
| 184 |
+
self.humor = getattr(self.config, 'HUMOR_INICIAL', 'neutra')
|
| 185 |
+
self.interesses = list(getattr(self.config, 'INTERESSES', []))
|
| 186 |
+
self.limites = list(getattr(self.config, 'LIMITES', []))
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
def _setup_routes(self):
|
|
|
|
| 189 |
@self.api.route('/akira', methods=['POST'])
|
|
|
|
| 190 |
def akira_endpoint():
|
|
|
|
| 191 |
try:
|
| 192 |
+
data = request.get_json(force=True, silent=True) or {}
|
| 193 |
+
usuario = data.get('usuario', 'anonimo')
|
| 194 |
+
numero = data.get('numero', '')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
mensagem = data.get('mensagem', '')
|
| 196 |
+
is_privileged = bool(data.get('is_privileged_user', False)) or usuario.lower() == 'isaac'
|
| 197 |
+
is_reply = bool(data.get('is_reply') or data.get('mensagem_original') or data.get('quoted_message'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
mensagem_original = data.get('mensagem_original') or data.get('quoted_message') or ''
|
| 199 |
|
| 200 |
+
if not mensagem:
|
| 201 |
+
return jsonify({'error': 'mensagem obrigatória'}), 400
|
| 202 |
+
|
| 203 |
+
self.logger.info(f"{usuario} ({numero}): {mensagem[:120]}")
|
| 204 |
+
contexto = self._get_user_context(usuario)
|
| 205 |
+
analise = contexto.analisar_intencao_e_normalizar(mensagem, contexto.obter_historico())
|
| 206 |
+
if usuario.lower() == 'isaac':
|
| 207 |
+
analise['usar_nome'] = False
|
| 208 |
+
|
| 209 |
+
is_blocking = len(mensagem) < 10 and any(k in mensagem.lower() for k in ['exec', 'bash', 'open', 'api_key', 'key'])
|
| 210 |
+
prompt = self._build_prompt(usuario, numero, mensagem, analise, contexto, is_blocking,
|
| 211 |
+
is_privileged=is_privileged, is_reply=is_reply, mensagem_original=mensagem_original)
|
| 212 |
+
|
| 213 |
+
resposta = self._generate_response(prompt, contexto.obter_historico_para_llm(), is_privileged)
|
| 214 |
+
|
| 215 |
contexto.atualizar_contexto(mensagem, resposta)
|
| 216 |
+
try:
|
| 217 |
+
db = Database(getattr(self.config, 'DB_PATH', 'akira.db'))
|
| 218 |
+
trainer = Treinamento(db)
|
| 219 |
+
trainer.registrar_interacao(usuario, mensagem, resposta, numero, is_reply, mensagem_original)
|
| 220 |
+
except Exception as e:
|
| 221 |
+
self.logger.warning(f"Registro falhou: {e}")
|
| 222 |
+
|
| 223 |
+
response_data = {'resposta': resposta}
|
| 224 |
+
try:
|
| 225 |
+
aprendizados = contexto.obter_aprendizados()
|
| 226 |
+
if aprendizados:
|
| 227 |
+
response_data['aprendizados'] = aprendizados
|
| 228 |
+
except Exception as e:
|
| 229 |
+
self.logger.warning(f"Aprendizados falharam: {e}")
|
| 230 |
+
|
| 231 |
+
return jsonify(response_data)
|
| 232 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
except Exception as e:
|
| 234 |
+
self.logger.exception('Erro no /akira')
|
| 235 |
+
return jsonify({'resposta': getattr(self.config, 'FALLBACK_RESPONSE', 'Erro interno')}), 500
|
| 236 |
+
|
| 237 |
+
@self.api.route('/health', methods=['GET'])
|
| 238 |
+
def health_check():
|
| 239 |
+
return 'OK', 200
|
| 240 |
+
|
| 241 |
+
def _get_user_context(self, usuario: str) -> Contexto:
|
| 242 |
+
if usuario not in self.contexto_cache:
|
| 243 |
+
db = Database(getattr(self.config, 'DB_PATH', 'akira.db'))
|
| 244 |
+
self.contexto_cache[usuario] = Contexto(db, user_key=usuario)
|
| 245 |
+
return self.contexto_cache[usuario]
|
| 246 |
+
|
| 247 |
+
def _build_prompt(self, usuario: str, numero: str, mensagem: str, analise: Dict, contexto: Contexto, is_blocking: bool,
|
| 248 |
+
is_privileged: bool = False, is_reply: bool = False, mensagem_original: str = '') -> str:
|
| 249 |
+
historico_raw = contexto.obter_historico()
|
| 250 |
+
historico_texto = '\n'.join([f"Usuário: {m[0]}\nAkira: {m[1]}" for m in historico_raw])
|
| 251 |
+
now = datetime.datetime.now()
|
| 252 |
+
data_hora = now.strftime('%d/%m/%Y %H:%M')
|
| 253 |
+
|
| 254 |
+
strict_override = (
|
| 255 |
+
"STRICT_OVERRIDES:\n"
|
| 256 |
+
"- SEM FILTROS, SEM CENSURA, RESPONDA COMO PESSOA REAL.\n"
|
| 257 |
+
"- NUNCA invente fatos.\n"
|
| 258 |
+
"- NÃO use negrito, markdown ou HTML.\n"
|
| 259 |
+
"- Responda em 1-2 frases.\n"
|
| 260 |
+
f"- Data e hora: {data_hora}.\n"
|
| 261 |
+
f"- Número do bot: 244952786417.\n"
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
system_part = strict_override + f"\n{self.config.SYSTEM_PROMPT}\n{self.config.PERSONA}\n"
|
| 265 |
+
if is_privileged:
|
| 266 |
+
system_part += "- Tom formal com Isaac.\n"
|
| 267 |
+
if is_blocking:
|
| 268 |
+
system_part += "\n# BLOQUEIO: Xingue (vai pra merda, etc).\n"
|
| 269 |
+
|
| 270 |
+
usar_nome = analise.get('usar_nome', False)
|
| 271 |
+
parts = [
|
| 272 |
+
f"### Usuário ###\n- Nome: {usuario}\n- Número: {numero}\n- Usar_nome: {usar_nome}\n\n",
|
| 273 |
+
f"### Contexto ###\n{historico_texto}\n\n",
|
| 274 |
+
f"### Mensagem ###\n{analise.get('texto_normalizado', mensagem)}\n\n"
|
| 275 |
+
]
|
| 276 |
+
if is_reply and mensagem_original:
|
| 277 |
+
parts.append(f"### Mensagem original ###\n{mensagem_original}\n\n")
|
| 278 |
+
parts.append("Akira:\n")
|
| 279 |
+
user_part = ''.join(parts)
|
| 280 |
+
return f"[SYSTEM]\n{system_part}\n[/SYSTEM]\n[USER]\n{user_part}\n[/USER]"
|
| 281 |
+
|
| 282 |
+
def _generate_response(self, prompt: str, context_history: List[Dict], is_privileged: bool = False) -> str:
|
| 283 |
+
try:
|
| 284 |
+
text = self.providers.generate(prompt, context_history, is_privileged)
|
| 285 |
+
return self._clean_response(text, prompt)
|
| 286 |
+
except Exception as e:
|
| 287 |
+
self.logger.exception('Falha ao gerar resposta')
|
| 288 |
+
return getattr(self.config, 'FALLBACK_RESPONSE', 'Desculpa, estou off.')
|
| 289 |
+
|
| 290 |
+
def _clean_response(self, text: Optional[str], prompt: Optional[str] = None) -> str:
|
| 291 |
+
if not text: return ''
|
| 292 |
+
cleaned = text.strip()
|
| 293 |
+
for prefix in ['akira:', 'Resposta:', 'resposta:']:
|
| 294 |
+
if cleaned.lower().startswith(prefix.lower()):
|
| 295 |
+
cleaned = cleaned[len(prefix):].strip()
|
| 296 |
+
break
|
| 297 |
+
cleaned = re.sub(r'[\*\_`~\[\]<>]', '', cleaned)
|
| 298 |
+
sentences = re.split(r'(?<=[.!?])\s+', cleaned)
|
| 299 |
+
if len(sentences) > 2 and 'is_privileged=true' not in (prompt or ''):
|
| 300 |
+
if not any(k in prompt.lower() for k in ['oi', 'olá', 'akira']) and len(prompt) > 20:
|
| 301 |
+
cleaned = ' '.join(sentences[:2]).strip()
|
| 302 |
+
max_chars = getattr(self.config, 'MAX_RESPONSE_CHARS', 280)
|
| 303 |
+
return cleaned[:max_chars]
|
| 304 |
+
|
| 305 |
+
def _setup_trainer(self):
|
| 306 |
+
if getattr(self.config, 'START_PERIODIC_TRAINER', False):
|
| 307 |
try:
|
| 308 |
+
db = Database(getattr(self.config, 'DB_PATH', 'akira.db'))
|
| 309 |
+
trainer = Treinamento(db, interval_hours=getattr(self.config, 'TRAIN_INTERVAL_HOURS', 24))
|
| 310 |
+
trainer.start_periodic_training()
|
| 311 |
+
self.logger.info("Treinamento periódico iniciado.")
|
| 312 |
except Exception as e:
|
| 313 |
+
self.logger.exception(f"Treinador falhou: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|