Skip to content

Commit a41e1df

Browse files
renames session manager classes to message history
1 parent dca2326 commit a41e1df

File tree

14 files changed

+1160
-1058
lines changed

14 files changed

+1160
-1058
lines changed
Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# LLM Session Memory"
7+
"# LLM Message History"
88
]
99
},
1010
{
@@ -15,12 +15,12 @@
1515
"\n",
1616
"The solution to this problem is to append the previous conversation history to each subsequent call to the LLM.\n",
1717
"\n",
18-
"This notebook will show how to use Redis to structure and store and retrieve this conversational session memory."
18+
"This notebook will show how to use Redis to structure and store and retrieve this conversational message history."
1919
]
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 1,
23+
"execution_count": null,
2424
"metadata": {},
2525
"outputs": [
2626
{
@@ -32,8 +32,8 @@
3232
}
3333
],
3434
"source": [
35-
"from redisvl.extensions.session_manager import StandardSessionManager\n",
36-
"chat_session = StandardSessionManager(name='student tutor')"
35+
"from redisvl.extensions.message_history import MessageHistory\n",
36+
"chat_history = MessageHistory(name='student tutor')"
3737
]
3838
},
3939
{
@@ -48,12 +48,12 @@
4848
},
4949
{
5050
"cell_type": "code",
51-
"execution_count": 2,
51+
"execution_count": null,
5252
"metadata": {},
5353
"outputs": [],
5454
"source": [
55-
"chat_session.add_message({\"role\":\"system\", \"content\":\"You are a helpful geography tutor, giving simple and short answers to questions about Europen countries.\"})\n",
56-
"chat_session.add_messages([\n",
55+
"chat_history.add_message({\"role\":\"system\", \"content\":\"You are a helpful geography tutor, giving simple and short answers to questions about European countries.\"})\n",
56+
"chat_history.add_messages([\n",
5757
" {\"role\":\"user\", \"content\":\"What is the capital of France?\"},\n",
5858
" {\"role\":\"llm\", \"content\":\"The capital is Paris.\"},\n",
5959
" {\"role\":\"user\", \"content\":\"And what is the capital of Spain?\"},\n",
@@ -72,7 +72,7 @@
7272
},
7373
{
7474
"cell_type": "code",
75-
"execution_count": 3,
75+
"execution_count": null,
7676
"metadata": {},
7777
"outputs": [
7878
{
@@ -88,7 +88,7 @@
8888
}
8989
],
9090
"source": [
91-
"context = chat_session.get_recent()\n",
91+
"context = chat_history.get_recent()\n",
9292
"for message in context:\n",
9393
" print(message)"
9494
]
@@ -97,12 +97,12 @@
9797
"cell_type": "markdown",
9898
"metadata": {},
9999
"source": [
100-
"In many LLM flows the conversation progresses in a series of prompt and response pairs. session managers offer a convienience function `store()` to add these simply."
100+
"In many LLM flows the conversation progresses in a series of prompt and response pairs. Message history offer a convenience function `store()` to add these simply."
101101
]
102102
},
103103
{
104104
"cell_type": "code",
105-
"execution_count": 4,
105+
"execution_count": null,
106106
"metadata": {},
107107
"outputs": [
108108
{
@@ -121,9 +121,9 @@
121121
"source": [
122122
"prompt = \"what is the size of England compared to Portugal?\"\n",
123123
"response = \"England is larger in land area than Portal by about 15000 square miles.\"\n",
124-
"chat_session.store(prompt, response)\n",
124+
"chat_history.store(prompt, response)\n",
125125
"\n",
126-
"context = chat_session.get_recent(top_k=6)\n",
126+
"context = chat_history.get_recent(top_k=6)\n",
127127
"for message in context:\n",
128128
" print(message)"
129129
]
@@ -144,7 +144,7 @@
144144
},
145145
{
146146
"cell_type": "code",
147-
"execution_count": 5,
147+
"execution_count": null,
148148
"metadata": {},
149149
"outputs": [
150150
{
@@ -160,33 +160,33 @@
160160
}
161161
],
162162
"source": [
163-
"chat_session.add_message({\"role\":\"system\", \"content\":\"You are a helpful algebra tutor, giving simple answers to math problems.\"}, session_tag='student two')\n",
164-
"chat_session.add_messages([\n",
163+
"chat_history.add_message({\"role\":\"system\", \"content\":\"You are a helpful algebra tutor, giving simple answers to math problems.\"}, session_tag='student two')\n",
164+
"chat_history.add_messages([\n",
165165
" {\"role\":\"user\", \"content\":\"What is the value of x in the equation 2x + 3 = 7?\"},\n",
166166
" {\"role\":\"llm\", \"content\":\"The value of x is 2.\"},\n",
167167
" {\"role\":\"user\", \"content\":\"What is the value of y in the equation 3y - 5 = 7?\"},\n",
168168
" {\"role\":\"llm\", \"content\":\"The value of y is 4.\"}],\n",
169169
" session_tag='student two'\n",
170170
" )\n",
171171
"\n",
172-
"for math_message in chat_session.get_recent(session_tag='student two'):\n",
172+
"for math_message in chat_history.get_recent(session_tag='student two'):\n",
173173
" print(math_message)"
174174
]
175175
},
176176
{
177177
"cell_type": "markdown",
178178
"metadata": {},
179179
"source": [
180-
"## Semantic conversation memory\n",
180+
"## Semantic message history\n",
181181
"For longer conversations our list of messages keeps growing. Since LLMs are stateless we have to continue to pass this conversation history on each subsequent call to ensure the LLM has the correct context.\n",
182182
"\n",
183183
"A typical flow looks like this:\n",
184184
"```\n",
185185
"while True:\n",
186186
" prompt = input('enter your next question')\n",
187-
" context = chat_session.get_recent()\n",
187+
" context = chat_history.get_recent()\n",
188188
" response = LLM_api_call(prompt=prompt, context=context)\n",
189-
" chat_session.store(prompt, response)\n",
189+
" chat_history.store(prompt, response)\n",
190190
"```\n",
191191
"\n",
192192
"This works, but as context keeps growing so too does our LLM token count, which increases latency and cost.\n",
@@ -195,12 +195,12 @@
195195
"\n",
196196
"A better solution is to pass only the relevant conversational context on each subsequent call.\n",
197197
"\n",
198-
"For this, RedisVL has the `SemanticSessionManager`, which uses vector similarity search to return only semantically relevant sections of the conversation."
198+
"For this, RedisVL has the `SemanticMessageHistory`, which uses vector similarity search to return only semantically relevant sections of the conversation."
199199
]
200200
},
201201
{
202202
"cell_type": "code",
203-
"execution_count": 6,
203+
"execution_count": null,
204204
"metadata": {},
205205
"outputs": [
206206
{
@@ -212,15 +212,15 @@
212212
}
213213
],
214214
"source": [
215-
"from redisvl.extensions.session_manager import SemanticSessionManager\n",
216-
"semantic_session = SemanticSessionManager(name='tutor')\n",
215+
"from redisvl.extensions.message_history import SemanticMessageHistory\n",
216+
"semantic_history = SemanticMessageHistory(name='tutor')\n",
217217
"\n",
218-
"semantic_session.add_messages(chat_session.get_recent(top_k=8))"
218+
"semantic_history.add_messages(chat_history.get_recent(top_k=8))"
219219
]
220220
},
221221
{
222222
"cell_type": "code",
223-
"execution_count": 7,
223+
"execution_count": null,
224224
"metadata": {},
225225
"outputs": [
226226
{
@@ -234,8 +234,8 @@
234234
],
235235
"source": [
236236
"prompt = \"what have I learned about the size of England?\"\n",
237-
"semantic_session.set_distance_threshold(0.35)\n",
238-
"context = semantic_session.get_relevant(prompt)\n",
237+
"semantic_history.set_distance_threshold(0.35)\n",
238+
"context = semantic_history.get_relevant(prompt)\n",
239239
"for message in context:\n",
240240
" print(message)"
241241
]
@@ -251,7 +251,7 @@
251251
},
252252
{
253253
"cell_type": "code",
254-
"execution_count": 8,
254+
"execution_count": null,
255255
"metadata": {},
256256
"outputs": [
257257
{
@@ -266,9 +266,9 @@
266266
}
267267
],
268268
"source": [
269-
"semantic_session.set_distance_threshold(0.7)\n",
269+
"semantic_history.set_distance_threshold(0.7)\n",
270270
"\n",
271-
"larger_context = semantic_session.get_relevant(prompt)\n",
271+
"larger_context = semantic_history.get_relevant(prompt)\n",
272272
"for message in larger_context:\n",
273273
" print(message)"
274274
]
@@ -284,7 +284,7 @@
284284
},
285285
{
286286
"cell_type": "code",
287-
"execution_count": 9,
287+
"execution_count": null,
288288
"metadata": {},
289289
"outputs": [
290290
{
@@ -300,28 +300,29 @@
300300
}
301301
],
302302
"source": [
303-
"semantic_session.store(\n",
303+
"semantic_history.store(\n",
304304
" prompt=\"what is the smallest country in Europe?\",\n",
305305
" response=\"Monaco is the smallest country in Europe at 0.78 square miles.\" # Incorrect. Vatican City is the smallest country in Europe\n",
306306
" )\n",
307307
"\n",
308308
"# get the key of the incorrect message\n",
309-
"context = semantic_session.get_recent(top_k=1, raw=True)\n",
309+
"context = semantic_history.get_recent(top_k=1, raw=True)\n",
310310
"bad_key = context[0]['entry_id']\n",
311-
"semantic_session.drop(bad_key)\n",
311+
"semantic_history.drop(bad_key)\n",
312312
"\n",
313-
"corrected_context = semantic_session.get_recent()\n",
313+
"corrected_context = semantic_history.get_recent()\n",
314314
"for message in corrected_context:\n",
315315
" print(message)"
316316
]
317317
},
318318
{
319319
"cell_type": "code",
320-
"execution_count": 10,
320+
"execution_count": null,
321321
"metadata": {},
322322
"outputs": [],
323323
"source": [
324-
"chat_session.clear()"
324+
"chat_history.clear()\n",
325+
"semantic_history.clear()"
325326
]
326327
}
327328
],

redisvl/extensions/constants.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
"""
2-
Constants used within the extension classes SemanticCache, BaseSessionManager,
3-
StandardSessionManager,SemanticSessionManager and SemanticRouter.
2+
Constants used within the extension classes SemanticCache, BaseMessageHistory,
3+
MessageHistory, SemanticMessageHistory and SemanticRouter.
44
These constants are also used within theses classes corresponding schema.
55
"""
66

7-
# BaseSessionManager
7+
# BaseMessageHistory
88
ID_FIELD_NAME: str = "entry_id"
99
ROLE_FIELD_NAME: str = "role"
1010
CONTENT_FIELD_NAME: str = "content"
1111
TOOL_FIELD_NAME: str = "tool_call_id"
1212
TIMESTAMP_FIELD_NAME: str = "timestamp"
1313
SESSION_FIELD_NAME: str = "session_tag"
1414

15-
# SemanticSessionManager
16-
SESSION_VECTOR_FIELD_NAME: str = "vector_field"
15+
# SemanticMessageHistory
16+
MESSAGE_VECTOR_FIELD_NAME: str = "vector_field"
1717

1818
# SemanticCache
1919
REDIS_KEY_FIELD_NAME: str = "key"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from redisvl.extensions.message_history.base_history import BaseMessageHistory
2+
from redisvl.extensions.message_history.message_history import MessageHistory
3+
from redisvl.extensions.message_history.semantic_message_history import (
4+
SemanticMessageHistory,
5+
)
6+
7+
__all__ = ["BaseMessageHistory", "MessageHistory", "SemanticMessageHistory"]

0 commit comments

Comments
 (0)