mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
Bagatur/zep python 1.0 (#9186)
Co-authored-by: Daniel Chalef <131175+danielchalef@users.noreply.github.com>
This commit is contained in:
parent
9b24f0b067
commit
0fa69d8988
@ -44,21 +44,20 @@
|
|||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2023-05-25T15:03:27.863217Z",
|
|
||||||
"start_time": "2023-05-25T15:03:25.690273Z"
|
|
||||||
},
|
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
"jupyter": {
|
"ExecuteTime": {
|
||||||
"outputs_hidden": false
|
"end_time": "2023-08-11T20:31:12.231459Z",
|
||||||
|
"start_time": "2023-08-11T20:31:11.211176Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.memory.chat_message_histories import ZepChatMessageHistory\n",
|
|
||||||
"from langchain.schema import HumanMessage, AIMessage\n",
|
|
||||||
"from uuid import uuid4\n",
|
|
||||||
"import getpass\n",
|
"import getpass\n",
|
||||||
|
"import time\n",
|
||||||
|
"from uuid import uuid4\n",
|
||||||
|
"\n",
|
||||||
|
"from langchain.memory import ZepMemory, CombinedMemory, VectorStoreRetrieverMemory\n",
|
||||||
|
"from langchain.schema import HumanMessage, AIMessage\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Set this to your Zep server URL\n",
|
"# Set this to your Zep server URL\n",
|
||||||
"ZEP_API_URL = \"http://localhost:8000\""
|
"ZEP_API_URL = \"http://localhost:8000\""
|
||||||
@ -76,19 +75,19 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
"outputs": [
|
"ExecuteTime": {
|
||||||
{
|
"end_time": "2023-08-11T20:31:12.237545Z",
|
||||||
"name": "stdin",
|
"start_time": "2023-08-11T20:31:12.232416Z"
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
" ········\n"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Provide your Zep API key. Note that this is optional. See https://docs.getzep.com/deployment/auth\n",
|
"# Provide your Zep API key. Note that this is optional. See https://docs.getzep.com/deployment/auth\n",
|
||||||
|
"AUTHENTICATE = False\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"zep_api_key = None\n",
|
||||||
|
"if AUTHENTICATE:\n",
|
||||||
" zep_api_key = getpass.getpass()"
|
" zep_api_key = getpass.getpass()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -96,36 +95,30 @@
|
|||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2023-05-25T15:03:29.118416Z",
|
|
||||||
"start_time": "2023-05-25T15:03:29.022464Z"
|
|
||||||
},
|
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
"jupyter": {
|
"ExecuteTime": {
|
||||||
"outputs_hidden": false
|
"end_time": "2023-08-11T20:31:12.342790Z",
|
||||||
|
"start_time": "2023-08-11T20:31:12.235291Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"session_id = str(uuid4()) # This is a unique identifier for the user/session\n",
|
"session_id = str(uuid4()) # This is a unique identifier for the user/session\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Set up Zep Chat History. We'll use this to add chat histories to the memory store\n",
|
"# Initialize the Zep Memory Class\n",
|
||||||
"zep_chat_history = ZepChatMessageHistory(\n",
|
"zep_memory = ZepMemory(\n",
|
||||||
" session_id=session_id, url=ZEP_API_URL, api_key=zep_api_key\n",
|
" session_id=session_id, url=ZEP_API_URL, api_key=zep_api_key\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2023-05-25T15:03:30.271181Z",
|
|
||||||
"start_time": "2023-05-25T15:03:30.180442Z"
|
|
||||||
},
|
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
"jupyter": {
|
"ExecuteTime": {
|
||||||
"outputs_hidden": false
|
"end_time": "2023-08-11T20:31:14.455269Z",
|
||||||
|
"start_time": "2023-08-11T20:31:12.345635Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -191,11 +184,13 @@
|
|||||||
"]\n",
|
"]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for msg in test_history:\n",
|
"for msg in test_history:\n",
|
||||||
" zep_chat_history.add_message(\n",
|
" zep_memory.chat_memory.add_message(\n",
|
||||||
" HumanMessage(content=msg[\"content\"])\n",
|
" HumanMessage(content=msg[\"content\"])\n",
|
||||||
" if msg[\"role\"] == \"human\"\n",
|
" if msg[\"role\"] == \"human\"\n",
|
||||||
" else AIMessage(content=msg[\"content\"])\n",
|
" else AIMessage(content=msg[\"content\"])\n",
|
||||||
" )"
|
" )\n",
|
||||||
|
" \n",
|
||||||
|
"time.sleep(2) # Wait for the messages to be embedded"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -211,29 +206,20 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2023-05-25T15:03:32.979155Z",
|
|
||||||
"start_time": "2023-05-25T15:03:32.590310Z"
|
|
||||||
},
|
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
"jupyter": {
|
"ExecuteTime": {
|
||||||
"outputs_hidden": false
|
"end_time": "2023-08-11T20:31:14.758738Z",
|
||||||
|
"start_time": "2023-08-11T20:31:14.458850Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": "[Document(page_content='Who was Octavia Butler?', metadata={'score': 0.7758688965570713, 'uuid': 'b3322d28-f589-48c7-9daf-5eb092d65976', 'created_at': '2023-08-11T20:31:12.3856Z', 'role': 'human', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 22, 'Start': 8, 'Text': 'Octavia Butler'}], 'Name': 'Octavia Butler'}]}}, 'token_count': 8}),\n Document(page_content=\"Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.\", metadata={'score': 0.7602672137411663, 'uuid': '756b7136-0b4c-4664-ad33-c4431670356c', 'created_at': '2023-08-11T20:31:12.420717Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 16, 'Start': 0, 'Text': \"Octavia Butler's\"}], 'Name': \"Octavia Butler's\"}, {'Label': 'ORG', 'Matches': [{'End': 58, 'Start': 41, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 76, 'Start': 60, 'Text': 'Samuel R. Delany'}], 'Name': 'Samuel R. Delany'}, {'Label': 'PERSON', 'Matches': [{'End': 93, 'Start': 82, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}]}}, 'token_count': 27}),\n Document(page_content='You might want to read Ursula K. Le Guin or Joanna Russ.', metadata={'score': 0.7596040989115522, 'uuid': '166d9556-2d48-4237-8a84-5d8a1024d5f4', 'created_at': '2023-08-11T20:31:12.434522Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'ORG', 'Matches': [{'End': 40, 'Start': 23, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 55, 'Start': 44, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}]}}, 'token_count': 18}),\n Document(page_content='Who were her contemporaries?', metadata={'score': 0.7575531381951208, 'uuid': 'c6a16691-4012-439f-b223-84fd4e79c4cf', 'created_at': '2023-08-11T20:31:12.410336Z', 'role': 'human', 'token_count': 8}),\n Document(page_content='Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American science fiction author.', metadata={'score': 0.7546476914454683, 'uuid': '7c093a2a-0099-415a-95c5-615a8026a894', 'created_at': '2023-08-11T20:31:12.399979Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 22, 'Start': 0, 'Text': 'Octavia Estelle Butler'}], 'Name': 'Octavia Estelle Butler'}, {'Label': 'DATE', 'Matches': [{'End': 37, 'Start': 24, 'Text': 'June 22, 1947'}], 'Name': 'June 22, 1947'}, {'Label': 'DATE', 'Matches': [{'End': 57, 'Start': 40, 'Text': 'February 24, 2006'}], 'Name': 'February 24, 2006'}, {'Label': 'NORP', 'Matches': [{'End': 74, 'Start': 66, 'Text': 'American'}], 'Name': 'American'}]}}, 'token_count': 31})]"
|
||||||
"[Document(page_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.', metadata={'score': 0.8897116216176073, 'uuid': 'db60ff57-f259-4ec4-8a81-178ed4c6e54f', 'created_at': '2023-06-26T23:40:22.816214Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'GPE', 'Matches': [{'End': 20, 'Start': 15, 'Text': 'Sower'}], 'Name': 'Sower'}, {'Label': 'PERSON', 'Matches': [{'End': 65, 'Start': 51, 'Text': 'Octavia Butler'}], 'Name': 'Octavia Butler'}, {'Label': 'DATE', 'Matches': [{'End': 84, 'Start': 80, 'Text': '1993'}], 'Name': '1993'}, {'Label': 'PERSON', 'Matches': [{'End': 124, 'Start': 110, 'Text': 'Lauren Olamina'}], 'Name': 'Lauren Olamina'}]}}, 'token_count': 56}),\n",
|
|
||||||
" Document(page_content=\"Write a short synopsis of Butler's book, Parable of the Sower. What is it about?\", metadata={'score': 0.8856661080361157, 'uuid': 'f1a5981a-8f6d-4168-a548-6e9c32f35fa1', 'created_at': '2023-06-26T23:40:22.809621Z', 'role': 'human', 'metadata': {'system': {'entities': [{'Label': 'ORG', 'Matches': [{'End': 32, 'Start': 26, 'Text': 'Butler'}], 'Name': 'Butler'}, {'Label': 'WORK_OF_ART', 'Matches': [{'End': 61, 'Start': 41, 'Text': 'Parable of the Sower'}], 'Name': 'Parable of the Sower'}]}}, 'token_count': 23}),\n",
|
|
||||||
" Document(page_content='Who was Octavia Butler?', metadata={'score': 0.7757595298492976, 'uuid': '361d0043-1009-4e13-a7f0-8aea8b1ee869', 'created_at': '2023-06-26T23:40:22.709886Z', 'role': 'human', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 22, 'Start': 8, 'Text': 'Octavia Butler'}], 'Name': 'Octavia Butler'}], 'intent': 'The subject wants to know about the identity or background of an individual named Octavia Butler.'}}, 'token_count': 8}),\n",
|
|
||||||
" Document(page_content=\"Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.\", metadata={'score': 0.7601242516059306, 'uuid': '56c45e8a-0f65-45f0-bc46-d9e65164b563', 'created_at': '2023-06-26T23:40:22.778836Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 16, 'Start': 0, 'Text': \"Octavia Butler's\"}], 'Name': \"Octavia Butler's\"}, {'Label': 'ORG', 'Matches': [{'End': 58, 'Start': 41, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 76, 'Start': 60, 'Text': 'Samuel R. Delany'}], 'Name': 'Samuel R. Delany'}, {'Label': 'PERSON', 'Matches': [{'End': 93, 'Start': 82, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}], 'intent': \"The subject is providing information about Octavia Butler's contemporaries.\"}}, 'token_count': 27}),\n",
|
|
||||||
" Document(page_content='You might want to read Ursula K. Le Guin or Joanna Russ.', metadata={'score': 0.7594731095320668, 'uuid': '6951f2fd-dfa4-4e05-9380-f322ef8f72f8', 'created_at': '2023-06-26T23:40:22.80464Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'ORG', 'Matches': [{'End': 40, 'Start': 23, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 55, 'Start': 44, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}]}}, 'token_count': 18})]"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"execution_count": 6,
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -260,29 +246,20 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 6,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2023-05-25T15:03:34.713354Z",
|
|
||||||
"start_time": "2023-05-25T15:03:34.577974Z"
|
|
||||||
},
|
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
"jupyter": {
|
"ExecuteTime": {
|
||||||
"outputs_hidden": false
|
"end_time": "2023-08-11T20:31:14.922838Z",
|
||||||
|
"start_time": "2023-08-11T20:31:14.751737Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": "[Document(page_content=\"Write a short synopsis of Butler's book, Parable of the Sower. What is it about?\", metadata={'score': 0.8857504413268114, 'uuid': '82f07ab5-9d4b-4db6-aaae-6028e6fd836b', 'created_at': '2023-08-11T20:31:12.437365Z', 'role': 'human', 'metadata': {'system': {'entities': [{'Label': 'ORG', 'Matches': [{'End': 32, 'Start': 26, 'Text': 'Butler'}], 'Name': 'Butler'}, {'Label': 'WORK_OF_ART', 'Matches': [{'End': 61, 'Start': 41, 'Text': 'Parable of the Sower'}], 'Name': 'Parable of the Sower'}]}}, 'token_count': 23}),\n Document(page_content='Who was Octavia Butler?', metadata={'score': 0.7758688965570713, 'uuid': 'b3322d28-f589-48c7-9daf-5eb092d65976', 'created_at': '2023-08-11T20:31:12.3856Z', 'role': 'human', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 22, 'Start': 8, 'Text': 'Octavia Butler'}], 'Name': 'Octavia Butler'}]}}, 'token_count': 8}),\n Document(page_content=\"Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.\", metadata={'score': 0.7602672137411663, 'uuid': '756b7136-0b4c-4664-ad33-c4431670356c', 'created_at': '2023-08-11T20:31:12.420717Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 16, 'Start': 0, 'Text': \"Octavia Butler's\"}], 'Name': \"Octavia Butler's\"}, {'Label': 'ORG', 'Matches': [{'End': 58, 'Start': 41, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 76, 'Start': 60, 'Text': 'Samuel R. Delany'}], 'Name': 'Samuel R. Delany'}, {'Label': 'PERSON', 'Matches': [{'End': 93, 'Start': 82, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}]}}, 'token_count': 27}),\n Document(page_content='You might want to read Ursula K. Le Guin or Joanna Russ.', metadata={'score': 0.7596040989115522, 'uuid': '166d9556-2d48-4237-8a84-5d8a1024d5f4', 'created_at': '2023-08-11T20:31:12.434522Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'ORG', 'Matches': [{'End': 40, 'Start': 23, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 55, 'Start': 44, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}]}}, 'token_count': 18}),\n Document(page_content='Who were her contemporaries?', metadata={'score': 0.7575531381951208, 'uuid': 'c6a16691-4012-439f-b223-84fd4e79c4cf', 'created_at': '2023-08-11T20:31:12.410336Z', 'role': 'human', 'token_count': 8})]"
|
||||||
"[Document(page_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.', metadata={'score': 0.889661105796371, 'uuid': 'db60ff57-f259-4ec4-8a81-178ed4c6e54f', 'created_at': '2023-06-26T23:40:22.816214Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'GPE', 'Matches': [{'End': 20, 'Start': 15, 'Text': 'Sower'}], 'Name': 'Sower'}, {'Label': 'PERSON', 'Matches': [{'End': 65, 'Start': 51, 'Text': 'Octavia Butler'}], 'Name': 'Octavia Butler'}, {'Label': 'DATE', 'Matches': [{'End': 84, 'Start': 80, 'Text': '1993'}], 'Name': '1993'}, {'Label': 'PERSON', 'Matches': [{'End': 124, 'Start': 110, 'Text': 'Lauren Olamina'}], 'Name': 'Lauren Olamina'}]}}, 'token_count': 56}),\n",
|
|
||||||
" Document(page_content=\"Write a short synopsis of Butler's book, Parable of the Sower. What is it about?\", metadata={'score': 0.885754241595424, 'uuid': 'f1a5981a-8f6d-4168-a548-6e9c32f35fa1', 'created_at': '2023-06-26T23:40:22.809621Z', 'role': 'human', 'metadata': {'system': {'entities': [{'Label': 'ORG', 'Matches': [{'End': 32, 'Start': 26, 'Text': 'Butler'}], 'Name': 'Butler'}, {'Label': 'WORK_OF_ART', 'Matches': [{'End': 61, 'Start': 41, 'Text': 'Parable of the Sower'}], 'Name': 'Parable of the Sower'}]}}, 'token_count': 23}),\n",
|
|
||||||
" Document(page_content='Who was Octavia Butler?', metadata={'score': 0.7758688965570713, 'uuid': '361d0043-1009-4e13-a7f0-8aea8b1ee869', 'created_at': '2023-06-26T23:40:22.709886Z', 'role': 'human', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 22, 'Start': 8, 'Text': 'Octavia Butler'}], 'Name': 'Octavia Butler'}], 'intent': 'The subject wants to know about the identity or background of an individual named Octavia Butler.'}}, 'token_count': 8}),\n",
|
|
||||||
" Document(page_content=\"Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.\", metadata={'score': 0.7602672137411663, 'uuid': '56c45e8a-0f65-45f0-bc46-d9e65164b563', 'created_at': '2023-06-26T23:40:22.778836Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'PERSON', 'Matches': [{'End': 16, 'Start': 0, 'Text': \"Octavia Butler's\"}], 'Name': \"Octavia Butler's\"}, {'Label': 'ORG', 'Matches': [{'End': 58, 'Start': 41, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 76, 'Start': 60, 'Text': 'Samuel R. Delany'}], 'Name': 'Samuel R. Delany'}, {'Label': 'PERSON', 'Matches': [{'End': 93, 'Start': 82, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}], 'intent': \"The subject is providing information about Octavia Butler's contemporaries.\"}}, 'token_count': 27}),\n",
|
|
||||||
" Document(page_content='You might want to read Ursula K. Le Guin or Joanna Russ.', metadata={'score': 0.7596040989115522, 'uuid': '6951f2fd-dfa4-4e05-9380-f322ef8f72f8', 'created_at': '2023-06-26T23:40:22.80464Z', 'role': 'ai', 'metadata': {'system': {'entities': [{'Label': 'ORG', 'Matches': [{'End': 40, 'Start': 23, 'Text': 'Ursula K. Le Guin'}], 'Name': 'Ursula K. Le Guin'}, {'Label': 'PERSON', 'Matches': [{'End': 55, 'Start': 44, 'Text': 'Joanna Russ'}], 'Name': 'Joanna Russ'}]}}, 'token_count': 18})]"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"execution_count": 7,
|
"execution_count": 6,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -293,19 +270,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 6,
|
||||||
"metadata": {
|
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2023-05-18T20:09:21.298710Z",
|
|
||||||
"start_time": "2023-05-18T20:09:21.297169Z"
|
|
||||||
},
|
|
||||||
"collapsed": false,
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": []
|
"source": [],
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false,
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2023-08-11T20:31:14.923032Z",
|
||||||
|
"start_time": "2023-08-11T20:31:14.918181Z"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
@ -124,7 +124,7 @@ class ZepChatMessageHistory(BaseChatMessageHistory):
|
|||||||
from zep_python import NotFoundError
|
from zep_python import NotFoundError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
zep_memory: Memory = self.zep_client.get_memory(self.session_id)
|
zep_memory: Memory = self.zep_client.memory.get_memory(self.session_id)
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"Session {self.session_id} not found in Zep. Returning None"
|
f"Session {self.session_id} not found in Zep. Returning None"
|
||||||
@ -165,7 +165,7 @@ class ZepChatMessageHistory(BaseChatMessageHistory):
|
|||||||
)
|
)
|
||||||
zep_memory = Memory(messages=[zep_message])
|
zep_memory = Memory(messages=[zep_message])
|
||||||
|
|
||||||
self.zep_client.add_memory(self.session_id, zep_memory)
|
self.zep_client.memory.add_memory(self.session_id, zep_memory)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, query: str, metadata: Optional[Dict] = None, limit: Optional[int] = None
|
self, query: str, metadata: Optional[Dict] = None, limit: Optional[int] = None
|
||||||
@ -177,14 +177,16 @@ class ZepChatMessageHistory(BaseChatMessageHistory):
|
|||||||
text=query, metadata=metadata
|
text=query, metadata=metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.zep_client.search_memory(self.session_id, payload, limit=limit)
|
return self.zep_client.memory.search_memory(
|
||||||
|
self.session_id, payload, limit=limit
|
||||||
|
)
|
||||||
|
|
||||||
def clear(self) -> None:
|
def clear(self) -> None:
|
||||||
"""Clear session memory from Zep. Note that Zep is long-term storage for memory
|
"""Clear session memory from Zep. Note that Zep is long-term storage for memory
|
||||||
and this is not advised unless you have specific data retention requirements.
|
and this is not advised unless you have specific data retention requirements.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.zep_client.delete_memory(self.session_id)
|
self.zep_client.memory.delete_memory(self.session_id)
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"Session {self.session_id} not found in Zep. Skipping delete."
|
f"Session {self.session_id} not found in Zep. Skipping delete."
|
||||||
|
@ -77,7 +77,7 @@ class ZepRetriever(BaseRetriever):
|
|||||||
text=query, metadata=metadata
|
text=query, metadata=metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
results: List[MemorySearchResult] = self.zep_client.search_memory(
|
results: List[MemorySearchResult] = self.zep_client.memory.search_memory(
|
||||||
self.session_id, payload, limit=self.top_k
|
self.session_id, payload, limit=self.top_k
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ class ZepRetriever(BaseRetriever):
|
|||||||
text=query, metadata=metadata
|
text=query, metadata=metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
results: List[MemorySearchResult] = await self.zep_client.asearch_memory(
|
results: List[MemorySearchResult] = await self.zep_client.memory.asearch_memory(
|
||||||
self.session_id, payload, limit=self.top_k
|
self.session_id, payload, limit=self.top_k
|
||||||
)
|
)
|
||||||
|
|
||||||
|
10
libs/langchain/poetry.lock
generated
10
libs/langchain/poetry.lock
generated
@ -13215,14 +13215,14 @@ multidict = ">=4.0"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zep-python"
|
name = "zep-python"
|
||||||
version = "0.32"
|
version = "1.0.2"
|
||||||
description = "Zep stores, manages, enriches, indexes, and searches long-term memory for conversational AI applications. This is the Python client for the Zep service."
|
description = "Zep stores, manages, enriches, indexes, and searches long-term memory for conversational AI applications. This is the Python client for the Zep service."
|
||||||
category = "main"
|
category = "main"
|
||||||
optional = true
|
optional = true
|
||||||
python-versions = ">=3.8,<4.0"
|
python-versions = ">=3.8.1,<4"
|
||||||
files = [
|
files = [
|
||||||
{file = "zep_python-0.32-py3-none-any.whl", hash = "sha256:5b4e54851c28742e869b8a66928b16618ff0074375428791c7bc4221c2744085"},
|
{file = "zep_python-1.0.2-py3-none-any.whl", hash = "sha256:67786a9f81fd2860cddb603150df49d6875b58d102e7a93152ae14fb00df3be4"},
|
||||||
{file = "zep_python-0.32.tar.gz", hash = "sha256:7320b36069b4063119fef0f90a7f75fabf0d6e77083abf5f187b5d241ce1576b"},
|
{file = "zep_python-1.0.2.tar.gz", hash = "sha256:27a0330a1253e8dccd3e14732218d7e053d6348e66a55d17bcebf361070e52d8"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@ -13321,4 +13321,4 @@ text-helpers = ["chardet"]
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = ">=3.8.1,<4.0"
|
python-versions = ">=3.8.1,<4.0"
|
||||||
content-hash = "58a4fbe647c8c266789b2bd5e4792a9514b8197e580dd6c6d9dc3ed374144a2b"
|
content-hash = "b61889380aec6007b6fcf7f823bf9b783177f2641c8efa0bc29b547b481c5b02"
|
||||||
|
@ -91,7 +91,7 @@ gql = {version = "^3.4.1", optional = true}
|
|||||||
pandas = {version = "^2.0.1", optional = true}
|
pandas = {version = "^2.0.1", optional = true}
|
||||||
telethon = {version = "^1.28.5", optional = true}
|
telethon = {version = "^1.28.5", optional = true}
|
||||||
neo4j = {version = "^5.8.1", optional = true}
|
neo4j = {version = "^5.8.1", optional = true}
|
||||||
zep-python = {version=">=0.32", optional=true}
|
zep-python = {version = ">=1.0.1,<1.1.0", optional = true}
|
||||||
langkit = {version = ">=0.0.6, <0.1.0", optional = true}
|
langkit = {version = ">=0.0.6, <0.1.0", optional = true}
|
||||||
chardet = {version="^5.1.0", optional=true}
|
chardet = {version="^5.1.0", optional=true}
|
||||||
requests-toolbelt = {version = "^1.0.0", optional = true}
|
requests-toolbelt = {version = "^1.0.0", optional = true}
|
||||||
|
@ -14,6 +14,9 @@ if TYPE_CHECKING:
|
|||||||
@pytest.mark.requires("zep_python")
|
@pytest.mark.requires("zep_python")
|
||||||
def zep_chat(mocker: MockerFixture) -> ZepChatMessageHistory:
|
def zep_chat(mocker: MockerFixture) -> ZepChatMessageHistory:
|
||||||
mock_zep_client: ZepClient = mocker.patch("zep_python.ZepClient", autospec=True)
|
mock_zep_client: ZepClient = mocker.patch("zep_python.ZepClient", autospec=True)
|
||||||
|
mock_zep_client.memory = mocker.patch(
|
||||||
|
"zep_python.memory.client.MemoryClient", autospec=True
|
||||||
|
)
|
||||||
zep_chat: ZepChatMessageHistory = ZepChatMessageHistory(
|
zep_chat: ZepChatMessageHistory = ZepChatMessageHistory(
|
||||||
"test_session", "http://localhost:8000"
|
"test_session", "http://localhost:8000"
|
||||||
)
|
)
|
||||||
@ -34,7 +37,7 @@ def test_messages(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> Non
|
|||||||
Message(content="message2", role="human", metadata={"key2": "value2"}),
|
Message(content="message2", role="human", metadata={"key2": "value2"}),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
zep_chat.zep_client.get_memory.return_value = mock_memory # type: ignore
|
zep_chat.zep_client.memory.get_memory.return_value = mock_memory # type: ignore
|
||||||
|
|
||||||
result = zep_chat.messages
|
result = zep_chat.messages
|
||||||
|
|
||||||
@ -49,25 +52,25 @@ def test_add_user_message(
|
|||||||
mocker: MockerFixture, zep_chat: ZepChatMessageHistory
|
mocker: MockerFixture, zep_chat: ZepChatMessageHistory
|
||||||
) -> None:
|
) -> None:
|
||||||
zep_chat.add_user_message("test message")
|
zep_chat.add_user_message("test message")
|
||||||
zep_chat.zep_client.add_memory.assert_called_once() # type: ignore
|
zep_chat.zep_client.memory.add_memory.assert_called_once() # type: ignore
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.requires("zep_python")
|
@pytest.mark.requires("zep_python")
|
||||||
def test_add_ai_message(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
def test_add_ai_message(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
||||||
zep_chat.add_ai_message("test message")
|
zep_chat.add_ai_message("test message")
|
||||||
zep_chat.zep_client.add_memory.assert_called_once() # type: ignore
|
zep_chat.zep_client.memory.add_memory.assert_called_once() # type: ignore
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.requires("zep_python")
|
@pytest.mark.requires("zep_python")
|
||||||
def test_append(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
def test_append(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
||||||
zep_chat.add_message(AIMessage(content="test message"))
|
zep_chat.add_message(AIMessage(content="test message"))
|
||||||
zep_chat.zep_client.add_memory.assert_called_once() # type: ignore
|
zep_chat.zep_client.memory.add_memory.assert_called_once() # type: ignore
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.requires("zep_python")
|
@pytest.mark.requires("zep_python")
|
||||||
def test_search(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
def test_search(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
||||||
zep_chat.search("test query")
|
zep_chat.search("test query")
|
||||||
zep_chat.zep_client.search_memory.assert_called_once_with( # type: ignore
|
zep_chat.zep_client.memory.search_memory.assert_called_once_with( # type: ignore
|
||||||
"test_session", mocker.ANY, limit=None
|
"test_session", mocker.ANY, limit=None
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -75,6 +78,6 @@ def test_search(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
|||||||
@pytest.mark.requires("zep_python")
|
@pytest.mark.requires("zep_python")
|
||||||
def test_clear(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
def test_clear(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) -> None:
|
||||||
zep_chat.clear()
|
zep_chat.clear()
|
||||||
zep_chat.zep_client.delete_memory.assert_called_once_with( # type: ignore
|
zep_chat.zep_client.memory.delete_memory.assert_called_once_with( # type: ignore
|
||||||
"test_session"
|
"test_session"
|
||||||
)
|
)
|
||||||
|
@ -58,10 +58,13 @@ def zep_retriever(
|
|||||||
mocker: MockerFixture, search_results: List[MemorySearchResult]
|
mocker: MockerFixture, search_results: List[MemorySearchResult]
|
||||||
) -> ZepRetriever:
|
) -> ZepRetriever:
|
||||||
mock_zep_client: ZepClient = mocker.patch("zep_python.ZepClient", autospec=True)
|
mock_zep_client: ZepClient = mocker.patch("zep_python.ZepClient", autospec=True)
|
||||||
mock_zep_client.search_memory.return_value = copy.deepcopy( # type: ignore
|
mock_zep_client.memory = mocker.patch(
|
||||||
|
"zep_python.memory.client.MemoryClient", autospec=True
|
||||||
|
)
|
||||||
|
mock_zep_client.memory.search_memory.return_value = copy.deepcopy( # type: ignore
|
||||||
search_results
|
search_results
|
||||||
)
|
)
|
||||||
mock_zep_client.asearch_memory.return_value = copy.deepcopy( # type: ignore
|
mock_zep_client.memory.asearch_memory.return_value = copy.deepcopy( # type: ignore
|
||||||
search_results
|
search_results
|
||||||
)
|
)
|
||||||
zep = ZepRetriever(session_id="123", url="http://localhost:8000")
|
zep = ZepRetriever(session_id="123", url="http://localhost:8000")
|
||||||
|
Loading…
Reference in New Issue
Block a user