Spaces:
Sleeping
Sleeping
uploads
Browse files- app/database/qdrant_vault.py +49 -0
app/database/qdrant_vault.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from qdrant_client import QdrantClient
|
| 2 |
+
from qdrant_client.http import models
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
class CognitiveVault:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
# Conectamos al contenedor de Qdrant
|
| 8 |
+
self.client = QdrantClient(
|
| 9 |
+
host=os.getenv("QDRANT_HOST", "localhost"),
|
| 10 |
+
port=int(os.getenv("QDRANT_PORT", 6333))
|
| 11 |
+
)
|
| 12 |
+
self.collection_name = "user_signatures"
|
| 13 |
+
self._ensure_collection()
|
| 14 |
+
|
| 15 |
+
def _ensure_collection(self):
|
| 16 |
+
"""Crea la colección si no existe"""
|
| 17 |
+
collections = self.client.get_collections().collections
|
| 18 |
+
exists = any(c.name == self.collection_name for c in collections)
|
| 19 |
+
|
| 20 |
+
if not exists:
|
| 21 |
+
self.client.recreate_collection(
|
| 22 |
+
collection_name=self.collection_name,
|
| 23 |
+
vectors_config=models.VectorParams(
|
| 24 |
+
size=128, # Tamaño del vector de firma (ajustable)
|
| 25 |
+
distance=models.Distance.COSINE
|
| 26 |
+
)
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
async def save_signature(self, user_id, vector, metadata):
|
| 30 |
+
"""Guarda una nueva firma cognitiva"""
|
| 31 |
+
self.client.upsert(
|
| 32 |
+
collection_name=self.collection_name,
|
| 33 |
+
points=[
|
| 34 |
+
models.PointStruct(
|
| 35 |
+
id=user_id, # Usamos el ID del usuario como punto
|
| 36 |
+
vector=vector,
|
| 37 |
+
payload=metadata
|
| 38 |
+
)
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
async def verify_identity(self, vector):
|
| 43 |
+
"""Busca la firma más parecida (Score de cercanía)"""
|
| 44 |
+
search_result = self.client.search(
|
| 45 |
+
collection_name=self.collection_name,
|
| 46 |
+
query_vector=vector,
|
| 47 |
+
limit=1
|
| 48 |
+
)
|
| 49 |
+
return search_result
|