File size: 3,291 Bytes
b5b3b28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sqlite3
import os
import datetime
from src.utils import setup_logger
from src.normalizer import normalize_text
from src.similarity import find_best_match

logger = setup_logger("SmartCache")

DB_PATH = "triem_cache.db"

def get_db_connection():
    conn = sqlite3.connect(DB_PATH)
    return conn

def init_db():
    conn = get_db_connection()
    c = conn.cursor()
    c.execute('''
        CREATE TABLE IF NOT EXISTS faq_cache (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            question_eng TEXT UNIQUE,
            answer_sat TEXT,
            backend TEXT,
            similarity_score REAL,
            created_at TEXT
        )
    ''')
    conn.commit()
    conn.close()

def reset_cache():
    """Manual trigger to drop and recreate cache db"""
    logger.info("Manual cache reset triggered.")
    if os.path.exists(DB_PATH):
        conn = get_db_connection()
        c = conn.cursor()
        c.execute('DROP TABLE IF EXISTS faq_cache')
        c.execute('''
            CREATE TABLE faq_cache (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                question_eng TEXT UNIQUE,
                answer_sat TEXT,
                backend TEXT,
                similarity_score REAL,
                created_at TEXT
            )
        ''')
        conn.commit()
        conn.close()
        logger.info("Cache reset successful. Table 'faq_cache' recreated.")
    else:
        logger.info("Cache DB does not exist, initializing fresh.")
        init_db()

def fetch_latest_entries(limit=500):
    init_db()
    conn = get_db_connection()
    c = conn.cursor()
    c.execute('SELECT id, question_eng, answer_sat, backend, similarity_score, created_at FROM faq_cache ORDER BY id DESC LIMIT ?', (limit,))
    entries = c.fetchall()
    conn.close()
    return entries

def check_cache(english_question):
    """
    Checks cache for a match based on the English question.
    Normalizes user question and DB questions.
    Returns best_match_answer, similarity_score, best_match_question, backend
    """
    user_q_norm = normalize_text(english_question)
    
    # Fetch 500 latest entries
    entries = fetch_latest_entries(500)
    
    # Normalize DB questions before matching
    normalized_entries = []
    for entry in entries:
        db_id, db_q, db_a, backend, sim, created_at = entry
        db_q_norm = normalize_text(db_q)
        normalized_entries.append((db_id, db_q_norm, db_a, backend, sim, created_at))
        
    best_score, best_match_answer, best_match_question, best_backend = find_best_match(
        user_q_norm, normalized_entries, threshold=0.80
    )
    
    return best_match_answer, best_score, best_match_question, best_backend

def save_to_cache(question_eng, answer_sat, backend, similarity_score=0.0):
    init_db()
    timestamp = datetime.datetime.now().isoformat()
    conn = get_db_connection()
    c = conn.cursor()
    try:
        c.execute('''
            INSERT OR IGNORE INTO faq_cache (question_eng, answer_sat, backend, similarity_score, created_at)
            VALUES (?, ?, ?, ?, ?)
        ''', (question_eng, answer_sat, backend, similarity_score, timestamp))
        conn.commit()
    except Exception as e:
        logger.error(f"Failed to save to cache: {e}")
    finally:
        conn.close()