|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# LLM Session Memory" |
| 7 | + "# LLM Message History" |
8 | 8 | ]
|
9 | 9 | },
|
10 | 10 | {
|
|
15 | 15 | "\n",
|
16 | 16 | "The solution to this problem is to append the previous conversation history to each subsequent call to the LLM.\n",
|
17 | 17 | "\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." |
19 | 19 | ]
|
20 | 20 | },
|
21 | 21 | {
|
|
32 | 32 | }
|
33 | 33 | ],
|
34 | 34 | "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')" |
37 | 37 | ]
|
38 | 38 | },
|
39 | 39 | {
|
|
52 | 52 | "metadata": {},
|
53 | 53 | "outputs": [],
|
54 | 54 | "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", |
57 | 57 | " {\"role\":\"user\", \"content\":\"What is the capital of France?\"},\n",
|
58 | 58 | " {\"role\":\"llm\", \"content\":\"The capital is Paris.\"},\n",
|
59 | 59 | " {\"role\":\"user\", \"content\":\"And what is the capital of Spain?\"},\n",
|
|
88 | 88 | }
|
89 | 89 | ],
|
90 | 90 | "source": [
|
91 |
| - "context = chat_session.get_recent()\n", |
| 91 | + "context = chat_history.get_recent()\n", |
92 | 92 | "for message in context:\n",
|
93 | 93 | " print(message)"
|
94 | 94 | ]
|
|
97 | 97 | "cell_type": "markdown",
|
98 | 98 | "metadata": {},
|
99 | 99 | "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." |
101 | 101 | ]
|
102 | 102 | },
|
103 | 103 | {
|
|
121 | 121 | "source": [
|
122 | 122 | "prompt = \"what is the size of England compared to Portugal?\"\n",
|
123 | 123 | "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", |
125 | 125 | "\n",
|
126 |
| - "context = chat_session.get_recent(top_k=6)\n", |
| 126 | + "context = chat_history.get_recent(top_k=6)\n", |
127 | 127 | "for message in context:\n",
|
128 | 128 | " print(message)"
|
129 | 129 | ]
|
|
160 | 160 | }
|
161 | 161 | ],
|
162 | 162 | "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", |
165 | 165 | " {\"role\":\"user\", \"content\":\"What is the value of x in the equation 2x + 3 = 7?\"},\n",
|
166 | 166 | " {\"role\":\"llm\", \"content\":\"The value of x is 2.\"},\n",
|
167 | 167 | " {\"role\":\"user\", \"content\":\"What is the value of y in the equation 3y - 5 = 7?\"},\n",
|
168 | 168 | " {\"role\":\"llm\", \"content\":\"The value of y is 4.\"}],\n",
|
169 | 169 | " session_tag='student two'\n",
|
170 | 170 | " )\n",
|
171 | 171 | "\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", |
173 | 173 | " print(math_message)"
|
174 | 174 | ]
|
175 | 175 | },
|
176 | 176 | {
|
177 | 177 | "cell_type": "markdown",
|
178 | 178 | "metadata": {},
|
179 | 179 | "source": [
|
180 |
| - "## Semantic conversation memory\n", |
| 180 | + "## Semantic message history\n", |
181 | 181 | "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",
|
182 | 182 | "\n",
|
183 | 183 | "A typical flow looks like this:\n",
|
184 | 184 | "```\n",
|
185 | 185 | "while True:\n",
|
186 | 186 | " prompt = input('enter your next question')\n",
|
187 |
| - " context = chat_session.get_recent()\n", |
| 187 | + " context = chat_history.get_recent()\n", |
188 | 188 | " response = LLM_api_call(prompt=prompt, context=context)\n",
|
189 |
| - " chat_session.store(prompt, response)\n", |
| 189 | + " chat_history.store(prompt, response)\n", |
190 | 190 | "```\n",
|
191 | 191 | "\n",
|
192 | 192 | "This works, but as context keeps growing so too does our LLM token count, which increases latency and cost.\n",
|
|
195 | 195 | "\n",
|
196 | 196 | "A better solution is to pass only the relevant conversational context on each subsequent call.\n",
|
197 | 197 | "\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." |
199 | 199 | ]
|
200 | 200 | },
|
201 | 201 | {
|
202 | 202 | "cell_type": "code",
|
203 |
| - "execution_count": 6, |
| 203 | + "execution_count": 7, |
204 | 204 | "metadata": {},
|
205 | 205 | "outputs": [
|
206 | 206 | {
|
|
212 | 212 | }
|
213 | 213 | ],
|
214 | 214 | "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", |
217 | 217 | "\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))" |
219 | 219 | ]
|
220 | 220 | },
|
221 | 221 | {
|
|
234 | 234 | ],
|
235 | 235 | "source": [
|
236 | 236 | "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", |
239 | 239 | "for message in context:\n",
|
240 | 240 | " print(message)"
|
241 | 241 | ]
|
|
266 | 266 | }
|
267 | 267 | ],
|
268 | 268 | "source": [
|
269 |
| - "semantic_session.set_distance_threshold(0.7)\n", |
| 269 | + "semantic_history.set_distance_threshold(0.7)\n", |
270 | 270 | "\n",
|
271 |
| - "larger_context = semantic_session.get_relevant(prompt)\n", |
| 271 | + "larger_context = semantic_history.get_relevant(prompt)\n", |
272 | 272 | "for message in larger_context:\n",
|
273 | 273 | " print(message)"
|
274 | 274 | ]
|
|
300 | 300 | }
|
301 | 301 | ],
|
302 | 302 | "source": [
|
303 |
| - "semantic_session.store(\n", |
| 303 | + "semantic_history.store(\n", |
304 | 304 | " prompt=\"what is the smallest country in Europe?\",\n",
|
305 | 305 | " response=\"Monaco is the smallest country in Europe at 0.78 square miles.\" # Incorrect. Vatican City is the smallest country in Europe\n",
|
306 | 306 | " )\n",
|
307 | 307 | "\n",
|
308 | 308 | "# 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", |
310 | 310 | "bad_key = context[0]['entry_id']\n",
|
311 |
| - "semantic_session.drop(bad_key)\n", |
| 311 | + "semantic_history.drop(bad_key)\n", |
312 | 312 | "\n",
|
313 |
| - "corrected_context = semantic_session.get_recent()\n", |
| 313 | + "corrected_context = semantic_history.get_recent()\n", |
314 | 314 | "for message in corrected_context:\n",
|
315 | 315 | " print(message)"
|
316 | 316 | ]
|
|
321 | 321 | "metadata": {},
|
322 | 322 | "outputs": [],
|
323 | 323 | "source": [
|
324 |
| - "chat_session.clear()" |
| 324 | + "chat_history.clear()\n", |
| 325 | + "semantic_history.clear()" |
325 | 326 | ]
|
326 | 327 | }
|
327 | 328 | ],
|
|
0 commit comments