@@ -22,7 +22,7 @@ def __init__(self,
22
22
vectorizer : Optional [BaseVectorizer ] = None ,
23
23
distance_threshold : float = 0.3 ,
24
24
redis_client : Optional [Redis ] = None ,
25
- preamble : str = None
25
+ preamble : str = ''
26
26
):
27
27
""" Initialize session memory with index
28
28
@@ -122,6 +122,9 @@ def set_scope(
122
122
) -> None :
123
123
""" Set the tag filter to apply to querries based on the desired scope.
124
124
125
+ 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.
127
+
125
128
Args:
126
129
session_id str: Id of the specific session to filter to. Default is
127
130
None, which means all sessions will be in scope.
@@ -133,7 +136,7 @@ def set_scope(
133
136
if not (session_id or user_id or application_id ):
134
137
return
135
138
136
- tag_filter = None
139
+ tag_filter = Tag ( 'application_id' ) == []
137
140
if application_id :
138
141
tag_filter = tag_filter & (Tag ("application_id" ) == application_id )
139
142
if user_id :
@@ -166,6 +169,7 @@ def fetch_context(
166
169
session_id : str = None ,
167
170
user_id : str = None ,
168
171
application_id : str = None ,
172
+ raw : bool = False
169
173
) -> Union [List [str ], List [Dict [str ,str ]]]:
170
174
""" Searches the chat history for information semantically related to
171
175
the specified prompt.
@@ -182,6 +186,13 @@ def fetch_context(
182
186
top_k int: The number of previous exchanges to return. Default is 3.
183
187
fallback bool: Whether to drop back to conversation history if no
184
188
relevant context is found.
189
+ session_id str: Tag to be added to entries to link to a specific
190
+ session.
191
+ user_id str: Tag to be added to entries to link to a specific user.
192
+ application_id str: Tag to be added to entries to link to a
193
+ specific application.
194
+ raw bool: Whether to return the full Redis hash entry or just the
195
+ prompt and response.
185
196
186
197
Returns:
187
198
Union[List[str], List[Dict[str,str]]: Either a list of strings, or a
@@ -212,14 +223,19 @@ def fetch_context(
212
223
213
224
# if we don't find semantic matches fallback to returning recent context
214
225
if not hits and fall_back :
215
- return self .conversation_history (as_text = as_text , top_k = top_k )
226
+ return self .conversation_history (as_text = as_text , top_k = top_k , raw = raw )
227
+ if raw :
228
+ return hits
216
229
return self ._format_context (hits , as_text )
217
230
218
231
219
232
def conversation_history (
220
233
self ,
221
234
as_text : bool = False ,
222
235
top_k : int = 3 ,
236
+ session_id : str = None ,
237
+ user_id : str = None ,
238
+ application_id : str = None ,
223
239
raw = False
224
240
) -> Union [List [str ], List [Dict [str ,str ]]]:
225
241
""" Retreive the conversation history in sequential order.
@@ -228,12 +244,18 @@ def conversation_history(
228
244
as_text bool: Whether to return the conversation as a single string,
229
245
or list of alternating prompts and responses.
230
246
top_k int: The number of previous exchanges to return. Default is 3
247
+ session_id str: Tag to be added to entries to link to a specific
248
+ session.
249
+ user_id str: Tag to be added to entries to link to a specific user.
250
+ application_id str: Tag to be added to entries to link to a
251
+ specific application.
231
252
raw bool: Whether to return the full Redis hash entry or just the
232
253
prompt and response
233
254
Returns:
234
255
Union[str, List[str]]: A single string transcription of the session
235
256
or list of strings if as_text is false.
236
257
"""
258
+ self .set_scope (session_id , user_id , application_id )
237
259
return_fields = [
238
260
"session_id" ,
239
261
"user_id" ,
@@ -254,6 +276,8 @@ def conversation_history(
254
276
filter_expression = combined
255
277
)
256
278
hits = self ._index .query (query )
279
+ if raw :
280
+ return hits
257
281
return self ._format_context (hits , as_text )
258
282
259
283
0 commit comments