quickstart.mdx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. ---
  2. title: Quickstart
  3. description: 'Get started with Mem0 Platform in minutes'
  4. ---
  5. ## 1. Installation
  6. Install the Mem0 Python package:
  7. ```bash
  8. pip install mem0ai
  9. ```
  10. ## 2. API Key Setup
  11. 1. Sign in to [Mem0 Platform](https://app.mem0.ai/dashboard/api-keys)
  12. 2. Copy your API Key from the dashboard
  13. ![Get API Key from Mem0 Platform](/images/platform/api-key.png)
  14. ## 3. Instantiate Client
  15. ```python
  16. from mem0 import MemoryClient
  17. client = MemoryClient(api_key="your-api-key")
  18. ```
  19. ## 4. Memory Operations
  20. Mem0 provides a simple and customizable interface for performing CRUD operations on memory.
  21. ### 4.1 Create Memories
  22. You can create long-term and short-term memories for your users, AI Agents, etc. Here are some examples:
  23. #### Long-term memory for a user
  24. ```python Code
  25. from mem0 import MemoryClient
  26. client = MemoryClient(api_key="your-api-key")
  27. messages = [
  28. {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."},
  29. {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."}
  30. ]
  31. client.add(messages, user_id="alex")
  32. ```
  33. #### Short-term memory for a user session
  34. ```python Code
  35. from mem0 import MemoryClient
  36. client = MemoryClient(api_key="your-api-key")
  37. messages = [
  38. {"role": "user", "content": "I'm planning a trip to Japan next month."},
  39. {"role": "assistant", "content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"},
  40. {"role": "user", "content": "Yes, please! Especially in Tokyo."},
  41. {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."}
  42. ]
  43. client.add(messages, user_id="alex123", session_id="trip-planning-2024")
  44. ```
  45. #### Long-term memory for agents
  46. ```python Code
  47. from mem0 import MemoryClient
  48. client = MemoryClient(api_key="your-api-key")
  49. messages = [
  50. {"role": "system", "content": "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations."},
  51. {"role": "assistant", "content": "Understood. I'll maintain personalized travel preferences for each user and provide customized recommendations based on their dietary restrictions, interests, and past interactions."}
  52. ]
  53. client.add(messages, agent_id="travel-assistant")
  54. ```
  55. You can monitor memory operations on the platform:
  56. ![Mem0 Platform Activity](/images/platform/activity.png)
  57. ### 4.2 Search Relevant Memories
  58. You can also get related memories for a given natural language question using our search method.
  59. <CodeGroup>
  60. ```python Code
  61. query = "What do you know about me?"
  62. client.search(query, user_id="alex")
  63. ```
  64. ```json Output
  65. [
  66. {
  67. "id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5",
  68. "memory": "Name: Alex. Vegetarian. Allergic to nuts.",
  69. "input": [
  70. {
  71. "role": "user",
  72. "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."
  73. },
  74. {
  75. "role": "assistant",
  76. "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."
  77. }
  78. ],
  79. "user_id": "alex",
  80. "hash": "9ee7e1455e84d1dab700ed8749aed75a",
  81. "metadata": null,
  82. "created_at": "2024-07-20T01:30:36.275141-07:00",
  83. "updated_at": "2024-07-20T01:30:36.275172-07:00"
  84. }
  85. ]
  86. ```
  87. </CodeGroup>
  88. Similarly, you can search for agent memories by passing the `agent_id` argument.
  89. ```python Code
  90. client.search("What are the learnings from previous runs?", agent_id="travel-assistant")
  91. ```
  92. ### 4.3 Get All Memories
  93. Fetch all memories for a user, agent, or session using the get_all() method.
  94. #### Get all memories of an AI Agent
  95. <CodeGroup>
  96. ```python Code
  97. client.get_all(agent_id="travel-assistant")
  98. ```
  99. ```json Output
  100. [
  101. {
  102. "id": "48677fae-16c4-4d57-9c27-f984f290722a",
  103. "memory": "Will remember personalized travel preferences for each user.",
  104. "input": [
  105. {
  106. "role": "system",
  107. "content": "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations."
  108. },
  109. {
  110. "role": "assistant",
  111. "content": "Understood. I'll maintain personalized travel preferences for each user and provide customized recommendations based on their dietary restrictions, interests, and past interactions."
  112. }
  113. ],
  114. "agent_id": "travel-assistant",
  115. "hash": "644d5f40af13d0525305d8dfadc00fd1",
  116. "metadata": null,
  117. "created_at": "2024-07-20T01:25:22.122299-07:00",
  118. "updated_at": "2024-07-20T01:25:22.122327-07:00"
  119. }
  120. ]
  121. ```
  122. </CodeGroup>
  123. #### Get all memories of user
  124. <CodeGroup>
  125. ```python Code
  126. user_memories = client.get_all(user_id="alex")
  127. ```
  128. ```json Output
  129. [
  130. {
  131. "id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5",
  132. "memory": "Name: Alex. Vegetarian. Allergic to nuts.",
  133. "input": [
  134. {
  135. "role": "user",
  136. "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."
  137. },
  138. {
  139. "role": "assistant",
  140. "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."
  141. }
  142. ],
  143. "user_id": "alex",
  144. "hash": "9ee7e1455e84d1dab700ed8749aed75a",
  145. "metadata": null,
  146. "created_at": "2024-07-20T01:30:36.275141-07:00",
  147. "updated_at": "2024-07-20T01:30:36.275172-07:00"
  148. }
  149. ]
  150. ```
  151. </CodeGroup>
  152. #### Get short-term memories for a session
  153. <CodeGroup>
  154. ```python Code
  155. short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024")
  156. ```
  157. ```python Output
  158. [
  159. {
  160. "id": "582bbe6d-506b-48c6-a4c6-5df3b1e63428",
  161. "memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.",
  162. "input": [
  163. {
  164. "role": "user",
  165. "content": "I'm planning a trip to Japan next month."
  166. },
  167. {
  168. "role": "assistant",
  169. "content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"
  170. },
  171. {
  172. "role": "user",
  173. "content": "Yes, please! Especially in Tokyo."
  174. },
  175. {
  176. "role": "assistant",
  177. "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."
  178. }
  179. ],
  180. "user_id": "alex123",
  181. "hash": "d2088c936e259f2f5d2d75543d31401c",
  182. "metadata": null,
  183. "created_at": "2024-07-20T01:34:09.748379-07:00",
  184. "updated_at": "2024-07-20T01:34:09.748391-07:00"
  185. }
  186. ]
  187. ```
  188. </CodeGroup>
  189. #### Get specific memory
  190. <CodeGroup>
  191. ```python Code
  192. memory = client.get(memory_id="582bbe6d-506b-48c6-a4c6-5df3b1e63428")
  193. ```
  194. ```python Output
  195. {
  196. "id": "582bbe6d-506b-48c6-a4c6-5df3b1e63428",
  197. "memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.",
  198. "input": [
  199. {
  200. "role": "user",
  201. "content": "I'm planning a trip to Japan next month."
  202. },
  203. {
  204. "role": "assistant",
  205. "content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"
  206. },
  207. {
  208. "role": "user",
  209. "content": "Yes, please! Especially in Tokyo."
  210. },
  211. {
  212. "role": "assistant",
  213. "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."
  214. }
  215. ],
  216. "user_id": "alex123",
  217. "hash": "d2088c936e259f2f5d2d75543d31401c",
  218. "metadata": null,
  219. "created_at": "2024-07-20T01:34:09.748379-07:00",
  220. "updated_at": "2024-07-20T01:34:09.748391-07:00"
  221. }
  222. ```
  223. </CodeGroup>
  224. ### 4.4 Memory History
  225. Get history of how a memory has changed over time
  226. <CodeGroup>
  227. ```python Code
  228. # Add some message to create history
  229. messages = [{"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."}]
  230. client.add(messages, user_id="alex")
  231. # Add second message to update history
  232. messages.append({'role': 'user', 'content': 'I turned vegetarian now.'})
  233. client.add(messages, user_id="alex")
  234. # Get history of how memory changed over time
  235. memory_id = "<memory-id-here>"
  236. history = client.history(memory_id)
  237. print(history)
  238. ```
  239. ```json Output
  240. [
  241. {
  242. 'id': '5a718c7e-7ed2-4c72-8c55-d7896dc61ca3',
  243. 'memory_id': 'c6c5da66-d851-4653-8a09-ec7b2c1c9cc9',
  244. 'input': [
  245. {
  246. 'role': 'user',
  247. 'content': "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."
  248. }
  249. ],
  250. 'old_memory': None,
  251. 'new_memory': 'Recently tried chicken and loved it. Interested in trying more non-vegetarian dishes.',
  252. 'event': 'ADD',
  253. 'metadata': None,
  254. 'created_at': '2024-07-20T02:13:35.842720-07:00',
  255. 'updated_at': '2024-07-20T02:13:35.818065-07:00'
  256. },
  257. {
  258. 'id': 'e52e2c73-4b97-4154-9844-aa44c6ee57f5',
  259. 'memory_id': 'c6c5da66-d851-4653-8a09-ec7b2c1c9cc9',
  260. 'input': [
  261. {
  262. 'role': 'user',
  263. 'content': "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."
  264. },
  265. {
  266. 'role': 'user',
  267. 'content': 'I turned vegetarian now.'
  268. }
  269. ],
  270. 'old_memory': 'Recently tried chicken and loved it. Interested in trying more non-vegetarian dishes.',
  271. 'new_memory': '',
  272. 'event': 'DELETE',
  273. 'metadata': None,
  274. 'created_at': '2024-07-20T02:13:38.070492-07:00',
  275. 'updated_at': '2024-07-20T02:13:38.060219-07:00'
  276. }
  277. ]
  278. ```
  279. </CodeGroup>
  280. ### 4.5 Delete Memory
  281. Delete specific memory:
  282. <CodeGroup>
  283. ```python Code
  284. client.delete(memory_id)
  285. ```
  286. ```python Output
  287. {'message': 'Memory deleted successfully!'}
  288. ```
  289. </CodeGroup>
  290. Delete all memories of a user:
  291. <CodeGroup>
  292. ```python Code
  293. client.delete_all(user_id="alex")
  294. ```
  295. ```python Output
  296. {'message': 'Memories deleted successfully!'}
  297. ```
  298. </CodeGroup>
  299. Fun fact: You can also delete the memory using the `add()` method by passing natural language command.
  300. ```python Code
  301. client.add("Delete all of my food preferences", user_id="alex")
  302. If you have any questions, please feel free to reach out to us using one of the following methods:
  303. <Snippet file="get-help.mdx" />