quickstart.mdx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. We provide a simple yet customizable interface for performing CRUD operations on memory. Here is how you can create and get memories:
  21. ### 4.1 Create Memories
  22. For users (long-term memory):
  23. ```python
  24. # create long-term memory for users
  25. client.add("Remember my name is Deshraj Yadav.", user_id="deshraj")
  26. client.add("I like to eat pizza and go out on weekends.", user_id="deshraj")
  27. client.add("Oh I am actually allergic to cheese to cannot eat pizza anymore.", user_id="deshraj")
  28. ```
  29. Output:
  30. ```python
  31. {'message': 'Memory added successfully!'}
  32. ```
  33. You can see all the memory operations happening on the platform itself.
  34. ![Mem0 Platform Activity](/images/platform/activity.png)
  35. You can also add memories for a particular session or for an AI agent that you are building:
  36. - For user sessions (short-term memory):
  37. ```python
  38. client.add("Deshraj is building Gmail AI agent", user_id="deshraj", session_id="session-1")
  39. ```
  40. - For agents (long-term memory):
  41. ```python
  42. client.add("Return short responses when responding to emails", agent_id="gmail-agent")
  43. ```
  44. ### 4.2 Retrieve Memories
  45. <CodeGroup>
  46. ```python Code
  47. client.get_all(user_id="deshraj")
  48. ```
  49. ```python Output
  50. [
  51. {
  52. 'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
  53. 'agent': None,
  54. 'consumer': {
  55. 'id': 8,
  56. 'user_id': 'deshraj',
  57. 'metadata': None,
  58. 'created_at': '2024-07-17T16:47:23.899900-07:00',
  59. 'updated_at': '2024-07-17T16:47:23.899918-07:00'
  60. },
  61. 'app': None,
  62. 'run': None,
  63. 'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
  64. 'input': 'Remember my name is Deshraj Yadav.',
  65. 'text': 'My name is Deshraj Yadav.',
  66. 'metadata': None,
  67. 'created_at': '2024-07-17T16:47:25.670180-07:00',
  68. 'updated_at': '2024-07-17T16:47:25.670197-07:00'
  69. },
  70. {
  71. 'id': 'f6dec5d1-b5db-45f5-a2fb-3979a0f27d30',
  72. 'agent': None,
  73. 'consumer': {
  74. 'id': 8,
  75. 'user_id': 'deshraj',
  76. # ... other consumer fields ...
  77. },
  78. # ... other fields ...
  79. 'text': 'I am allergic to cheese so I cannot eat pizza anymore.',
  80. # ... remaining fields ...
  81. },
  82. # ... additional memory entries ...
  83. ]
  84. ```
  85. </CodeGroup>
  86. Similarly, you can get all memories for an agent:
  87. ```python
  88. agent_memories = client.get_all(agent_id="gmail-agent")
  89. ```
  90. Get specific memory:
  91. <CodeGroup>
  92. ```python Code
  93. memory = client.get(memory_id="dbce6e06-6adf-40b8-9187-3d30bd13b741")
  94. ```
  95. ```python Output
  96. {
  97. 'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
  98. 'agent': None,
  99. 'consumer': {
  100. 'id': 8,
  101. 'user_id': 'deshraj',
  102. 'metadata': None,
  103. 'created_at': '2024-07-17T16:47:23.899900-07:00',
  104. 'updated_at': '2024-07-17T16:47:23.899918-07:00'
  105. },
  106. 'app': None,
  107. 'run': None,
  108. 'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
  109. 'input': 'Remember my name is Deshraj Yadav.',
  110. 'text': 'My name is Deshraj Yadav.',
  111. 'metadata': None,
  112. 'created_at': '2024-07-17T16:47:25.670180-07:00',
  113. 'updated_at': '2024-07-17T16:47:25.670197-07:00'
  114. }
  115. ```
  116. </CodeGroup>
  117. ### 4.3 Update Memory
  118. You can also update specific memory by using the following method:
  119. <CodeGroup>
  120. ```python Code
  121. client.update(memory_id, data="Updated name is Deshraj Kumar")
  122. ```
  123. ```python Output
  124. {
  125. 'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
  126. 'agent': None,
  127. 'consumer': {
  128. 'id': 8,
  129. 'user_id': 'deshraj',
  130. 'metadata': None,
  131. 'created_at': '2024-07-17T16:47:23.899900-07:00',
  132. 'updated_at': '2024-07-17T16:47:23.899918-07:00'
  133. },
  134. 'app': None,
  135. 'run': None,
  136. 'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
  137. 'input': 'Updated name is Deshraj Kumar.',
  138. 'text': 'Name is Deshraj Kumar.',
  139. 'metadata': None,
  140. 'created_at': '2024-07-17T16:47:25.670180-07:00',
  141. 'updated_at': '2024-07-17T16:47:25.670197-07:00'
  142. }
  143. ```
  144. </CodeGroup>
  145. ### 4.4 Memory History
  146. Get history of how a memory has changed over time
  147. <CodeGroup>
  148. ```python Code
  149. history = client.history(memory_id)
  150. ```
  151. ```python Output
  152. [
  153. {
  154. 'id': '51193804-2ee6-4f81-b4e7-497e98b70858',
  155. 'memory': {
  156. 'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
  157. 'agent': None,
  158. 'consumer': {
  159. 'id': 8,
  160. 'user_id': 'deshraj',
  161. 'metadata': None,
  162. 'created_at': '2024-07-17T16:47:23.899900-07:00',
  163. 'updated_at': '2024-07-17T16:47:23.899918-07:00'
  164. },
  165. 'app': None,
  166. 'run': None,
  167. 'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
  168. 'input': 'Remember my name is Deshraj Yadav.',
  169. 'text': 'My name is Deshraj Yadav.',
  170. 'metadata': None,
  171. 'created_at': '2024-07-17T16:47:25.670180-07:00',
  172. 'updated_at': '2024-07-17T16:47:25.670197-07:00'
  173. },
  174. 'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
  175. 'event': 'ADD',
  176. 'input': 'Remember my name is Deshraj Yadav.',
  177. 'previous_text': None,
  178. 'text': 'My name is Deshraj Yadav.',
  179. 'metadata': None,
  180. 'created_at': '2024-07-17T16:47:25.686899-07:00',
  181. 'updated_at': '2024-07-17T16:47:25.670197-07:00',
  182. 'change_description': 'Memory ADD event'
  183. }
  184. ]
  185. ```
  186. </CodeGroup>
  187. ### 4.5 Search for relevant memories
  188. <CodeGroup>
  189. ```python Code
  190. client.search("What does Deshraj like to eat?", user_id="deshraj", limit=3)
  191. ```
  192. ```python Output
  193. [
  194. {
  195. "id": "dbce6e06-6adf-40b8-9187-3d30bd13b741",
  196. "agent": null,
  197. "consumer": {
  198. "id": 8,
  199. "user_id": "deshraj",
  200. "metadata": null,
  201. "created_at": "...",
  202. "updated_at": "..."
  203. },
  204. "app": null,
  205. "run": null,
  206. "hash": "57288ac8a87c4ac8d3ac7f2075d264ca",
  207. "input": "Remember my name is Deshraj Yadav.",
  208. "text": "My name is Deshraj Yadav.",
  209. "metadata": null,
  210. "created_at": "2024-07-17T16:47:25.670180-07:00",
  211. "updated_at": "..."
  212. },
  213. {
  214. "id": "091dbed6-74b4-4e15-b765-81be2abe0d6b",
  215. "agent": null,
  216. "consumer": {
  217. "id": 8,
  218. "user_id": "deshraj",
  219. "metadata": null,
  220. "created_at": "...",
  221. "updated_at": "..."
  222. },
  223. "app": null,
  224. "run": null,
  225. "hash": "622a5a24d5ac54136414a22ec12f9520",
  226. "input": "Oh I am actually allergic to cheese to cannot eat pizza anymore.",
  227. "text": "I like to eat pizza and go out on weekends.",
  228. "metadata": null,
  229. "created_at": "2024-07-17T16:49:24.276695-07:00",
  230. "updated_at": "..."
  231. },
  232. {
  233. "id": "5fb8f85d-3383-4bad-9d46-f171272478a4",
  234. "agent": null,
  235. "consumer": {
  236. "id": 8,
  237. "user_id": "deshraj",
  238. "metadata": null,
  239. "created_at": "...",
  240. "updated_at": "..."
  241. },
  242. "app": null,
  243. "run": {
  244. "id": 1,
  245. "run_id": "session-1",
  246. "name": "",
  247. "metadata": null,
  248. "created_at": "...",
  249. "updated_at": "..."
  250. },
  251. "hash": "179ced9649ac2b85350ece4946b1ee9b",
  252. "input": "Deshraj is building Gmail AI agent",
  253. "text": "Deshraj is building Gmail AI agent",
  254. "metadata": null,
  255. "created_at": "2024-07-17T16:52:41.278920-07:00",
  256. "updated_at": "..."
  257. },
  258. {
  259. "id": "f6dec5d1-b5db-45f5-a2fb-3979a0f27d30",
  260. "agent": null,
  261. "consumer": {
  262. "id": 8,
  263. "user_id": "deshraj",
  264. "metadata": null,
  265. "created_at": "...",
  266. "updated_at": "..."
  267. },
  268. "app": null,
  269. "run": null,
  270. "hash": "19248f0766044b5973fc0ef1bf3955ef",
  271. "input": "Oh I am actually allergic to cheese to cannot eat pizza anymore.",
  272. "text": "I am allergic to cheese so I cannot eat pizza anymore.",
  273. "metadata": null,
  274. "created_at": "2024-07-17T16:49:38.622084-07:00",
  275. "updated_at": "..."
  276. }
  277. ]
  278. ```
  279. </CodeGroup>
  280. ### 4.6 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>