Skip to content

renames session manager to message history #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ To get started with RAG, either from scratch or using a popular framework like L
| [/RAG/07_user_role_based_rag.ipynb](python-recipes/RAG/07_user_role_based_rag.ipynb) | Implement a simple RBAC policy with vector search using Redis |

### LLM Memory
LLMs are stateless. To maintain context within a conversation chat sessions must be stored and resent to the LLM. Redis manages the storage and retrieval of chat sessions to maintain context and conversational relevance.
LLMs are stateless. To maintain context within a conversation chat sessions must be stored and resent to the LLM. Redis manages the storage and retrieval of message histories to maintain context and conversational relevance.
| Recipe | Description |
| --- | --- |
| [/llm-session-manager/00_session_manager.ipynb](python-recipes/llm-session-manager/00_llm_session_manager.ipynb) | LLM session manager with semantic similarity |
| [/llm-session-manager/01_multiple_sessions.ipynb](python-recipes/llm-session-manager/01_multiple_sessions.ipynb) | Handle multiple simultaneous chats with one instance |
| [/llm-message-history/00_message_history.ipynb](python-recipes/llm-message-history/00_llm_message_history.ipynb) | LLM message history with semantic similarity |
| [/llm-message-history/01_multiple_sessions.ipynb](python-recipes/llm-message-history/01_multiple_sessions.ipynb) | Handle multiple simultaneous chats with one instance |

