massgen 0.1.4__py3-none-any.whl → 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of massgen might be problematic. Click here for more details.
- massgen/__init__.py +1 -1
- massgen/chat_agent.py +340 -20
- massgen/cli.py +326 -19
- massgen/configs/README.md +52 -10
- massgen/configs/memory/gpt5mini_gemini_baseline_research_to_implementation.yaml +94 -0
- massgen/configs/memory/gpt5mini_gemini_context_window_management.yaml +187 -0
- massgen/configs/memory/gpt5mini_gemini_research_to_implementation.yaml +127 -0
- massgen/configs/memory/gpt5mini_high_reasoning_gemini.yaml +107 -0
- massgen/configs/memory/single_agent_compression_test.yaml +64 -0
- massgen/configs/tools/custom_tools/multimodal_tools/playwright_with_img_understanding.yaml +98 -0
- massgen/configs/tools/custom_tools/multimodal_tools/understand_video_example.yaml +54 -0
- massgen/memory/README.md +277 -0
- massgen/memory/__init__.py +26 -0
- massgen/memory/_base.py +193 -0
- massgen/memory/_compression.py +237 -0
- massgen/memory/_context_monitor.py +211 -0
- massgen/memory/_conversation.py +255 -0
- massgen/memory/_fact_extraction_prompts.py +333 -0
- massgen/memory/_mem0_adapters.py +257 -0
- massgen/memory/_persistent.py +687 -0
- massgen/memory/docker-compose.qdrant.yml +36 -0
- massgen/memory/docs/DESIGN.md +388 -0
- massgen/memory/docs/QUICKSTART.md +409 -0
- massgen/memory/docs/SUMMARY.md +319 -0
- massgen/memory/docs/agent_use_memory.md +408 -0
- massgen/memory/docs/orchestrator_use_memory.md +586 -0
- massgen/memory/examples.py +237 -0
- massgen/orchestrator.py +207 -7
- massgen/tests/memory/test_agent_compression.py +174 -0
- massgen/tests/memory/test_context_window_management.py +286 -0
- massgen/tests/memory/test_force_compression.py +154 -0
- massgen/tests/memory/test_simple_compression.py +147 -0
- massgen/tests/test_agent_memory.py +534 -0
- massgen/tests/test_conversation_memory.py +382 -0
- massgen/tests/test_orchestrator_memory.py +620 -0
- massgen/tests/test_persistent_memory.py +435 -0
- massgen/token_manager/token_manager.py +6 -0
- massgen/tools/__init__.py +8 -0
- massgen/tools/_planning_mcp_server.py +520 -0
- massgen/tools/planning_dataclasses.py +434 -0
- {massgen-0.1.4.dist-info → massgen-0.1.5.dist-info}/METADATA +109 -76
- {massgen-0.1.4.dist-info → massgen-0.1.5.dist-info}/RECORD +46 -12
- {massgen-0.1.4.dist-info → massgen-0.1.5.dist-info}/WHEEL +0 -0
- {massgen-0.1.4.dist-info → massgen-0.1.5.dist-info}/entry_points.txt +0 -0
- {massgen-0.1.4.dist-info → massgen-0.1.5.dist-info}/licenses/LICENSE +0 -0
- {massgen-0.1.4.dist-info → massgen-0.1.5.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
# MassGen Memory - Quick Start Guide
|
|
2
|
+
|
|
3
|
+
Get up and running with MassGen memory in 5 minutes!
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Install mem0 for persistent memory support
|
|
9
|
+
pip install mem0ai
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Basic Usage
|
|
13
|
+
|
|
14
|
+
### 1. Short-term Conversation Memory
|
|
15
|
+
|
|
16
|
+
Perfect for maintaining context during active chats:
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
import asyncio
|
|
20
|
+
from massgen.memory import ConversationMemory
|
|
21
|
+
|
|
22
|
+
async def chat_example():
|
|
23
|
+
# Create memory
|
|
24
|
+
memory = ConversationMemory()
|
|
25
|
+
|
|
26
|
+
# Add messages
|
|
27
|
+
await memory.add({"role": "user", "content": "What's 2+2?"})
|
|
28
|
+
await memory.add({"role": "assistant", "content": "2+2 equals 4."})
|
|
29
|
+
|
|
30
|
+
# Get all messages
|
|
31
|
+
messages = await memory.get_messages()
|
|
32
|
+
print(f"Stored {len(messages)} messages")
|
|
33
|
+
|
|
34
|
+
# Get only recent messages
|
|
35
|
+
recent = await memory.get_messages(limit=10)
|
|
36
|
+
|
|
37
|
+
asyncio.run(chat_example())
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**When to use**: Current conversation, temporary storage, quick message access
|
|
41
|
+
|
|
42
|
+
### 2. Long-term Persistent Memory
|
|
43
|
+
|
|
44
|
+
For cross-session knowledge retention:
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import asyncio
|
|
48
|
+
from massgen.memory import PersistentMemory
|
|
49
|
+
|
|
50
|
+
async def persistent_example():
|
|
51
|
+
# NOTE: Replace with your actual backends
|
|
52
|
+
from massgen.backend import YourLLMBackend, YourEmbeddingBackend
|
|
53
|
+
|
|
54
|
+
llm = YourLLMBackend(model="gpt-4")
|
|
55
|
+
embedding = YourEmbeddingBackend(model="text-embedding-3-small")
|
|
56
|
+
|
|
57
|
+
# Create persistent memory
|
|
58
|
+
memory = PersistentMemory(
|
|
59
|
+
agent_name="my_assistant",
|
|
60
|
+
llm_backend=llm,
|
|
61
|
+
embedding_backend=embedding,
|
|
62
|
+
on_disk=True # Persist across restarts
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
# Record conversation
|
|
66
|
+
await memory.record([
|
|
67
|
+
{"role": "user", "content": "I love hiking in mountains"},
|
|
68
|
+
{"role": "assistant", "content": "That sounds wonderful!"}
|
|
69
|
+
])
|
|
70
|
+
|
|
71
|
+
# Later, retrieve relevant info
|
|
72
|
+
context = await memory.retrieve("user's hobbies")
|
|
73
|
+
print(f"Retrieved: {context}") # "user loves hiking"
|
|
74
|
+
|
|
75
|
+
asyncio.run(persistent_example())
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**When to use**: Long-term facts, cross-session knowledge, semantic search
|
|
79
|
+
|
|
80
|
+
## Common Patterns
|
|
81
|
+
|
|
82
|
+
### Pattern 1: Simple Chat Agent
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
class SimpleChatAgent:
|
|
86
|
+
def __init__(self, backend):
|
|
87
|
+
self.backend = backend
|
|
88
|
+
self.memory = ConversationMemory()
|
|
89
|
+
|
|
90
|
+
async def chat(self, user_message):
|
|
91
|
+
# Add user message
|
|
92
|
+
await self.memory.add({"role": "user", "content": user_message})
|
|
93
|
+
|
|
94
|
+
# Get context
|
|
95
|
+
context = await self.memory.get_messages()
|
|
96
|
+
|
|
97
|
+
# Generate response
|
|
98
|
+
response = await self.backend.chat(context)
|
|
99
|
+
|
|
100
|
+
# Save response
|
|
101
|
+
await self.memory.add({"role": "assistant", "content": response})
|
|
102
|
+
|
|
103
|
+
return response
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Pattern 2: Agent with Memory
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
class SmartAgent:
|
|
110
|
+
def __init__(self, llm_backend, embedding_backend):
|
|
111
|
+
self.backend = llm_backend
|
|
112
|
+
self.short_term = ConversationMemory()
|
|
113
|
+
self.long_term = PersistentMemory(
|
|
114
|
+
agent_name="smart_agent",
|
|
115
|
+
llm_backend=llm_backend,
|
|
116
|
+
embedding_backend=embedding_backend
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
async def chat(self, user_message):
|
|
120
|
+
# 1. Add to short-term
|
|
121
|
+
await self.short_term.add({"role": "user", "content": user_message})
|
|
122
|
+
|
|
123
|
+
# 2. Get relevant long-term memories
|
|
124
|
+
context = await self.long_term.retrieve(user_message)
|
|
125
|
+
|
|
126
|
+
# 3. Build full context
|
|
127
|
+
messages = await self.short_term.get_messages()
|
|
128
|
+
if context:
|
|
129
|
+
messages.insert(0, {
|
|
130
|
+
"role": "system",
|
|
131
|
+
"content": f"Relevant background: {context}"
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
# 4. Generate response
|
|
135
|
+
response = await self.backend.chat(messages)
|
|
136
|
+
|
|
137
|
+
# 5. Save to short-term
|
|
138
|
+
await self.short_term.add({"role": "assistant", "content": response})
|
|
139
|
+
|
|
140
|
+
# 6. Optionally save to long-term
|
|
141
|
+
# (only if important)
|
|
142
|
+
if self._is_important(response):
|
|
143
|
+
await self.long_term.record(messages[-2:])
|
|
144
|
+
|
|
145
|
+
return response
|
|
146
|
+
|
|
147
|
+
def _is_important(self, message):
|
|
148
|
+
# Implement your logic
|
|
149
|
+
keywords = ["remember", "important", "fact", "name", "birthday"]
|
|
150
|
+
return any(kw in message.lower() for kw in keywords)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Pattern 3: Agent-Controlled Memory (Tools)
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
class AutonomousAgent:
|
|
157
|
+
def __init__(self, backend, embedding_backend):
|
|
158
|
+
self.backend = backend
|
|
159
|
+
self.memory = PersistentMemory(
|
|
160
|
+
agent_name="autonomous",
|
|
161
|
+
llm_backend=backend,
|
|
162
|
+
embedding_backend=embedding_backend
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# Register memory as tools
|
|
166
|
+
self.tools = [
|
|
167
|
+
{
|
|
168
|
+
"name": "save_to_memory",
|
|
169
|
+
"description": "Save important facts for later",
|
|
170
|
+
"parameters": {
|
|
171
|
+
"type": "object",
|
|
172
|
+
"properties": {
|
|
173
|
+
"thinking": {"type": "string"},
|
|
174
|
+
"content": {"type": "array", "items": {"type": "string"}}
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
"function": self.memory.save_to_memory
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
"name": "recall_from_memory",
|
|
181
|
+
"description": "Search your memory for information",
|
|
182
|
+
"parameters": {
|
|
183
|
+
"type": "object",
|
|
184
|
+
"properties": {
|
|
185
|
+
"keywords": {"type": "array", "items": {"type": "string"}}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
"function": self.memory.recall_from_memory
|
|
189
|
+
}
|
|
190
|
+
]
|
|
191
|
+
|
|
192
|
+
async def chat(self, user_message):
|
|
193
|
+
# Agent can now decide when to save/recall memories
|
|
194
|
+
response = await self.backend.chat(
|
|
195
|
+
messages=[{"role": "user", "content": user_message}],
|
|
196
|
+
tools=self.tools
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
# If agent called save_to_memory or recall_from_memory,
|
|
200
|
+
# those functions will be executed automatically
|
|
201
|
+
|
|
202
|
+
return response
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Configuration Options
|
|
206
|
+
|
|
207
|
+
### Conversation Memory
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
memory = ConversationMemory()
|
|
211
|
+
|
|
212
|
+
# Truncate to last N messages
|
|
213
|
+
await memory.truncate_to_size(100)
|
|
214
|
+
|
|
215
|
+
# Get messages by role
|
|
216
|
+
user_msgs = await memory.get_messages_by_role("user")
|
|
217
|
+
|
|
218
|
+
# Save/restore state
|
|
219
|
+
state = memory.state_dict()
|
|
220
|
+
new_memory = ConversationMemory()
|
|
221
|
+
new_memory.load_state_dict(state)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Persistent Memory
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
# Basic configuration
|
|
228
|
+
memory = PersistentMemory(
|
|
229
|
+
agent_name="agent_1",
|
|
230
|
+
llm_backend=llm,
|
|
231
|
+
embedding_backend=embedding
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
# With user tracking
|
|
235
|
+
memory = PersistentMemory(
|
|
236
|
+
agent_name="agent_1",
|
|
237
|
+
user_name="alice",
|
|
238
|
+
llm_backend=llm,
|
|
239
|
+
embedding_backend=embedding
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
# With session tracking
|
|
243
|
+
memory = PersistentMemory(
|
|
244
|
+
agent_name="agent_1",
|
|
245
|
+
session_name="2024-01-15",
|
|
246
|
+
llm_backend=llm,
|
|
247
|
+
embedding_backend=embedding
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
# Custom vector store
|
|
251
|
+
from mem0.vector_stores.configs import VectorStoreConfig
|
|
252
|
+
|
|
253
|
+
vector_config = VectorStoreConfig(
|
|
254
|
+
provider="qdrant",
|
|
255
|
+
config={
|
|
256
|
+
"on_disk": True,
|
|
257
|
+
"path": "./my_memory_db"
|
|
258
|
+
}
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
memory = PersistentMemory(
|
|
262
|
+
agent_name="agent_1",
|
|
263
|
+
llm_backend=llm,
|
|
264
|
+
embedding_backend=embedding,
|
|
265
|
+
vector_store_config=vector_config
|
|
266
|
+
)
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Tips & Best Practices
|
|
270
|
+
|
|
271
|
+
### 1. Memory Management
|
|
272
|
+
```python
|
|
273
|
+
# Keep conversation memory manageable
|
|
274
|
+
if await memory.size() > 100:
|
|
275
|
+
await memory.truncate_to_size(50) # Keep last 50
|
|
276
|
+
|
|
277
|
+
# Or clear when starting new topic
|
|
278
|
+
await memory.clear()
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### 2. Selective Persistence
|
|
282
|
+
```python
|
|
283
|
+
# Don't save everything to long-term memory
|
|
284
|
+
# Only save important information
|
|
285
|
+
|
|
286
|
+
if contains_important_info(message):
|
|
287
|
+
await long_term.record([message])
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### 3. Efficient Retrieval
|
|
291
|
+
```python
|
|
292
|
+
# Use specific queries for better results
|
|
293
|
+
context = await memory.retrieve("user's name and preferences")
|
|
294
|
+
|
|
295
|
+
# Instead of vague queries
|
|
296
|
+
# context = await memory.retrieve("information") # Too broad
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### 4. State Persistence
|
|
300
|
+
```python
|
|
301
|
+
import json
|
|
302
|
+
|
|
303
|
+
# Save state to file
|
|
304
|
+
state = memory.state_dict()
|
|
305
|
+
with open("memory_state.json", "w") as f:
|
|
306
|
+
json.dump(state, f)
|
|
307
|
+
|
|
308
|
+
# Restore state from file
|
|
309
|
+
with open("memory_state.json", "r") as f:
|
|
310
|
+
state = json.load(f)
|
|
311
|
+
new_memory = ConversationMemory()
|
|
312
|
+
new_memory.load_state_dict(state)
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## Troubleshooting
|
|
316
|
+
|
|
317
|
+
### Problem: ImportError for mem0
|
|
318
|
+
```bash
|
|
319
|
+
# Solution: Install mem0
|
|
320
|
+
pip install mem0ai
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
### Problem: Memory not persisting
|
|
324
|
+
```python
|
|
325
|
+
# Make sure on_disk=True
|
|
326
|
+
memory = PersistentMemory(
|
|
327
|
+
agent_name="agent_1",
|
|
328
|
+
llm_backend=llm,
|
|
329
|
+
embedding_backend=embedding,
|
|
330
|
+
on_disk=True # ← Important!
|
|
331
|
+
)
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### Problem: Out of memory
|
|
335
|
+
```python
|
|
336
|
+
# Regularly truncate conversation memory
|
|
337
|
+
await conv_memory.truncate_to_size(100)
|
|
338
|
+
|
|
339
|
+
# Or clear when no longer needed
|
|
340
|
+
await conv_memory.clear()
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Problem: Slow retrieval
|
|
344
|
+
```python
|
|
345
|
+
# Reduce limit in retrieve calls
|
|
346
|
+
context = await memory.retrieve(query, limit=3) # Instead of default 5
|
|
347
|
+
|
|
348
|
+
# Use more specific queries
|
|
349
|
+
# Good: "user's favorite color"
|
|
350
|
+
# Bad: "information about user"
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
## Next Steps
|
|
354
|
+
|
|
355
|
+
1. **Read the full documentation**: Check `README.md` for detailed API reference
|
|
356
|
+
2. **Explore examples**: Run `examples.py` to see more usage patterns
|
|
357
|
+
3. **Understand design**: Read `DESIGN.md` for architecture details
|
|
358
|
+
4. **Try it out**: Integrate memory into your agents!
|
|
359
|
+
|
|
360
|
+
## Complete Example
|
|
361
|
+
|
|
362
|
+
Here's a fully working example you can copy and run:
|
|
363
|
+
|
|
364
|
+
```python
|
|
365
|
+
import asyncio
|
|
366
|
+
from massgen.memory import ConversationMemory
|
|
367
|
+
|
|
368
|
+
async def complete_example():
|
|
369
|
+
# Create memory
|
|
370
|
+
memory = ConversationMemory()
|
|
371
|
+
|
|
372
|
+
# Simulate a conversation
|
|
373
|
+
print("Starting conversation...")
|
|
374
|
+
|
|
375
|
+
await memory.add({"role": "user", "content": "Hi, I'm Alice"})
|
|
376
|
+
await memory.add({"role": "assistant", "content": "Nice to meet you, Alice!"})
|
|
377
|
+
await memory.add({"role": "user", "content": "What's my name?"})
|
|
378
|
+
await memory.add({"role": "assistant", "content": "Your name is Alice."})
|
|
379
|
+
|
|
380
|
+
# Show stored messages
|
|
381
|
+
messages = await memory.get_messages()
|
|
382
|
+
print(f"\n📚 Stored {len(messages)} messages:")
|
|
383
|
+
for msg in messages:
|
|
384
|
+
print(f" {msg['role']}: {msg['content']}")
|
|
385
|
+
|
|
386
|
+
# Show memory size
|
|
387
|
+
size = await memory.size()
|
|
388
|
+
print(f"\n📊 Memory size: {size} messages")
|
|
389
|
+
|
|
390
|
+
# Get last message
|
|
391
|
+
last = await memory.get_last_message()
|
|
392
|
+
print(f"\n💬 Last message: {last['role']}: {last['content']}")
|
|
393
|
+
|
|
394
|
+
# Save state
|
|
395
|
+
state = memory.state_dict()
|
|
396
|
+
print(f"\n💾 State saved: {len(state['messages'])} messages")
|
|
397
|
+
|
|
398
|
+
print("\n✅ Example completed!")
|
|
399
|
+
|
|
400
|
+
if __name__ == "__main__":
|
|
401
|
+
asyncio.run(complete_example())
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
Run this with:
|
|
405
|
+
```bash
|
|
406
|
+
python -m massgen.memory.examples # Or save the code above
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
Happy coding! 🚀
|