Skip to content

Commit 3d404e7

Browse files
formatting
1 parent 4e432a4 commit 3d404e7

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

redisvl/extensions/session_manager/session.py

+24-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import hashlib
2-
from typing import Any, Dict, List, Optional, Tuple, Union
32
from datetime import datetime
4-
3+
from typing import Any, Dict, List, Optional, Tuple, Union
54
from redis import Redis
6-
75
from redisvl.index import SearchIndex
86
from redisvl.query import FilterQuery, RangeQuery
97
from redisvl.query.filter import Tag, Num
@@ -12,18 +10,19 @@
1210
from redisvl.utils.vectorize import BaseVectorizer, HFTextVectorizer
1311

1412
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+
):
2726
""" Initialize session memory with index
2827
2928
Session Manager stores the current and previous user text prompts and
@@ -123,7 +122,7 @@ def set_scope(
123122
""" Set the tag filter to apply to querries based on the desired scope.
124123
125124
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.
127126
128127
Args:
129128
session_id str: Id of the specific session to filter to. Default is
@@ -160,7 +159,7 @@ def delete(self) -> None:
160159
self._index.delete(drop=True)
161160

162161

163-
def fetch_context(
162+
def fetch_relevant(
164163
self,
165164
prompt: str,
166165
as_text: bool = False,
@@ -184,8 +183,8 @@ def fetch_context(
184183
as_text bool: Whether to return the prompt:response pairs as text
185184
or as JSON
186185
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.
189188
session_id str: Tag to be added to entries to link to a specific
190189
session.
191190
user_id str: Tag to be added to entries to link to a specific user.
@@ -223,13 +222,13 @@ def fetch_context(
223222

224223
# if we don't find semantic matches fallback to returning recent context
225224
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)
227226
if raw:
228227
return hits
229228
return self._format_context(hits, as_text)
230229

231230

232-
def conversation_history(
231+
def fetch_recent(
233232
self,
234233
as_text: bool = False,
235234
top_k: int = 3,
@@ -238,7 +237,7 @@ def conversation_history(
238237
application_id: str = None,
239238
raw = False
240239
) -> Union[List[str], List[Dict[str,str]]]:
241-
""" Retreive the conversation history in sequential order.
240+
""" Retreive the recent conversation history in sequential order.
242241
243242
Args:
244243
as_text bool: Whether to return the conversation as a single string,
@@ -272,8 +271,8 @@ def conversation_history(
272271
combined = self._tag_filter & last_k_filter
273272

274273
query = FilterQuery(
275-
return_fields=return_fields,
276-
filter_expression=combined
274+
return_fields=return_fields,
275+
filter_expression=combined
277276
)
278277
hits = self._index.query(query)
279278
if raw:
@@ -291,7 +290,7 @@ def _format_context(
291290
292291
Args:
293292
hits List: The hashes containing prompt & response pairs from
294-
conversation history.
293+
recent conversation history.
295294
as_text bool: Whether to return the conversation as a single string,
296295
or list of alternating prompts and responses.
297296
Returns:

0 commit comments

Comments
 (0)