11"""PostgreSQL cache implementation."""
22
3+ import json
34import psycopg2
45
56from cache .cache import Cache
67from cache .cache_error import CacheError
7- from models .cache_entry import CacheEntry , AdditionalKwargs
8+ from models .cache_entry import CacheEntry
89from models .config import PostgreSQLDatabaseConfiguration
9- from models .responses import ConversationData
10+ from models .responses import ConversationData , ReferencedDocument
1011from log import get_logger
1112from utils .connection_decorator import connection
1213
@@ -19,17 +20,18 @@ class PostgresCache(Cache):
1920 The cache itself lives stored in following table:
2021
2122 ```
22- Column | Type | Nullable |
23- -----------------+--------------------------------+----------+
24- user_id | text | not null |
25- conversation_id | text | not null |
26- created_at | timestamp without time zone | not null |
27- started_at | text | |
28- completed_at | text | |
29- query | text | |
30- response | text | |
31- provider | text | |
32- model | text | |
23+ Column | Type | Nullable |
24+ -----------------------+--------------------------------+----------+
25+ user_id | text | not null |
26+ conversation_id | text | not null |
27+ created_at | timestamp without time zone | not null |
28+ started_at | text | |
29+ completed_at | text | |
30+ query | text | |
31+ response | text | |
32+ provider | text | |
33+ model | text | |
34+ referenced_documents | jsonb | |
3335 Indexes:
3436 "cache_pkey" PRIMARY KEY, btree (user_id, conversation_id, created_at)
3537 "timestamps" btree (created_at)
@@ -38,16 +40,16 @@ class PostgresCache(Cache):
3840
3941 CREATE_CACHE_TABLE = """
4042 CREATE TABLE IF NOT EXISTS cache (
41- user_id text NOT NULL,
42- conversation_id text NOT NULL,
43- created_at timestamp NOT NULL,
44- started_at text,
45- completed_at text,
46- query text,
47- response text,
48- provider text,
49- model text,
50- additional_kwargs jsonb,
43+ user_id text NOT NULL,
44+ conversation_id text NOT NULL,
45+ created_at timestamp NOT NULL,
46+ started_at text,
47+ completed_at text,
48+ query text,
49+ response text,
50+ provider text,
51+ model text,
52+ referenced_documents jsonb,
5153 PRIMARY KEY(user_id, conversation_id, created_at)
5254 );
5355 """
@@ -68,15 +70,15 @@ class PostgresCache(Cache):
6870 """
6971
7072 SELECT_CONVERSATION_HISTORY_STATEMENT = """
71- SELECT query, response, provider, model, started_at, completed_at, additional_kwargs
73+ SELECT query, response, provider, model, started_at, completed_at, referenced_documents
7274 FROM cache
7375 WHERE user_id=%s AND conversation_id=%s
7476 ORDER BY created_at
7577 """
7678
7779 INSERT_CONVERSATION_HISTORY_STATEMENT = """
7880 INSERT INTO cache(user_id, conversation_id, created_at, started_at, completed_at,
79- query, response, provider, model, additional_kwargs )
81+ query, response, provider, model, referenced_documents )
8082 VALUES (%s, %s, CURRENT_TIMESTAMP, %s, %s, %s, %s, %s, %s, %s)
8183 """
8284
@@ -214,18 +216,18 @@ def get(
214216 result = []
215217 for conversation_entry in conversation_entries :
216218 # Parse it back into an LLMResponse object
217- additional_kwargs_data = conversation_entry [6 ]
218- additional_kwargs_obj = None
219- if additional_kwargs_data :
220- additional_kwargs_obj = AdditionalKwargs .model_validate (additional_kwargs_data )
219+ docs_data = conversation_entry [6 ]
220+ docs_obj = None
221+ if docs_data :
222+ docs_obj = [ ReferencedDocument .model_validate (doc ) for doc in docs_data ]
221223 cache_entry = CacheEntry (
222224 query = conversation_entry [0 ],
223225 response = conversation_entry [1 ],
224226 provider = conversation_entry [2 ],
225227 model = conversation_entry [3 ],
226228 started_at = conversation_entry [4 ],
227229 completed_at = conversation_entry [5 ],
228- additional_kwargs = additional_kwargs_obj ,
230+ referenced_documents = docs_obj ,
229231 )
230232 result .append (cache_entry )
231233
@@ -253,10 +255,11 @@ def insert_or_append(
253255 raise CacheError ("insert_or_append: cache is disconnected" )
254256
255257 try :
256- additional_kwargs_json = None
257- if cache_entry .additional_kwargs :
258- # Use exclude_none=True to keep JSON clean
259- additional_kwargs_json = cache_entry .additional_kwargs .model_dump_json (exclude_none = True )
258+ referenced_documents_json = None
259+ if cache_entry .referenced_documents :
260+ docs_as_dicts = [doc .model_dump (mode = 'json' ) for doc in cache_entry .referenced_documents ]
261+ referenced_documents_json = json .dumps (docs_as_dicts )
262+
260263 # the whole operation is run in one transaction
261264 with self .connection .cursor () as cursor :
262265 cursor .execute (
@@ -270,7 +273,7 @@ def insert_or_append(
270273 cache_entry .response ,
271274 cache_entry .provider ,
272275 cache_entry .model ,
273- additional_kwargs_json ,
276+ referenced_documents_json ,
274277 ),
275278 )
276279
0 commit comments