quickstart.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ---
  2. title: 🚀 Quickstart
  3. description: 'Get started with Mem0 quickly!'
  4. ---
  5. > Welcome to the Mem0 quickstart guide. This guide will help you get up and running with Mem0 in no time.
  6. ## Installation
  7. To install Mem0, you can use pip. Run the following command in your terminal:
  8. ```bash
  9. pip install mem0ai
  10. ```
  11. ## Basic Usage
  12. ### Initialize Mem0
  13. <Tabs>
  14. <Tab title="Basic">
  15. ```python
  16. from mem0 import Memory
  17. m = Memory()
  18. ```
  19. </Tab>
  20. <Tab title="Advanced">
  21. If you want to run Mem0 in production, initialize using the following method:
  22. Run Qdrant first:
  23. ```bash
  24. docker pull qdrant/qdrant
  25. docker run -p 6333:6333 -p 6334:6334 \
  26. -v $(pwd)/qdrant_storage:/qdrant/storage:z \
  27. qdrant/qdrant
  28. ```
  29. Then, instantiate memory with qdrant server:
  30. ```python
  31. from mem0 import Memory
  32. config = {
  33. "vector_store": {
  34. "provider": "qdrant",
  35. "config": {
  36. "host": "localhost",
  37. "port": 6333,
  38. }
  39. },
  40. }
  41. m = Memory.from_config(config)
  42. ```
  43. </Tab>
  44. </Tabs>
  45. ### Store a Memory
  46. ```python
  47. # For a user
  48. result = m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
  49. print(result)
  50. ```
  51. Output:
  52. ```python
  53. [
  54. {
  55. 'id': 'm1',
  56. 'event': 'add',
  57. 'data': 'Likes to play cricket on weekends'
  58. }
  59. ]
  60. ```
  61. ### Retrieve Memories
  62. ```python
  63. # Get all memories
  64. all_memories = m.get_all()
  65. print(all_memories)
  66. ```
  67. Output:
  68. ```python
  69. [
  70. {
  71. 'id': 'm1',
  72. 'text': 'Likes to play cricket on weekends',
  73. 'metadata': {
  74. 'data': 'Likes to play cricket on weekends',
  75. 'category': 'hobbies'
  76. }
  77. },
  78. # ... other memories ...
  79. ]
  80. ```
  81. ```python
  82. # Get a single memory by ID
  83. specific_memory = m.get("m1")
  84. print(specific_memory)
  85. ```
  86. Output:
  87. ```python
  88. {
  89. 'id': 'm1',
  90. 'text': 'Likes to play cricket on weekends',
  91. 'metadata': {
  92. 'data': 'Likes to play cricket on weekends',
  93. 'category': 'hobbies'
  94. }
  95. }
  96. ```
  97. ### Search Memories
  98. ```python
  99. related_memories = m.search(query="What are Alice's hobbies?", user_id="alice")
  100. print(related_memories)
  101. ```
  102. Output:
  103. ```python
  104. [
  105. {
  106. 'id': 'm1',
  107. 'text': 'Likes to play cricket on weekends',
  108. 'metadata': {
  109. 'data': 'Likes to play cricket on weekends',
  110. 'category': 'hobbies'
  111. },
  112. 'score': 0.85 # Similarity score
  113. },
  114. # ... other related memories ...
  115. ]
  116. ```
  117. ### Update a Memory
  118. ```python
  119. result = m.update(memory_id="m1", data="Likes to play tennis on weekends")
  120. print(result)
  121. ```
  122. Output:
  123. ```python
  124. {
  125. 'id': 'm1',
  126. 'event': 'update',
  127. 'data': 'Likes to play tennis on weekends'
  128. }
  129. ```
  130. ### Memory History
  131. ```python
  132. history = m.history(memory_id="m1")
  133. print(history)
  134. ```
  135. Output:
  136. ```python
  137. [
  138. {
  139. 'id': 'h1',
  140. 'memory_id': 'm1',
  141. 'prev_value': None,
  142. 'new_value': 'Likes to play cricket on weekends',
  143. 'event': 'add',
  144. 'timestamp': '2024-07-14 10:00:54.466687',
  145. 'is_deleted': 0
  146. },
  147. {
  148. 'id': 'h2',
  149. 'memory_id': 'm1',
  150. 'prev_value': 'Likes to play cricket on weekends',
  151. 'new_value': 'Likes to play tennis on weekends',
  152. 'event': 'update',
  153. 'timestamp': '2024-07-14 10:15:17.230943',
  154. 'is_deleted': 0
  155. }
  156. ]
  157. ```
  158. ### Delete Memory
  159. ```python
  160. m.delete(memory_id="m1") # Delete a memory
  161. m.delete_all(user_id="alice") # Delete all memories
  162. ```
  163. ### Reset Memory
  164. ```python
  165. m.reset() # Reset all memories
  166. ```
  167. If you have any questions, please feel free to reach out to us using one of the following methods:
  168. <Snippet file="get-help.mdx" />