-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
177 lines (137 loc) · 5.65 KB
/
cache.py
File metadata and controls
177 lines (137 loc) · 5.65 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Модуль кеширования для RAG ассистента.
Использует SQLite для хранения пар вопрос-ответ с временными метками.
"""
import sqlite3
import hashlib
import json
from datetime import datetime
from typing import Optional, Dict, Any
import os
class RAGCache:
"""Кеш для хранения результатов RAG запросов."""
def __init__(self, db_path: str = "rag_cache.db"):
"""
Инициализация кеша.
Args:
db_path: путь к файлу базы данных SQLite
"""
self.db_path = db_path
self._init_db()
def _init_db(self):
"""Создание таблицы кеша, если она не существует."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS cache (
query_hash TEXT PRIMARY KEY,
query TEXT NOT NULL,
answer TEXT NOT NULL,
context TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
conn.close()
def _get_query_hash(self, query: str) -> str:
"""
Вычисление хеша запроса для использования как ключ кеша.
Args:
query: текст запроса
Returns:
SHA-256 хеш запроса
"""
# Нормализация запроса: lowercase и удаление лишних пробелов
normalized_query = " ".join(query.lower().strip().split())
return hashlib.sha256(normalized_query.encode()).hexdigest()
def get(self, query: str) -> Optional[Dict[str, Any]]:
"""
Получение ответа из кеша.
Args:
query: текст запроса
Returns:
Словарь с ответом и метаданными, или None если не найдено
"""
query_hash = self._get_query_hash(query)
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
SELECT query, answer, context, created_at
FROM cache
WHERE query_hash = ?
""", (query_hash,))
result = cursor.fetchone()
conn.close()
if result:
return {
"query": result[0],
"answer": result[1],
"context": json.loads(result[2]) if result[2] else None,
"created_at": result[3],
"from_cache": True
}
return None
def set(self, query: str, answer: str, context: list = None):
"""
Сохранение ответа в кеш.
Args:
query: текст запроса
answer: текст ответа
context: список документов, использованных как контекст
"""
query_hash = self._get_query_hash(query)
context_json = json.dumps(context) if context else None
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Используем INSERT OR REPLACE для обновления существующих записей
cursor.execute("""
INSERT OR REPLACE INTO cache (query_hash, query, answer, context, created_at)
VALUES (?, ?, ?, ?, ?)
""", (query_hash, query, answer, context_json, datetime.now().isoformat()))
conn.commit()
conn.close()
def clear(self):
"""Очистка всего кеша."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("DELETE FROM cache")
conn.commit()
conn.close()
def get_stats(self) -> Dict[str, Any]:
"""
Получение статистики кеша.
Returns:
Словарь со статистикой
"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM cache")
count = cursor.fetchone()[0]
cursor.execute("SELECT MIN(created_at), MAX(created_at) FROM cache")
dates = cursor.fetchone()
conn.close()
return {
"total_entries": count,
"oldest_entry": dates[0] if dates[0] else None,
"newest_entry": dates[1] if dates[1] else None,
"db_size_mb": os.path.getsize(self.db_path) / (1024 * 1024) if os.path.exists(self.db_path) else 0
}
if __name__ == "__main__":
# Тестирование кеша
cache = RAGCache("test_cache.db")
# Сохранение
cache.set(
query="Что такое машинное обучение?",
answer="Машинное обучение - это раздел искусственного интеллекта...",
context=["doc1", "doc2"]
)
# Получение
result = cache.get("Что такое машинное обучение?")
print("Результат из кеша:", result)
# Статистика
stats = cache.get_stats()
print("Статистика кеша:", stats)
# Очистка тестовой БД
import os
if os.path.exists("test_cache.db"):
os.remove("test_cache.db")