JairoDanielMT's picture
Upload folder using huggingface_hub
4ef6c2b verified
Raw
History Blame Contribute Delete
1.63 kB
import sqlite3
import time
from typing import List, Dict, Any, Optional
from functools import lru_cache
from src.knowledge.repository import KnowledgeRepository
class KnowledgeEngine:
def __init__(self, db_path: str = "data/knowledge/build/knowledge.db"):
self.repo = KnowledgeRepository(db_path)
self.repo.connect()
@lru_cache(maxsize=1024)
def get_entity_knowledge(self, entity_id: str) -> Optional[List[Dict[str, Any]]]:
"""
Retrieves canonical visual traits for a given entity_id.
Uses LRU Cache to maintain <1ms latency for repeated requests.
"""
cursor = self.repo._conn.cursor()
# We assume entity_id is already resolved to its canonical representation by the intent parser
# Querying the visual traits
cursor.execute("""
SELECT t.trait_id, t.confidence, t.source_count, p.source_uri
FROM entity_visual_traits t
LEFT JOIN trait_provenance p ON t.entity_id = p.entity_id AND t.trait_id = p.trait_id
WHERE t.entity_id = ?
""", (entity_id,))
rows = cursor.fetchall()
if not rows:
return None
traits = []
for row in rows:
traits.append({
"trait_id": row["trait_id"],
"confidence": row["confidence"],
"source_count": row["source_count"],
"provenance": row["source_uri"]
})
return traits
def close(self):
self.repo.close()