Skip to content

Commit 37ae270

Browse files
improves scope settings
1 parent 5fda1ec commit 37ae270

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

redisvl/extensions/session_manager/session.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self,
2222
vectorizer: Optional[BaseVectorizer] = None,
2323
distance_threshold: float = 0.3,
2424
redis_client: Optional[Redis] = None,
25-
preamble: str = None
25+
preamble: str = ''
2626
):
2727
""" Initialize session memory with index
2828
@@ -122,6 +122,9 @@ def set_scope(
122122
) -> None:
123123
""" Set the tag filter to apply to querries based on the desired scope.
124124
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+
125128
Args:
126129
session_id str: Id of the specific session to filter to. Default is
127130
None, which means all sessions will be in scope.
@@ -133,7 +136,7 @@ def set_scope(
133136
if not (session_id or user_id or application_id):
134137
return
135138

136-
tag_filter = None
139+
tag_filter = Tag('application_id') == []
137140
if application_id:
138141
tag_filter = tag_filter & (Tag("application_id") == application_id)
139142
if user_id:
@@ -166,6 +169,7 @@ def fetch_context(
166169
session_id: str = None,
167170
user_id: str = None,
168171
application_id: str = None,
172+
raw: bool = False
169173
) -> Union[List[str], List[Dict[str,str]]]:
170174
""" Searches the chat history for information semantically related to
171175
the specified prompt.
@@ -182,6 +186,13 @@ def fetch_context(
182186
top_k int: The number of previous exchanges to return. Default is 3.
183187
fallback bool: Whether to drop back to conversation history if no
184188
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.
185196
186197
Returns:
187198
Union[List[str], List[Dict[str,str]]: Either a list of strings, or a
@@ -212,14 +223,19 @@ def fetch_context(
212223

213224
# if we don't find semantic matches fallback to returning recent context
214225
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
216229
return self._format_context(hits, as_text)
217230

218231

219232
def conversation_history(
220233
self,
221234
as_text: bool = False,
222235
top_k: int = 3,
236+
session_id: str = None,
237+
user_id: str = None,
238+
application_id: str = None,
223239
raw = False
224240
) -> Union[List[str], List[Dict[str,str]]]:
225241
""" Retreive the conversation history in sequential order.
@@ -228,12 +244,18 @@ def conversation_history(
228244
as_text bool: Whether to return the conversation as a single string,
229245
or list of alternating prompts and responses.
230246
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.
231252
raw bool: Whether to return the full Redis hash entry or just the
232253
prompt and response
233254
Returns:
234255
Union[str, List[str]]: A single string transcription of the session
235256
or list of strings if as_text is false.
236257
"""
258+
self.set_scope(session_id, user_id, application_id)
237259
return_fields = [
238260
"session_id",
239261
"user_id",
@@ -254,6 +276,8 @@ def conversation_history(
254276
filter_expression=combined
255277
)
256278
hits = self._index.query(query)
279+
if raw:
280+
return hits
257281
return self._format_context(hits, as_text)
258282

259283

0 commit comments

Comments
 (0)