Skip to content

Commit

Permalink
removed client history
Browse files Browse the repository at this point in the history
  • Loading branch information
spike-spiegel-21 committed Oct 20, 2024
1 parent ffc5197 commit 81ee252
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 87 deletions.
217 changes: 206 additions & 11 deletions docs/docs/examples/memory/Mem0ChatEngine.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
"outputs": [],
"source": [
"# Initializing memory\n",
"from llama_index.memory.mem0 import Mem0Composable\n",
"from llama_index.memory.mem0 import Mem0ComposableMemory\n",
"context_dict = {\n",
" \"user_id\": \"mayank_10\"\n",
" \"user_id\": \"david_1\"\n",
"}\n",
"memory = Mem0Composable.from_client(\n",
"memory = Mem0ComposableMemory.from_client(\n",
" context_dict=context_dict,\n",
" api_key=\"<your_api_key>\"\n",
" api_key=\"\"\n",
")"
]
},
Expand All @@ -41,7 +41,7 @@
"source": [
"# Initialize llm\n",
"import os\n",
"os.environ[\"OPENAI_API_KEY\"] = \"<your_api_key>\"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
"from llama_index.llms.openai import OpenAI\n",
"llm = OpenAI(model=\"gpt-4o\")"
]
Expand Down Expand Up @@ -69,7 +69,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Hello Mayank! How can I assist you today?\n"
"Hello, Mayank! How can I assist you today?\n"
]
}
],
Expand All @@ -87,7 +87,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"That sounds exciting! San Francisco has a lot to offer. Do you have any specific places or activities in mind that you’d like to explore while you’re there?\n"
"That sounds exciting! San Francisco has a lot to offer. Do you have any specific places or activities in mind that you’d like to explore, or would you like some recommendations?\n"
]
}
],
Expand All @@ -105,7 +105,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"When scheduling a meeting in San Francisco, it's important to consider factors like traffic and popular tourist times. Generally, late morning or early afternoon, around 11:00 AM to 2:00 PM, can be a good time to avoid rush hour and have a productive meeting. However, it ultimately depends on your itinerary and preferences. Let me know if you need more specific advice!\n"
"When scheduling a meeting in San Francisco, it's important to consider the city's traffic patterns and your itinerary. Generally, late morning or early afternoon, around 11:00 AM to 2:00 PM, can be a good time to avoid rush hour traffic and have a productive meeting. However, if you have specific plans or commitments, it might be best to schedule around those. Let me know if you need more help!\n"
]
}
],
Expand Down Expand Up @@ -155,7 +155,7 @@
"}\n",
"\n",
"# Initialize memory\n",
"memory = Mem0Composable.from_config(\n",
"memory = Mem0ComposableMemory.from_config(\n",
" confif_dict=config,\n",
" context_dict=context_dict\n",
")"
Expand Down Expand Up @@ -200,7 +200,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"That sounds exciting! San Francisco has a lot to offer. Do you have any specific places or activities in mind that you’d like to explore while you’re there?\n"
"That sounds exciting! San Francisco has a lot to offer. Do you have any specific places or activities in mind that you want to explore, or would you like some recommendations?\n"
]
}
],
Expand All @@ -218,14 +218,209 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Since you mentioned you're planning to visit San Francisco, you might consider checking out some popular spots like the Golden Gate Bridge, Alcatraz Island, Fisherman's Wharf, or exploring neighborhoods like Chinatown and Haight-Ashbury. If you have specific interests, let me know, and I can suggest more tailored activities!\n"
"Since you mentioned you're planning to visit San Francisco, here are a few popular places you might consider exploring:\n",
"\n",
"1. **Golden Gate Bridge** - A must-see iconic landmark.\n",
"2. **Alcatraz Island** - Take a ferry to explore the historic prison.\n",
"3. **Fisherman's Wharf** - Great for seafood and waterfront views.\n",
"4. **Chinatown** - Experience the vibrant culture and cuisine.\n",
"5. **Golden Gate Park** - Perfect for a leisurely stroll or a visit to the museums.\n",
"6. **Lombard Street** - Known for its steep, winding road.\n",
"\n",
"Let me know if you need more information or have other interests!\n"
]
}
],
"source": [
"response = agent.chat(\"Where I will be visiting tommorow?\")\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tests"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': '63012c6b-9a5e-4e89-a025-680752c9e649', 'data': {'old_memory': 'Likes to eat pizza in snacks over burger', 'new_memory': 'Likes to eat chowmein in snacks over pizza'}, 'event': 'UPDATE'}, {'id': '1aeb2b94-5254-489e-ae1c-71ca7e98fa02', 'data': {'memory': 'Likes to eat burger in snacks'}, 'event': 'ADD'}]\n"
]
}
],
"source": [
"from mem0 import MemoryClient\n",
"client = MemoryClient(api_key=\"m0-CyWinV6RbPWGgnMVp3Dn7RlrolmRdZXvQvJIox6R\")\n",
"\n",
"# Add some message to create history\n",
"messages = [{\"role\": \"user\", \"content\": \"Hi, My name is mayank\"}]\n",
"\n",
"# # Add second message to update history\n",
"messages.append({\"role\": \"assistant\", \"content\": \"Hi, Mayank nice to meet you.\"})\n",
"messages.append({\"role\": \"user\", \"content\": \"I like to Burger in snacks.\"})\n",
"messages.append({\"role\": \"assistant\", \"content\": \"Okay, noted your prefrences.\"})\n",
"messages.append({\"role\": \"user\", \"content\": \"I like to eat pizza in snacks over burger.\"})\n",
"messages.append({'role': 'user', 'content': 'I like to eat chowmein in snacks over pizza'})\n",
"\n",
"res = client.add(messages, user_id=\"raghu_2\")\n",
"print(res)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': '9f75e83e-8b38-49c5-84dc-bf18db377aaa', 'data': {'memory': 'Leaving for Vietnam tomorrow'}, 'event': 'ADD'}]\n"
]
}
],
"source": [
"messages2 = [{\"role\": \"assistant\", \"content\": \"Hi Raghu, You can visit taj mahal in India. Also you can go to Delhi\"}]\n",
"res = client.add(\"I have to leave to Vietnam tommorow\", user_id=\"raghu_2\")\n",
"print(res)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"# \n",
"# [{'id': 'fdb9d075-6e25-4a57-b993-adabdefd9041', 'data': {'memory': 'Name is Mayank'}, 'event': 'ADD'}, {'id': '63012c6b-9a5e-4e89-a025-680752c9e649', 'data': {'memory': 'Likes burgers as snacks'}, 'event': 'ADD'}]\n",
"# [{'id': '63012c6b-9a5e-4e89-a025-680752c9e649', 'data': {'old_memory': 'Likes burgers as snacks', 'new_memory': 'Likes to eat pizza in snacks over burger'}, 'event': 'UPDATE'}]\n",
"[\n",
" {'id': 'e5af43ba-d396-4186-9ba5-12ab8918b262', \n",
" 'memory_id': '63012c6b-9a5e-4e89-a025-680752c9e649', \n",
" 'input': [\n",
" {'role': 'user', 'content': 'Hi, My name is mayank'}, \n",
" {'role': 'assistant', 'content': 'Hi, Mayank nice to meet you.'}, \n",
" {'role': 'user', 'content': 'I like to Burger in snacks.'}, \n",
" {'role': 'assistant', 'content': 'Okay, noted your prefrences.'},\n",
" {'role': 'assistant', 'content': 'I like to eat chowmein in snacks over pizza'}],\n",
" \n",
" 'old_memory': None, \n",
" 'new_memory': 'Likes burgers as snacks', \n",
" 'user_id': 'raghu_2', \n",
" 'categories': ['food'], \n",
" 'event': 'ADD', 'metadata': None, 'created_at': '2024-10-20T02:43:39.908162-07:00', 'updated_at': '2024-10-20T02:43:39.893717-07:00'}, {'id': 'f20f07b6-0dfc-4396-95a0-1dd5eee82fea', 'memory_id': '63012c6b-9a5e-4e89-a025-680752c9e649', 'input': [{'role': 'user', 'content': 'Hi, My name is mayank'}, {'role': 'assistant', 'content': 'Hi, Mayank nice to meet you.'}, {'role': 'user', 'content': 'I like to Burger in snacks.'}, {'role': 'assistant', 'content': 'Okay, noted your prefrences.'}], 'old_memory': 'Likes burgers as snacks', 'new_memory': 'Likes to eat pizza in snacks over burger', 'user_id': 'raghu_2', 'categories': ['food'], 'event': 'UPDATE', 'metadata': None, 'created_at': '2024-10-20T02:46:32.650687-07:00', 'updated_at': '2024-10-20T02:46:32.637414-07:00'}]\n",
"\n",
"# Get history of how memory changed over time\n",
"memory_id = \"63012c6b-9a5e-4e89-a025-680752c9e649\"\n",
"history = client.history(memory_id)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': 'e5af43ba-d396-4186-9ba5-12ab8918b262', 'memory_id': '63012c6b-9a5e-4e89-a025-680752c9e649', 'input': [{'role': 'user', 'content': 'Hi, My name is mayank'}, {'role': 'assistant', 'content': 'Hi, Mayank nice to meet you.'}, {'role': 'user', 'content': 'I like to Burger in snacks.'}, {'role': 'assistant', 'content': 'Okay, noted your prefrences.'}], 'old_memory': None, 'new_memory': 'Likes burgers as snacks', 'user_id': 'raghu_2', 'categories': ['food'], 'event': 'ADD', 'metadata': None, 'created_at': '2024-10-20T02:43:39.908162-07:00', 'updated_at': '2024-10-20T02:43:39.893717-07:00'}, {'id': 'f20f07b6-0dfc-4396-95a0-1dd5eee82fea', 'memory_id': '63012c6b-9a5e-4e89-a025-680752c9e649', 'input': [{'role': 'user', 'content': 'Hi, My name is mayank'}, {'role': 'assistant', 'content': 'Hi, Mayank nice to meet you.'}, {'role': 'user', 'content': 'I like to Burger in snacks.'}, {'role': 'assistant', 'content': 'Okay, noted your prefrences.'}], 'old_memory': 'Likes burgers as snacks', 'new_memory': 'Likes to eat pizza in snacks over burger', 'user_id': 'raghu_2', 'categories': ['food'], 'event': 'UPDATE', 'metadata': None, 'created_at': '2024-10-20T02:46:32.650687-07:00', 'updated_at': '2024-10-20T02:46:32.637414-07:00'}]\n"
]
}
],
"source": [
"print(history)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[{'id': '3265a55c-cee5-45b2-987e-679b8e177771', 'memory_id': '0c073fdf-a417-4c18-9f8a-3ea2a5d50e8f', 'input': [{'role': 'user', 'content': 'Hi, My name is Mayank.'}], 'old_memory': None, 'new_memory': 'Name is Mayank', 'user_id': 'mayank_21', 'categories': ['personal_details'], 'event': 'ADD', 'metadata': None, 'created_at': '2024-10-20T02:08:07.334859-07:00', 'updated_at': '2024-10-20T02:08:07.326438-07:00'}]\n",
"[{'id': '990abd8e-0bad-40be-9e06-50b55bc30aec', 'memory_id': '919c84e2-f54c-4a38-a905-973eb90b6c6d', 'input': [{'role': 'user', 'content': 'My name is Raghu.'}], 'old_memory': None, 'new_memory': 'Name is Raghu', 'user_id': 'mayank_21', 'categories': ['personal_details'], 'event': 'ADD', 'metadata': None, 'created_at': '2024-10-20T02:10:29.136596-07:00', 'updated_at': '2024-10-20T02:10:29.128864-07:00'}]\n",
"[{'id': '1bc43d66-ff5d-4eef-9dbe-58b98539304c', 'data': {'memory': 'Likes to eat ice cream'}, 'event': 'ADD'}]\n",
"[{'id': '181e2e16-c841-4c7b-a77e-8d062adc1391', 'memory_id': '1bc43d66-ff5d-4eef-9dbe-58b98539304c', 'input': [{'role': 'user', 'content': 'I like to eat ice cream'}], 'old_memory': None, 'new_memory': 'Likes to eat ice cream', 'user_id': 'mayank_21', 'categories': ['food'], 'event': 'ADD', 'metadata': None, 'created_at': '2024-10-20T02:13:05.850445-07:00', 'updated_at': '2024-10-20T02:13:05.843199-07:00'}]\n",
"[{'id': '1bc43d66-ff5d-4eef-9dbe-58b98539304c', 'data': {'memory': 'Likes to eat ice cream'}, 'event': 'DELETE'}]\n",
"#1038102d-4fcf-41d5-833a-d21bec50680d\n",
"#fba767e3-ac98-4541-9187-fc7dc7b07a27"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': '41f66e2c-5da7-404f-bd67-b05996962e7a', 'memory': 'Likes ice cream', 'user_id': 'raghu_1', 'hash': '665c416431c628e75f708cf71ced25ab', 'metadata': None, 'categories': ['food'], 'created_at': '2024-10-20T02:36:01.701954-07:00', 'updated_at': '2024-10-20T02:36:01.701974-07:00', 'score': 0.33866615160300395}, {'id': 'adb12a05-8c10-44c9-a035-258d65ca706c', 'memory': 'Does not like ice cream', 'user_id': 'raghu_1', 'hash': 'f03f2523a65c1319e3b629944d622ae7', 'metadata': None, 'categories': ['user_preferences', 'food'], 'created_at': '2024-10-20T02:21:50.007369-07:00', 'updated_at': '2024-10-20T02:21:50.007387-07:00', 'score': 0.3099606797040534}, {'id': 'b86a079f-31ba-448e-be81-3524bc8ef39c', 'memory': 'Name is Raghu', 'user_id': 'raghu_1', 'hash': 'da0d42ceaaab2f5263b7be1611fd5e88', 'metadata': None, 'categories': ['personal_details'], 'created_at': '2024-10-20T02:21:49.973540-07:00', 'updated_at': '2024-10-20T02:21:49.973559-07:00', 'score': 0.30741360320096134}]\n"
]
}
],
"source": [
"res = client.search(query='What do I have in desert?',user_id='raghu_1')\n",
"print(res)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"4\n"
]
}
],
"source": [
"a = [1,2,3,4]\n",
"\n",
"for i in a[2:]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 81ee252

Please sign in to comment.