44
55from cache .cache import Cache
66from cache .cache_error import CacheError
7- from models .cache_entry import CacheEntry , ConversationData
7+ from models .cache_entry import CacheEntry , AdditionalKwargs
88from models .config import PostgreSQLDatabaseConfiguration
9+ from models .responses import ConversationData
910from log import get_logger
1011from utils .connection_decorator import connection
1112
@@ -37,15 +38,16 @@ class PostgresCache(Cache):
3738
3839 CREATE_CACHE_TABLE = """
3940 CREATE TABLE IF NOT EXISTS cache (
40- user_id text NOT NULL,
41- conversation_id text NOT NULL,
42- created_at timestamp NOT NULL,
43- started_at text,
44- completed_at text,
45- query text,
46- response text,
47- provider text,
48- model text,
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,
4951 PRIMARY KEY(user_id, conversation_id, created_at)
5052 );
5153 """
@@ -66,16 +68,16 @@ class PostgresCache(Cache):
6668 """
6769
6870 SELECT_CONVERSATION_HISTORY_STATEMENT = """
69- SELECT query, response, provider, model, started_at, completed_at
71+ SELECT query, response, provider, model, started_at, completed_at, additional_kwargs
7072 FROM cache
7173 WHERE user_id=%s AND conversation_id=%s
7274 ORDER BY created_at
7375 """
7476
7577 INSERT_CONVERSATION_HISTORY_STATEMENT = """
7678 INSERT INTO cache(user_id, conversation_id, created_at, started_at, completed_at,
77- query, response, provider, model)
78- VALUES (%s, %s, CURRENT_TIMESTAMP, %s, %s, %s, %s, %s, %s)
79+ query, response, provider, model, additional_kwargs )
80+ VALUES (%s, %s, CURRENT_TIMESTAMP, %s, %s, %s, %s, %s, %s, %s )
7981 """
8082
8183 QUERY_CACHE_SIZE = """
@@ -211,13 +213,19 @@ def get(
211213
212214 result = []
213215 for conversation_entry in conversation_entries :
216+ # 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 )
214221 cache_entry = CacheEntry (
215222 query = conversation_entry [0 ],
216223 response = conversation_entry [1 ],
217224 provider = conversation_entry [2 ],
218225 model = conversation_entry [3 ],
219226 started_at = conversation_entry [4 ],
220227 completed_at = conversation_entry [5 ],
228+ additional_kwargs = additional_kwargs_obj ,
221229 )
222230 result .append (cache_entry )
223231
@@ -245,6 +253,10 @@ def insert_or_append(
245253 raise CacheError ("insert_or_append: cache is disconnected" )
246254
247255 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 )
248260 # the whole operation is run in one transaction
249261 with self .connection .cursor () as cursor :
250262 cursor .execute (
@@ -258,6 +270,7 @@ def insert_or_append(
258270 cache_entry .response ,
259271 cache_entry .provider ,
260272 cache_entry .model ,
273+ additional_kwargs_json ,
261274 ),
262275 )
263276
0 commit comments