akra35567 commited on
Commit
92c2576
·
1 Parent(s): 8675351

Update modules/database.py

Browse files
Files changed (1) hide show
  1. modules/database.py +51 -6
modules/database.py CHANGED
@@ -1,6 +1,7 @@
1
  """
2
  Banco de dados SQLite para Akira IA.
3
  Gerencia contexto, mensagens, embeddings, gírias, tom e aprendizados detalhados.
 
4
  """
5
 
6
  import sqlite3
@@ -295,11 +296,8 @@ class Database:
295
  (usuario, usuario, limite)
296
  ) or []
297
 
298
- def salvar_embedding(self, numero_usuario: str, mensagem: str, resposta: str, embedding: bytes):
299
- """Salva embedding no banco junto com mensagem e resposta."""
300
  try:
301
- texto = f"{numero_usuario}: {mensagem} | {resposta}"
302
- # converte numpy array para bytes, se necessário
303
  if hasattr(embedding, "tobytes"):
304
  embedding = embedding.tobytes()
305
  self._execute_with_retry(
@@ -310,11 +308,58 @@ class Database:
310
  except Exception as e:
311
  logger.warning(f"Erro ao salvar embedding: {e}")
312
 
313
- # Outros métodos (girias, contexto, tom, aprendizados) permanecem iguais ao original
314
- # Exemplo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
  def registrar_tom_usuario(self, numero_usuario: str, tom_detectado: str, intensidade: float = 0.5, contexto: Optional[str] = None):
316
  self._execute_with_retry(
317
  "INSERT INTO tom_usuario (numero_usuario, tom_detectado, intensidade, contexto) VALUES (?, ?, ?, ?)",
318
  (numero_usuario, tom_detectado, intensidade, contexto),
319
  commit=True
320
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
  Banco de dados SQLite para Akira IA.
3
  Gerencia contexto, mensagens, embeddings, gírias, tom e aprendizados detalhados.
4
+ Versão completa 11/2025.
5
  """
6
 
7
  import sqlite3
 
296
  (usuario, usuario, limite)
297
  ) or []
298
 
299
+ def salvar_embedding(self, texto: str, embedding: bytes):
 
300
  try:
 
 
301
  if hasattr(embedding, "tobytes"):
302
  embedding = embedding.tobytes()
303
  self._execute_with_retry(
 
308
  except Exception as e:
309
  logger.warning(f"Erro ao salvar embedding: {e}")
310
 
311
+ # ================================================================
312
+ # MÉTODOS ADICIONAIS PARA CONTEXTO / APRENDIZADOS / GIRIAS
313
+ # ================================================================
314
+ def recuperar_girias_usuario(self, numero_usuario: str) -> List[Dict[str, Any]]:
315
+ rows = self._execute_with_retry(
316
+ "SELECT giria, significado, contexto, frequencia FROM girias_aprendidas WHERE numero_usuario=?",
317
+ (numero_usuario,)
318
+ ) or []
319
+ return [{'giria': r[0], 'significado': r[1], 'contexto': r[2], 'frequencia': r[3]} for r in rows]
320
+
321
+ def recuperar_aprendizado_detalhado(self, numero_usuario: str) -> Dict[str, str]:
322
+ rows = self._execute_with_retry(
323
+ "SELECT chave, valor FROM aprendizados WHERE numero_usuario=?",
324
+ (numero_usuario,)
325
+ ) or []
326
+ return {r[0]: r[1] for r in rows}
327
+
328
+ def obter_tom_predominante(self, numero_usuario: str) -> Optional[str]:
329
+ rows = self._execute_with_retry(
330
+ "SELECT tom_detectado FROM tom_usuario WHERE numero_usuario=? ORDER BY created_at DESC LIMIT 1",
331
+ (numero_usuario,)
332
+ ) or []
333
+ return rows[0][0] if rows else None
334
+
335
  def registrar_tom_usuario(self, numero_usuario: str, tom_detectado: str, intensidade: float = 0.5, contexto: Optional[str] = None):
336
  self._execute_with_retry(
337
  "INSERT INTO tom_usuario (numero_usuario, tom_detectado, intensidade, contexto) VALUES (?, ?, ?, ?)",
338
  (numero_usuario, tom_detectado, intensidade, contexto),
339
  commit=True
340
  )
341
+
342
+ def salvar_aprendizado_detalhado(self, numero_usuario: str, chave: str, valor: str):
343
+ self._execute_with_retry(
344
+ "INSERT INTO aprendizados (numero_usuario, chave, valor) VALUES (?, ?, ?)",
345
+ (numero_usuario, chave, valor),
346
+ commit=True
347
+ )
348
+
349
+ def salvar_giria_aprendida(self, numero_usuario: str, giria: str, significado: str, contexto: Optional[str] = None):
350
+ existing = self._execute_with_retry(
351
+ "SELECT id, frequencia FROM girias_aprendidas WHERE numero_usuario=? AND giria=?",
352
+ (numero_usuario, giria)
353
+ )
354
+ if existing:
355
+ self._execute_with_retry(
356
+ "UPDATE girias_aprendidas SET frequencia=frequencia+1, updated_at=CURRENT_TIMESTAMP WHERE id=?",
357
+ (existing[0][0],),
358
+ commit=True
359
+ )
360
+ else:
361
+ self._execute_with_retry(
362
+ "INSERT INTO girias_aprendidas (numero_usuario, giria, significado, contexto) VALUES (?, ?, ?, ?)",
363
+ (numero_usuario, giria, significado, contexto),
364
+ commit=True
365
+ )