### Semantic Cache
An estimated 31% of LLM queries are potentially redundant ([source](https://arxiv.org/pdf/2403.02694)). Redis enables semantic caching to help cut down on LLM costs quickly.
Expand Down
6 changes: 3 additions & 3 deletions python-recipes/RAG/07_user_role_based_rag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
}
],
"source": [
"%pip install -q \"redisvl>=0.4.1\" openai langchain-community pypdf"
"%pip install -q \"redisvl>=0.6.0\" openai langchain-community pypdf"
]
},
{
Expand Down Expand Up @@ -1335,7 +1335,7 @@
"from typing import List, Optional\n",
"import os\n",
"\n",
"from redisvl.extensions.session_manager import StandardSessionManager\n",
"from redisvl.extensions.message_history import MessageHistory\n",
"\n",
"\n",
"class RAGChatManager:\n",
Expand Down Expand Up @@ -1395,7 +1395,7 @@
" user_id: User identifier\n",
" \"\"\"\n",
" if user_id not in self.sessions:\n",
" self.sessions[user_id] = StandardSessionManager(\n",
" self.sessions[user_id] = MessageHistory(\n",
" name=f\"session:{user_id}\",\n",
" redis_client=self.kb.redis_client\n",
" )\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"source": [
"![Redis](https://redis.io/wp-content/uploads/2024/04/Logotype.svg?auto=webp&quality=85,75&width=120)\n",
"\n",
"# LLM Session Memory - Multiple Sessions\n",
"# LLM Message History\n",
"\n",
"Large Language Models are inherently stateless and have no knowledge of previous interactions with a user, or even of previous parts of the current conversation. While this may not be noticeable when asking simple questions, it becomes a hinderance when engaging in long running conversations that rely on conversational context.\n",
"Large Language Models are inherently stateless and have no knowledge of previous interactions with a user, or even of previous parts of the current conversation. While this may not be noticeable when asking simple questions, it becomes a hindrance when engaging in long running conversations that rely on conversational context.\n",
"\n",
"The solution to this problem is to append the previous conversation history to each subsequent call to the LLM.\n",
"\n",
"This notebook will show how to use Redis to structure and store and retrieve this conversational session memory.\n",
"This notebook will show how to use Redis to structure and store and retrieve this conversational message history.\n",
"\n",
"## Let's Begin!\n",
"<a href=\"https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/session-manager/00_session_manager.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
"<a href=\"https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/llm-message-history/00_message_history.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
]
},
{
Expand All @@ -31,7 +31,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install cohere \"redisvl>=0.4.1\" sentence-transformers"
"%pip install cohere \"redisvl>=0.6.0\" sentence-transformers"
]
},
{
Expand Down Expand Up @@ -153,7 +153,7 @@
" return response.text\n",
"\n",
" def remap(self, context) -> List[Dict]:\n",
" ''' re-index the chat history to match the Cohere API requirements '''\n",
" ''' re-index the message history to match the Cohere API requirements '''\n",
" new_context = []\n",
" for statement in context:\n",
" if statement[\"role\"] == \"user\":\n",
Expand All @@ -174,9 +174,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import SemanticSessionManager\n",
"### Import MessageHistory\n",
"\n",
"redisvl provides the SemanticSessionManager for easy management of session state."
"redisvl provides the MessageHistory and SemanticMessageHistory classes for easy management of LLM conversations."
]
},
{
Expand All @@ -185,10 +185,10 @@
"metadata": {},
"outputs": [],
"source": [
"from redisvl.extensions.session_manager import SemanticSessionManager\n",
"from redisvl.extensions.message_history import SemanticMessageHistory\n",
"\n",
"user_session = SemanticSessionManager(name=\"llm chef\")\n",
"user_session.add_message({\"role\":\"system\", \"content\":\"You are a helpful chef, assisting people in making delicious meals\"})"
"user_history = SemanticMessageHistory(name=\"llm chef\")\n",
"user_history.add_message({\"role\":\"system\", \"content\":\"You are a helpful chef, assisting people in making delicious meals\"})"
]
},
{
Expand Down Expand Up @@ -224,9 +224,9 @@
],
"source": [
"prompt = \"can you give me some ideas for breakfast?\"\n",
"context = user_session.get_recent()\n",
"context = user_history.get_recent()\n",
"response = client.converse(prompt=prompt, context=context)\n",
"user_session.store(prompt, response)\n",
"user_history.store(prompt, response)\n",
"print('USER: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand Down Expand Up @@ -286,9 +286,9 @@
],
"source": [
"prompt = \"can you give me the recipe for those pancakes?\"\n",
"context = user_session.get_recent()\n",
"context = user_history.get_recent()\n",
"response = client.converse(prompt=prompt, context=context)\n",
"user_session.store(prompt, response)\n",
"user_history.store(prompt, response)\n",
"print('USER: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand Down Expand Up @@ -360,9 +360,9 @@
],
"source": [
"prompt =\"I am vegetarian. Can you remove the eggs?\"\n",
"context = user_session.get_recent()\n",
"context = user_history.get_recent()\n",
"response = client.converse(prompt=prompt, context=context)\n",
"user_session.store(prompt, response)\n",
"user_history.store(prompt, response)\n",
"print('USER: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand Down Expand Up @@ -436,9 +436,9 @@
],
"source": [
"prompt = \"I am also vegan. Can you replace the butter too?\"\n",
"context = user_session.get_recent()\n",
"context = user_history.get_recent()\n",
"response = client.converse(prompt=prompt, context=context)\n",
"user_session.store(prompt, response)\n",
"user_history.store(prompt, response)\n",
"print('USER: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand Down Expand Up @@ -521,9 +521,9 @@
],
"source": [
"prompt = \"I changed my mind. Can you give me the first recipe from your list?\"\n",
"context = user_session.get_recent(top_k=5)\n",
"context = user_history.get_recent(top_k=5)\n",
"response = client.converse(prompt=prompt, context=context)\n",
"user_session.store(prompt, response)\n",
"user_history.store(prompt, response)\n",
"print('USER: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand Down Expand Up @@ -561,7 +561,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Semantic session memory"
"## Semantic message history"
]
},
{
Expand Down Expand Up @@ -608,10 +608,10 @@
],
"source": [
"prompt = \"Can you give me the avocado one?\"\n",
"user_session.set_distance_threshold(0.75)\n",
"context = user_session.get_relevant(prompt=prompt)\n",
"user_history.set_distance_threshold(0.75)\n",
"context = user_history.get_relevant(prompt=prompt)\n",
"response = client.converse(prompt=prompt, context=context)\n",
"user_session.store(prompt, response)\n",
"user_history.store(prompt, response)\n",
"print('USER: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand Down Expand Up @@ -648,7 +648,7 @@
"metadata": {},
"outputs": [],
"source": [
"user_session.clear()"
"user_history.clear()"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"source": [
"![Redis](https://redis.io/wp-content/uploads/2024/04/Logotype.svg?auto=webp&quality=85,75&width=120)\n",
"\n",
"# LLM Session Memory - Multiple Sessions\n",
"# LLM Message History - Multiple Sessions\n",
"\n",
"Large Language Models are inherently stateless and have no knowledge of previous interactions with a user, or even of previous parts of the current conversation. The solution to this problem is to append the previous conversation history to each subsequent call to the LLM.\n",
"This notebook will show how to use Redis to structure and store and retrieve this conversational session memory and how to manage multiple sessions simultaneously.\n",
"This notebook will show how to use Redis to structure and store and retrieve this conversational message history and how to manage multiple conversation sessions simultaneously.\n",
"\n",
"## Let's Begin!\n",
"<a href=\"https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/session-manager/01_multiple_sessions.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
"<a href=\"https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/llm-message-history/01_multiple_sessions.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
]
},
{
Expand All @@ -28,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install cohere \"redisvl>=0.4.1\" sentence-transformers"
"%pip install cohere \"redisvl>=0.6.0\" sentence-transformers"
]
},
{
Expand Down Expand Up @@ -150,7 +150,7 @@
" return response.text\n",
"\n",
" def remap(self, context) -> List[Dict]:\n",
" ''' re-index the chat history to match the Cohere API requirements '''\n",
" ''' re-index the message history to match the Cohere API requirements '''\n",
" new_context = []\n",
" for statement in context:\n",
" if statement[\"role\"] == \"user\":\n",
Expand All @@ -171,9 +171,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import SemanticSessionManager\n",
"### Import SemanticMessageHistory\n",
"\n",
"redisvl provides the SemanticSessionManager for easy management of session state.\n",
"redisvl provides the SemanticMessageHistory for easy management of conversational message history state.\n",
"It also allows for tagging of messages to separate conversation sessions with the `session_tag` optional parameter.\n",
"Let's create a few personas that can talk to our AI.\n"
]
Expand All @@ -195,16 +195,16 @@
"metadata": {},
"outputs": [],
"source": [
"from redisvl.extensions.session_manager import SemanticSessionManager\n",
"from redisvl.extensions.message_history import SemanticMessageHistory\n",
"\n",
"session = SemanticSessionManager(name='budgeting help')"
"history = SemanticMessageHistory(name='budgeting help')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Here we'll have multiple separate conversations simultaneously, all using the same session manager.\n",
"#### Here we'll have multiple separate conversations simultaneously, all using the same message history object.\n",
"#### Let's add some conversation history to get started.\n",
"\n",
"#### We'll assign each message to one of our users with their own `session_tag`."
Expand All @@ -217,7 +217,7 @@
"outputs": [],
"source": [
"# adding messages to the student session\n",
"session.add_messages(\n",
"history.add_messages(\n",
" [{\"role\":\"system\",\n",
" \"content\":\"You are a personal assistant helping people create sound financial budgets. Be very brief and concise in your responses.\"},\n",
" {\"role\":\"user\",\n",
Expand All @@ -230,7 +230,7 @@
" session_tag=student)\n",
"\n",
"#adding messages to the young professional session\n",
"session.add_messages(\n",
"history.add_messages(\n",
" [{\"role\":\"system\",\n",
" \"content\":\"You are a personal assistant helping people create sound financial budgets. Be very brief and concise in your responses.\"},\n",
" {\"role\":\"user\",\n",
Expand All @@ -243,7 +243,7 @@
" session_tag=yp)\n",
"\n",
"#adding messages to the retiree session\n",
"session.add_messages(\n",
"history.add_messages(\n",
" [{\"role\":\"system\",\n",
" \"content\":\"You are a personal assistant helping people create sound financial budgets. Be very brief and concise in your responses.\"},\n",
" {\"role\":\"user\",\n",
Expand All @@ -260,7 +260,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### With the same session manager calling the same LLM we can handle distinct conversations. There's no need to instantiate separate classes or clients.\n",
"#### With the same message history instance and calling the same LLM we can handle distinct conversations. There's no need to instantiate separate classes or clients.\n",
"\n",
"#### Just retrieve the conversation of interest using the same `session_tag` parameter when fetching context."
]
Expand All @@ -282,9 +282,9 @@
],
"source": [
"prompt = \"What is the single most important thing I should focus on financially?\"\n",
"context = session.get_recent(session_tag=student)\n",
"context = history.get_recent(session_tag=student)\n",
"response = client.converse(prompt=prompt, context=context)\n",
"session.store(prompt, response, session_tag=student)\n",
"history.store(prompt, response, session_tag=student)\n",
"print('Student: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand All @@ -306,9 +306,9 @@
],
"source": [
"prompt = \"What is the single most important thing I should focus on financially?\"\n",
"context = session.get_recent(session_tag=yp)\n",
"context = history.get_recent(session_tag=yp)\n",
"response = client.converse(prompt=prompt, context=context)\n",
"session.store(prompt, response, session_tag=yp)\n",
"history.store(prompt, response, session_tag=yp)\n",
"print('Young Professional: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand All @@ -330,9 +330,9 @@
],
"source": [
"prompt = \"What is the single most important thing I should focus on financially?\"\n",
"context = session.get_recent(session_tag=retired)\n",
"context = history.get_recent(session_tag=retired)\n",
"response = client.converse(prompt=prompt, context=context)\n",
"session.store(prompt, response, session_tag=retired)\n",
"history.store(prompt, response, session_tag=retired)\n",
"print('Retiree: ', prompt)\n",
"print('\\nLLM: ', response)"
]
Expand Down Expand Up @@ -362,7 +362,7 @@
}
],
"source": [
"for ctx in session.get_recent(session_tag=student):\n",
"for ctx in history.get_recent(session_tag=student):\n",
" print(ctx)"
]
},
Expand All @@ -372,7 +372,7 @@
"metadata": {},
"outputs": [],
"source": [
"session.clear()"
"history.clear()"
]
}
],
Expand Down
Loading