-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkbmem0.py
97 lines (80 loc) · 2.33 KB
/
kbmem0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "xxx"
os.environ["OPENAI_BASE_URL"]= "http://localhost:8097/v1"
config = {
"llm": {
"provider": "openai",
"config": {
"model": "qwen2-instruct-70b",
"temperature": 0.2,
"max_tokens": 1500,
}
},
"embedder":{
"provider": "openai",
"config":{
"model": "bge-large-zh-v1.5"
}
},
"history_db_path": "/home/terry/.mem0/history.db",
"collection_name": "mem0",
"embedding_model_dims": 1024
}
xconfig = {
"llm": {
"provider": "openai",
"config": {
"model": "gemma2:27b",
"temperature": 0.2,
"max_tokens": 1500,
}
},
"embedder":{
"provider": "openai",
"config":{
"model": "nomic-embed-text"
}
},
"history_db_path": "/home/terry/.mem0/history.db",
"collection_name": "mem0",
"embedding_model_dims": 1024
}
# Initialize Mem0
m = Memory.from_config(config)
class Mem0DB():
def __init__(self, dir=None):
if dir is not None:
self.build_knowledge_base(dir)
def build_knowledge_base(self, directory):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path) and (file_path.endswith('.txt') or file_path.endswith(".md")):
self.add_document(file_path, 'default')
def add_document(self, file_path, user_id):
print("adding document:" + file_path)
with open(file_path, 'r') as fp:
content = fp.read()
m.add(content, user_id)
def dump(self):
return m.get_all()
def search_db(text, user_id='default'):
# Search memories
related_memories = m.search(query=text, user_id=user_id)
print(related_memories)
return None
def update_db(data, user_id):
memory_id = related_memories[0]["id"]
# Update a memory
result = m.update(memory_id=memory_id, data="Likes to play tennis on weekends")
print(result)
# Get memory history
history = m.history(memory_id=memory_id)
print(history)
if __name__ == "__main__":
import sys
mem0db = Mem0DB("test")
print("searching...")
ret = search_db(sys.argv[1])
print(ret)
print(mem0db.dump())