@@ -114,6 +114,36 @@ def __init__(self,
114
114
self ._tag_filter = self ._tag_filter & user_filter & session_filter
115
115
116
116
117
+ def set_scope (
118
+ self ,
119
+ session_id : str = None ,
120
+ user_id : str = None ,
121
+ application_id : str = None
122
+ ) -> None :
123
+ """ Set the tag filter to apply to querries based on the desired scope.
124
+
125
+ Args:
126
+ session_id str: Id of the specific session to filter to. Default is
127
+ None, which means all sessions will be in scope.
128
+ user_id str: Id of the specific user to filter to. Default is None,
129
+ which means all users will be in scope.
130
+ application_id str: Id of the specific application to filter to.
131
+ Default is None, which means all applications ill be in scope.
132
+ """
133
+ if not (session_id or user_id or application_id ):
134
+ return
135
+
136
+ tag_filter = None
137
+ if application_id :
138
+ tag_filter = tag_filter & (Tag ("application_id" ) == application_id )
139
+ if user_id :
140
+ tag_filter = tag_filter & (Tag ("user_id" ) == self ._user_id )
141
+ if session_id :
142
+ tag_filter = tag_filter & (Tag ("session_id" ) == self ._user_id )
143
+
144
+ self ._tag_filter = tag_filter
145
+
146
+
117
147
def clear (self ) -> None :
118
148
""" Clears the chat session history. """
119
149
with self ._index .client .pipeline (transaction = False ) as pipe :
@@ -132,7 +162,10 @@ def fetch_context(
132
162
prompt : str ,
133
163
as_text : bool = False ,
134
164
top_k : int = 3 ,
135
- fall_back : bool = False
165
+ fall_back : bool = False ,
166
+ session_id : str = None ,
167
+ user_id : str = None ,
168
+ application_id : str = None ,
136
169
) -> Union [List [str ], List [Dict [str ,str ]]]:
137
170
""" Searches the chat history for information semantically related to
138
171
the specified prompt.
@@ -152,9 +185,9 @@ def fetch_context(
152
185
153
186
Returns:
154
187
Union[List[str], List[Dict[str,str]]: Either a list of strings, or a
155
- list of prompts and responses in JSON containing the most relevant
188
+ list of prompts and responses in JSON containing the most relevant.
156
189
"""
157
-
190
+ self . set_scope ( session_id , user_id , application_id )
158
191
return_fields = [
159
192
"session_id" ,
160
193
"user_id" ,
@@ -226,9 +259,21 @@ def conversation_history(
226
259
227
260
def _format_context (
228
261
self ,
229
- hits : List [Dict [str : Any ]],
262
+ hits : List [Dict [str , Any ]],
230
263
as_text : bool
231
- ) -> Union [List [str ], List [Dict [str ,str ]]]:
264
+ ) -> Union [List [str ], List [Dict [str , str ]]]:
265
+ """ Extracts the prompt and response fields from the Redis hashes and
266
+ formats them as either flat dictionaries oor strings.
267
+
268
+ Args:
269
+ hits List: The hashes containing prompt & response pairs from
270
+ conversation history.
271
+ as_text bool: Whether to return the conversation as a single string,
272
+ or list of alternating prompts and responses.
273
+ Returns:
274
+ Union[str, List[str]]: A single string transcription of the session
275
+ or list of strings if as_text is false.
276
+ """
232
277
if hits :
233
278
hits .sort (key = lambda x : x ['timestamp' ]) # TODO move sorting to query.py
234
279
0 commit comments