1
1
import hashlib
2
- from typing import Any , Dict , List , Optional , Tuple , Union
3
2
from datetime import datetime
4
-
3
+ from typing import Any , Dict , List , Optional , Tuple , Union
5
4
from redis import Redis
6
-
7
5
from redisvl .index import SearchIndex
8
6
from redisvl .query import FilterQuery , RangeQuery
9
7
from redisvl .query .filter import Tag , Num
12
10
from redisvl .utils .vectorize import BaseVectorizer , HFTextVectorizer
13
11
14
12
class SessionManager :
15
- def __init__ (self ,
16
- name : str ,
17
- session_id : str ,
18
- user_id : str ,
19
- application_id : str ,
20
- scope : str = 'session' ,
21
- prefix : Optional [str ] = None ,
22
- vectorizer : Optional [BaseVectorizer ] = None ,
23
- distance_threshold : float = 0.3 ,
24
- redis_client : Optional [Redis ] = None ,
25
- preamble : str = ''
26
- ):
13
+ def __init__ (
14
+ self ,
15
+ name : str ,
16
+ session_id : str ,
17
+ user_id : str ,
18
+ application_id : str ,
19
+ scope : str = 'session' ,
20
+ prefix : Optional [str ] = None ,
21
+ vectorizer : Optional [BaseVectorizer ] = None ,
22
+ distance_threshold : float = 0.3 ,
23
+ redis_client : Optional [Redis ] = None ,
24
+ preamble : str = ''
25
+ ):
27
26
""" Initialize session memory with index
28
27
29
28
Session Manager stores the current and previous user text prompts and
@@ -123,7 +122,7 @@ def set_scope(
123
122
""" Set the tag filter to apply to querries based on the desired scope.
124
123
125
124
This new scope persists until another call to set_scope is made, or if
126
- scope specified in calls to conversation_history or fetch_context .
125
+ scope specified in calls to fetch_recent or fetch_relevant .
127
126
128
127
Args:
129
128
session_id str: Id of the specific session to filter to. Default is
@@ -160,7 +159,7 @@ def delete(self) -> None:
160
159
self ._index .delete (drop = True )
161
160
162
161
163
- def fetch_context (
162
+ def fetch_relevant (
164
163
self ,
165
164
prompt : str ,
166
165
as_text : bool = False ,
@@ -184,8 +183,8 @@ def fetch_context(
184
183
as_text bool: Whether to return the prompt:response pairs as text
185
184
or as JSON
186
185
top_k int: The number of previous exchanges to return. Default is 3.
187
- fallback bool: Whether to drop back to conversation history if no
188
- relevant context is found.
186
+ fallback bool: Whether to drop back to recent conversation history
187
+ if no relevant context is found.
189
188
session_id str: Tag to be added to entries to link to a specific
190
189
session.
191
190
user_id str: Tag to be added to entries to link to a specific user.
@@ -223,13 +222,13 @@ def fetch_context(
223
222
224
223
# if we don't find semantic matches fallback to returning recent context
225
224
if not hits and fall_back :
226
- return self .conversation_history (as_text = as_text , top_k = top_k , raw = raw )
225
+ return self .fetch_recent (as_text = as_text , top_k = top_k , raw = raw )
227
226
if raw :
228
227
return hits
229
228
return self ._format_context (hits , as_text )
230
229
231
230
232
- def conversation_history (
231
+ def fetch_recent (
233
232
self ,
234
233
as_text : bool = False ,
235
234
top_k : int = 3 ,
@@ -238,7 +237,7 @@ def conversation_history(
238
237
application_id : str = None ,
239
238
raw = False
240
239
) -> Union [List [str ], List [Dict [str ,str ]]]:
241
- """ Retreive the conversation history in sequential order.
240
+ """ Retreive the recent conversation history in sequential order.
242
241
243
242
Args:
244
243
as_text bool: Whether to return the conversation as a single string,
@@ -272,8 +271,8 @@ def conversation_history(
272
271
combined = self ._tag_filter & last_k_filter
273
272
274
273
query = FilterQuery (
275
- return_fields = return_fields ,
276
- filter_expression = combined
274
+ return_fields = return_fields ,
275
+ filter_expression = combined
277
276
)
278
277
hits = self ._index .query (query )
279
278
if raw :
@@ -291,7 +290,7 @@ def _format_context(
291
290
292
291
Args:
293
292
hits List: The hashes containing prompt & response pairs from
294
- conversation history.
293
+ recent conversation history.
295
294
as_text bool: Whether to return the conversation as a single string,
296
295
or list of alternating prompts and responses.
297
296
Returns:
0 commit comments