|
| 1 | +import json |
| 2 | + |
1 | 3 | from crewai_tools import tool |
2 | | -from mem0 import Memory |
| 4 | +from mem0 import MemoryClient |
3 | 5 | from dotenv import load_dotenv |
4 | 6 | import os |
5 | 7 |
|
6 | 8 | load_dotenv() |
7 | 9 |
|
8 | | -# Optionally configure mem0 to use any other store |
9 | | -# https://docs.mem0.ai/components/vectordbs/config#config |
10 | | -config = { |
11 | | - "graph_store": { |
12 | | - "provider": "neo4j", |
13 | | - "config": { |
14 | | - "url": os.getenv("NEO4J_URL"), |
15 | | - "username": os.getenv("NEO4J_USERNAME", 'neo4j'), |
16 | | - "password": os.getenv("NEO4J_PASSWORD"), |
17 | | - } |
18 | | - }, |
19 | | - "version": "v1.1" |
20 | | -} |
21 | | - |
22 | | -memory = Memory.from_config(config) |
| 10 | +# These functions can be extended by changing the user_id parameter |
| 11 | +# Memories are sorted by user_id |
23 | 12 |
|
24 | | -# These functions can be extended by taking an optional user_id parameter |
25 | | -# https://docs.mem0.ai/integrations/multion#add-memories-to-mem0 |
| 13 | +MEM0_API_KEY = os.getenv('MEM0_API_KEY') |
| 14 | +client = MemoryClient(api_key=MEM0_API_KEY) |
26 | 15 |
|
| 16 | +# These tools will only save information about the user |
| 17 | +# "Potato is a vegetable" is not a memory |
| 18 | +# "My favorite food is potatoes" IS a memory |
27 | 19 |
|
28 | 20 | @tool("Write to Memory") |
29 | | -def write_to_memory(data: str) -> str: |
30 | | - """Writes data to the memory store""" |
31 | | - result = memory.add(data) |
32 | | - return f"Memory added with ID: {result['id']}" |
| 21 | +def write_to_memory(user_message: str) -> str: |
| 22 | + """ |
| 23 | + Writes data to the memory store for a user. The tool will decide what |
| 24 | + specific information is important to store as memory. |
| 25 | + """ |
| 26 | + messages = [ |
| 27 | + {"role": "user", "content": user_message}, |
| 28 | + ] |
| 29 | + result = client.add(messages, user_id='default') # configure user |
| 30 | + return json.dumps(result) |
33 | 31 |
|
34 | 32 |
|
35 | 33 | @tool("Read from Memory") |
36 | 34 | def read_from_memory(query: str) -> str: |
37 | | - """Reads memories based on a query.""" |
38 | | - memories = memory.search(query=query) |
39 | | - if memories["memories"]: |
40 | | - return "\n".join([mem["data"] for mem in memories["memories"]]) |
| 35 | + """ |
| 36 | + Reads memories related to user based on a query. |
| 37 | + """ |
| 38 | + memories = client.search(query=query, user_id='default') |
| 39 | + if memories: |
| 40 | + return "\n".join([mem['memory'] for mem in memories]) |
41 | 41 | else: |
42 | 42 | return "No relevant memories found." |
0 commit comments