MCPcopy Create free account
hub / github.com/MemTensor/MemOS / MemobaseClient

Class MemobaseClient

evaluation/scripts/utils/client.py:95–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

93
94
95class MemobaseClient:
96 def __init__(self):
97 from memobase import MemoBaseClient
98
99 self.client = MemoBaseClient(
100 project_url=os.getenv("MEMOBASE_PROJECT_URL"), api_key=os.getenv("MEMOBASE_API_KEY")
101 )
102
103 def add(self, messages, user_id, batch_size=2):
104 """
105 messages = [{"role": "assistant", "content": data, "created_at": iso_date}]
106 """
107 from memobase import ChatBlob
108
109 real_uid = self.string_to_uuid(user_id)
110 user = self.client.get_or_create_user(real_uid)
111 for i in range(0, len(messages), batch_size):
112 batch_messages = messages[i : i + batch_size]
113 max_retries = 5
114 for attempt in range(max_retries):
115 try:
116 _ = user.insert(ChatBlob(messages=batch_messages), sync=True)
117 except Exception as e:
118 if attempt < max_retries - 1:
119 time.sleep(2**attempt)
120 else:
121 raise e
122
123 def search(self, query, user_id, top_k):
124 real_uid = self.string_to_uuid(user_id)
125 user = self.client.get_user(real_uid, no_get=True)
126 memories = user.context(
127 max_token_size=top_k * 100,
128 chats=[{"role": "user", "content": query}],
129 event_similarity_threshold=0.2,
130 fill_window_with_events=True,
131 )
132 return memories
133
134 def delete_user(self, user_id):
135 from memobase.error import ServerError
136
137 real_uid = self.string_to_uuid(user_id)
138 with suppress(ServerError):
139 self.client.delete_user(real_uid)
140
141 def string_to_uuid(self, s: str, salt="memobase_client"):
142 return str(uuid.uuid5(uuid.NAMESPACE_DNS, s + salt))
143
144
145class MemosApiClient:

Callers 7

process_userFunction · 0.90
process_userFunction · 0.90
process_userFunction · 0.90
ingest_convFunction · 0.90
mainFunction · 0.90
ingest_convFunction · 0.90
process_userFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected