forked from Archives/langchain
c0c2fd0782
Co-authored-by: Daniel Chalef <131175+danielchalef@users.noreply.github.com>
390 lines
16 KiB
Plaintext
390 lines
16 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"attachments": {},
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Zep Memory\n",
|
||
"\n",
|
||
"## REACT Agent Chat Message History with Zep - A long-term memory store for LLM applications.\n",
|
||
"\n",
|
||
"This notebook demonstrates how to use the [Zep Long-term Memory Store](https://docs.getzep.com/) as memory for your chatbot.\n",
|
||
"\n",
|
||
"We'll demonstrate:\n",
|
||
"\n",
|
||
"1. Adding conversation history to the Zep memory store.\n",
|
||
"2. Running an agent and having message automatically added to the store.\n",
|
||
"3. Viewing the enriched messages.\n",
|
||
"4. Vector search over the conversation history.\n",
|
||
"\n",
|
||
"### More on Zep:\n",
|
||
"\n",
|
||
"Zep stores, summarizes, embeds, indexes, and enriches conversational AI chat histories, and exposes them via simple, low-latency APIs.\n",
|
||
"\n",
|
||
"Key Features:\n",
|
||
"\n",
|
||
"- **Fast!** Zep’s async extractors operate independently of the your chat loop, ensuring a snappy user experience.\n",
|
||
"- **Long-term memory persistence**, with access to historical messages irrespective of your summarization strategy.\n",
|
||
"- **Auto-summarization** of memory messages based on a configurable message window. A series of summaries are stored, providing flexibility for future summarization strategies.\n",
|
||
"- **Hybrid search** over memories and metadata, with messages automatically embedded on creation.\n",
|
||
"- **Entity Extractor** that automatically extracts named entities from messages and stores them in the message metadata.\n",
|
||
"- **Auto-token counting** of memories and summaries, allowing finer-grained control over prompt assembly.\n",
|
||
"- Python and JavaScript SDKs.\n",
|
||
"\n",
|
||
"Zep project: [https://github.com/getzep/zep](https://github.com/getzep/zep)\n",
|
||
"Docs: [https://docs.getzep.com/](https://docs.getzep.com/)\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-05-25T15:09:41.754535Z",
|
||
"start_time": "2023-05-25T15:09:40.897232Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langchain.memory.chat_message_histories import ZepChatMessageHistory\n",
|
||
"from langchain.memory import ConversationBufferMemory\n",
|
||
"from langchain import OpenAI\n",
|
||
"from langchain.schema import HumanMessage, AIMessage\n",
|
||
"from langchain.tools import DuckDuckGoSearchRun\n",
|
||
"from langchain.agents import initialize_agent, AgentType\n",
|
||
"from uuid import uuid4\n",
|
||
"\n",
|
||
"\n",
|
||
"# Set this to your Zep server URL\n",
|
||
"ZEP_API_URL = \"http://localhost:8000\"\n",
|
||
"\n",
|
||
"session_id = str(uuid4()) # This is a unique identifier for the user"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-05-25T15:09:41.762056Z",
|
||
"start_time": "2023-05-25T15:09:41.755238Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": "True"
|
||
},
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Load your OpenAI key from a .env file\n",
|
||
"from dotenv import load_dotenv\n",
|
||
"\n",
|
||
"load_dotenv()"
|
||
]
|
||
},
|
||
{
|
||
"attachments": {},
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Initialize the Zep Chat Message History Class and initialize the Agent\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-05-25T15:09:41.840440Z",
|
||
"start_time": "2023-05-25T15:09:41.762277Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"ddg = DuckDuckGoSearchRun()\n",
|
||
"tools = [ddg]\n",
|
||
"\n",
|
||
"# Set up Zep Chat History\n",
|
||
"zep_chat_history = ZepChatMessageHistory(\n",
|
||
" session_id=session_id,\n",
|
||
" url=ZEP_API_URL,\n",
|
||
")\n",
|
||
"\n",
|
||
"# Use a standard ConversationBufferMemory to encapsulate the Zep chat history\n",
|
||
"memory = ConversationBufferMemory(\n",
|
||
" memory_key=\"chat_history\", chat_memory=zep_chat_history\n",
|
||
")\n",
|
||
"\n",
|
||
"# Initialize the agent\n",
|
||
"llm = OpenAI(temperature=0)\n",
|
||
"agent_chain = initialize_agent(\n",
|
||
" tools,\n",
|
||
" llm,\n",
|
||
" agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,\n",
|
||
" verbose=True,\n",
|
||
" memory=memory,\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"attachments": {},
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Add some history data\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-05-25T15:09:41.960661Z",
|
||
"start_time": "2023-05-25T15:09:41.842656Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Preload some messages into the memory. The default message window is 12 messages. We want to push beyond this to demonstrate auto-summarization.\n",
|
||
"test_history = [\n",
|
||
" {\"role\": \"human\", \"content\": \"Who was Octavia Butler?\"},\n",
|
||
" {\n",
|
||
" \"role\": \"ai\",\n",
|
||
" \"content\": (\n",
|
||
" \"Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American\"\n",
|
||
" \" science fiction author.\"\n",
|
||
" ),\n",
|
||
" },\n",
|
||
" {\"role\": \"human\", \"content\": \"Which books of hers were made into movies?\"},\n",
|
||
" {\n",
|
||
" \"role\": \"ai\",\n",
|
||
" \"content\": (\n",
|
||
" \"The most well-known adaptation of Octavia Butler's work is the FX series\"\n",
|
||
" \" Kindred, based on her novel of the same name.\"\n",
|
||
" ),\n",
|
||
" },\n",
|
||
" {\"role\": \"human\", \"content\": \"Who were her contemporaries?\"},\n",
|
||
" {\n",
|
||
" \"role\": \"ai\",\n",
|
||
" \"content\": (\n",
|
||
" \"Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R.\"\n",
|
||
" \" Delany, and Joanna Russ.\"\n",
|
||
" ),\n",
|
||
" },\n",
|
||
" {\"role\": \"human\", \"content\": \"What awards did she win?\"},\n",
|
||
" {\n",
|
||
" \"role\": \"ai\",\n",
|
||
" \"content\": (\n",
|
||
" \"Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur\"\n",
|
||
" \" Fellowship.\"\n",
|
||
" ),\n",
|
||
" },\n",
|
||
" {\n",
|
||
" \"role\": \"human\",\n",
|
||
" \"content\": \"Which other women sci-fi writers might I want to read?\",\n",
|
||
" },\n",
|
||
" {\n",
|
||
" \"role\": \"ai\",\n",
|
||
" \"content\": \"You might want to read Ursula K. Le Guin or Joanna Russ.\",\n",
|
||
" },\n",
|
||
" {\n",
|
||
" \"role\": \"human\",\n",
|
||
" \"content\": (\n",
|
||
" \"Write a short synopsis of Butler's book, Parable of the Sower. What is it\"\n",
|
||
" \" about?\"\n",
|
||
" ),\n",
|
||
" },\n",
|
||
" {\n",
|
||
" \"role\": \"ai\",\n",
|
||
" \"content\": (\n",
|
||
" \"Parable of the Sower is a science fiction novel by Octavia Butler,\"\n",
|
||
" \" published in 1993. It follows the story of Lauren Olamina, a young woman\"\n",
|
||
" \" living in a dystopian future where society has collapsed due to\"\n",
|
||
" \" environmental disasters, poverty, and violence.\"\n",
|
||
" ),\n",
|
||
" },\n",
|
||
"]\n",
|
||
"\n",
|
||
"for msg in test_history:\n",
|
||
" zep_chat_history.append(\n",
|
||
" HumanMessage(content=msg[\"content\"])\n",
|
||
" if msg[\"role\"] == \"human\"\n",
|
||
" else AIMessage(content=msg[\"content\"])\n",
|
||
" )"
|
||
]
|
||
},
|
||
{
|
||
"attachments": {},
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Run the agent\n",
|
||
"\n",
|
||
"Doing so will automatically add the input and response to the Zep memory.\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-05-25T15:09:50.485377Z",
|
||
"start_time": "2023-05-25T15:09:41.962287Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
"\u001b[32;1m\u001b[1;3mThought: Do I need to use a tool? No\n",
|
||
"AI: Parable of the Sower is a prescient novel that speaks to the challenges facing contemporary society, such as climate change, economic inequality, and the rise of authoritarianism. It is a cautionary tale that warns of the dangers of ignoring these issues and the importance of taking action to address them.\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": "'Parable of the Sower is a prescient novel that speaks to the challenges facing contemporary society, such as climate change, economic inequality, and the rise of authoritarianism. It is a cautionary tale that warns of the dangers of ignoring these issues and the importance of taking action to address them.'"
|
||
},
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"agent_chain.run(\n",
|
||
" input=\"WWhat is the book's relevance to the challenges facing contemporary society?\"\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"attachments": {},
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Inspect the Zep memory\n",
|
||
"\n",
|
||
"Note the summary, and that the history has been enriched with token counts, UUIDs, and timestamps.\n",
|
||
"\n",
|
||
"Summaries are biased towards the most recent messages.\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-05-25T15:09:50.493438Z",
|
||
"start_time": "2023-05-25T15:09:50.479230Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"The conversation is about Octavia Butler. The AI describes her as an American science fiction author and mentions the\n",
|
||
"FX series Kindred as a well-known adaptation of her work. The human then asks about her contemporaries, and the AI lists \n",
|
||
"Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.\n",
|
||
"\n",
|
||
"\n",
|
||
"{'role': 'human', 'content': 'What awards did she win?', 'uuid': '9fa75c3c-edae-41e3-b9bc-9fcf16b523c9', 'created_at': '2023-05-25T15:09:41.91662Z', 'token_count': 8}\n",
|
||
"{'role': 'ai', 'content': 'Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur Fellowship.', 'uuid': 'def4636c-32cb-49ed-b671-32035a034712', 'created_at': '2023-05-25T15:09:41.919874Z', 'token_count': 21}\n",
|
||
"{'role': 'human', 'content': 'Which other women sci-fi writers might I want to read?', 'uuid': '6e87bd4a-bc23-451e-ae36-05a140415270', 'created_at': '2023-05-25T15:09:41.923771Z', 'token_count': 14}\n",
|
||
"{'role': 'ai', 'content': 'You might want to read Ursula K. Le Guin or Joanna Russ.', 'uuid': 'f65d8dde-9ee8-4983-9da6-ba789b7e8aa4', 'created_at': '2023-05-25T15:09:41.935254Z', 'token_count': 18}\n",
|
||
"{'role': 'human', 'content': \"Write a short synopsis of Butler's book, Parable of the Sower. What is it about?\", 'uuid': '5678d056-7f05-4e70-b8e5-f85efa56db01', 'created_at': '2023-05-25T15:09:41.938974Z', 'token_count': 23}\n",
|
||
"{'role': 'ai', 'content': 'Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.', 'uuid': '50d64946-9239-4327-83e6-71dcbdd16198', 'created_at': '2023-05-25T15:09:41.957437Z', 'token_count': 56}\n",
|
||
"{'role': 'human', 'content': \"WWhat is the book's relevance to the challenges facing contemporary society?\", 'uuid': 'a39cfc07-8858-480a-9026-fc47a8ef7001', 'created_at': '2023-05-25T15:09:50.469533Z', 'token_count': 16}\n",
|
||
"{'role': 'ai', 'content': 'Parable of the Sower is a prescient novel that speaks to the challenges facing contemporary society, such as climate change, economic inequality, and the rise of authoritarianism. It is a cautionary tale that warns of the dangers of ignoring these issues and the importance of taking action to address them.', 'uuid': 'a4ecf0fe-fdd0-4aad-b72b-efde2e6830cc', 'created_at': '2023-05-25T15:09:50.473793Z', 'token_count': 62}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"def print_messages(messages):\n",
|
||
" for m in messages:\n",
|
||
" print(m.to_dict())\n",
|
||
"\n",
|
||
"\n",
|
||
"print(zep_chat_history.zep_summary)\n",
|
||
"print(\"\\n\")\n",
|
||
"print_messages(zep_chat_history.zep_messages)"
|
||
]
|
||
},
|
||
{
|
||
"attachments": {},
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Vector search over the Zep memory\n",
|
||
"\n",
|
||
"Zep provides native vector search over historical conversation memory. Embedding happens automatically.\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2023-05-25T15:09:50.751203Z",
|
||
"start_time": "2023-05-25T15:09:50.495050Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"{'uuid': '6e87bd4a-bc23-451e-ae36-05a140415270', 'created_at': '2023-05-25T15:09:41.923771Z', 'role': 'human', 'content': 'Which other women sci-fi writers might I want to read?', 'token_count': 14} 0.9118298949424545\n",
|
||
"{'uuid': 'f65d8dde-9ee8-4983-9da6-ba789b7e8aa4', 'created_at': '2023-05-25T15:09:41.935254Z', 'role': 'ai', 'content': 'You might want to read Ursula K. Le Guin or Joanna Russ.', 'token_count': 18} 0.8533024416448016\n",
|
||
"{'uuid': '52cfe3e8-b800-4dd8-a7dd-8e9e4764dfc8', 'created_at': '2023-05-25T15:09:41.913856Z', 'role': 'ai', 'content': \"Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.\", 'token_count': 27} 0.852352466457884\n",
|
||
"{'uuid': 'd40da612-0867-4a43-92ec-778b86490a39', 'created_at': '2023-05-25T15:09:41.858543Z', 'role': 'human', 'content': 'Who was Octavia Butler?', 'token_count': 8} 0.8235468913583194\n",
|
||
"{'uuid': '4fcfbce4-7bfa-44bd-879a-8cbf265bdcf9', 'created_at': '2023-05-25T15:09:41.893848Z', 'role': 'ai', 'content': 'Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American science fiction author.', 'token_count': 31} 0.8204317130595353\n",
|
||
"{'uuid': 'def4636c-32cb-49ed-b671-32035a034712', 'created_at': '2023-05-25T15:09:41.919874Z', 'role': 'ai', 'content': 'Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur Fellowship.', 'token_count': 21} 0.8196714827228725\n",
|
||
"{'uuid': '862107de-8f6f-43c0-91fa-4441f01b2b3a', 'created_at': '2023-05-25T15:09:41.898149Z', 'role': 'human', 'content': 'Which books of hers were made into movies?', 'token_count': 11} 0.7954322970428519\n",
|
||
"{'uuid': '97164506-90fe-4c71-9539-69ebcd1d90a2', 'created_at': '2023-05-25T15:09:41.90887Z', 'role': 'human', 'content': 'Who were her contemporaries?', 'token_count': 8} 0.7942531405021976\n",
|
||
"{'uuid': '50d64946-9239-4327-83e6-71dcbdd16198', 'created_at': '2023-05-25T15:09:41.957437Z', 'role': 'ai', 'content': 'Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.', 'token_count': 56} 0.78144769172694\n",
|
||
"{'uuid': 'c460ffd4-0715-4c69-b793-1092054973e6', 'created_at': '2023-05-25T15:09:41.903082Z', 'role': 'ai', 'content': \"The most well-known adaptation of Octavia Butler's work is the FX series Kindred, based on her novel of the same name.\", 'token_count': 29} 0.7811962820699464\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"search_results = zep_chat_history.search(\"who are some famous women sci-fi authors?\")\n",
|
||
"for r in search_results:\n",
|
||
" print(r.message, r.dist)"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": ".venv",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.11.3"
|
||
},
|
||
"orig_nbformat": 4
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|