1
1
from typing import Any , Dict , List , Optional , Union
2
+ from uuid import uuid4
2
3
3
4
from redis import Redis
4
5
6
+ from redisvl .query .filter import FilterExpression
7
+
5
8
6
9
class BaseSessionManager :
7
- id_field_name : str = "id_field "
10
+ id_field_name : str = "_id "
8
11
role_field_name : str = "role"
9
12
content_field_name : str = "content"
10
13
tool_field_name : str = "tool_call_id"
11
14
timestamp_field_name : str = "timestamp"
15
+ session_field_name : str = "session_tag"
12
16
13
17
def __init__ (
14
18
self ,
15
19
name : str ,
16
- session_tag : str ,
17
- user_tag : str ,
20
+ session_tag : Optional [str ] = None ,
18
21
):
19
22
"""Initialize session memory with index
20
23
@@ -26,29 +29,10 @@ def __init__(
26
29
Args:
27
30
name (str): The name of the session manager index.
28
31
session_tag (str): Tag to be added to entries to link to a specific
29
- session.
30
- user_tag (str): Tag to be added to entries to link to a specific user.
32
+ session. Defaults to instance uuid.
31
33
"""
32
34
self ._name = name
33
- self ._user_tag = user_tag
34
- self ._session_tag = session_tag
35
-
36
- def set_scope (
37
- self ,
38
- session_tag : Optional [str ] = None ,
39
- user_tag : Optional [str ] = None ,
40
- ) -> None :
41
- """Set the filter to apply to querries based on the desired scope.
42
-
43
- This new scope persists until another call to set_scope is made, or if
44
- scope specified in calls to get_recent.
45
-
46
- Args:
47
- session_tag (str): Id of the specific session to filter to. Default is
48
- None.
49
- user_tag (str): Id of the specific user to filter to. Default is None.
50
- """
51
- raise NotImplementedError
35
+ self ._session_tag = session_tag or uuid4 ().hex
52
36
53
37
def clear (self ) -> None :
54
38
"""Clears the chat session history."""
@@ -75,23 +59,21 @@ def messages(self) -> Union[List[str], List[Dict[str, str]]]:
75
59
def get_recent (
76
60
self ,
77
61
top_k : int = 5 ,
78
- session_tag : Optional [str ] = None ,
79
- user_tag : Optional [str ] = None ,
80
62
as_text : bool = False ,
81
63
raw : bool = False ,
64
+ session_tag : Optional [str ] = None ,
82
65
) -> Union [List [str ], List [Dict [str , str ]]]:
83
66
"""Retreive the recent conversation history in sequential order.
84
67
85
68
Args:
86
69
top_k (int): The number of previous exchanges to return. Default is 5.
87
70
Note that one exchange contains both a prompt and response.
88
- session_tag (str): Tag to be added to entries to link to a specific
89
- session.
90
- user_tag (str): Tag to be added to entries to link to a specific user.
91
71
as_text (bool): Whether to return the conversation as a single string,
92
72
or list of alternating prompts and responses.
93
73
raw (bool): Whether to return the full Redis hash entry or just the
94
74
prompt and response
75
+ session_tag (str): Tag to be added to entries to link to a specific
76
+ session. Defaults to instance uuid.
95
77
96
78
Returns:
97
79
Union[str, List[str]]: A single string transcription of the session
@@ -113,6 +95,7 @@ def _format_context(
113
95
recent conversation history.
114
96
as_text (bool): Whether to return the conversation as a single string,
115
97
or list of alternating prompts and responses.
98
+
116
99
Returns:
117
100
Union[str, List[str]]: A single string transcription of the session
118
101
or list of strings if as_text is false.
@@ -141,33 +124,42 @@ def _format_context(
141
124
)
142
125
return statements
143
126
144
- def store (self , prompt : str , response : str ) -> None :
127
+ def store (
128
+ self , prompt : str , response : str , session_tag : Optional [str ] = None
129
+ ) -> None :
145
130
"""Insert a prompt:response pair into the session memory. A timestamp
146
131
is associated with each exchange so that they can be later sorted
147
132
in sequential ordering after retrieval.
148
133
149
134
Args:
150
135
prompt (str): The user prompt to the LLM.
151
136
response (str): The corresponding LLM response.
137
+ session_tag (Optional[str]): The tag to mark the message with. Defaults to None.
152
138
"""
153
139
raise NotImplementedError
154
140
155
- def add_messages (self , messages : List [Dict [str , str ]]) -> None :
141
+ def add_messages (
142
+ self , messages : List [Dict [str , str ]], session_tag : Optional [str ] = None
143
+ ) -> None :
156
144
"""Insert a list of prompts and responses into the session memory.
157
145
A timestamp is associated with each so that they can be later sorted
158
146
in sequential ordering after retrieval.
159
147
160
148
Args:
161
149
messages (List[Dict[str, str]]): The list of user prompts and LLM responses.
150
+ session_tag (Optional[str]): The tag to mark the messages with. Defaults to None.
162
151
"""
163
152
raise NotImplementedError
164
153
165
- def add_message (self , message : Dict [str , str ]) -> None :
154
+ def add_message (
155
+ self , message : Dict [str , str ], session_tag : Optional [str ] = None
156
+ ) -> None :
166
157
"""Insert a single prompt or response into the session memory.
167
158
A timestamp is associated with it so that it can be later sorted
168
159
in sequential ordering after retrieval.
169
160
170
161
Args:
171
162
message (Dict[str,str]): The user prompt or LLM response.
163
+ session_tag (Optional[str]): The tag to mark the message with. Defaults to None.
172
164
"""
173
165
raise NotImplementedError
0 commit comments