Skip to content

Commit 23291c9

Browse files
Merge pull request #994 from MervinPraison/claude/issue-992-20250718-1345
feat: integrate MongoDB as memory and knowledge provider
2 parents 83fdd00 + 26c242b commit 23291c9

File tree

13 files changed

+2662
-15
lines changed

13 files changed

+2662
-15
lines changed

examples/python/mongodb/README.md

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
# MongoDB Integration Examples for PraisonAI Agents
2+
3+
This directory contains comprehensive examples demonstrating how to integrate MongoDB with PraisonAI agents for memory, knowledge, and data operations.
4+
5+
## Features
6+
7+
MongoDB integration with PraisonAI provides:
8+
9+
- **Memory Provider**: Use MongoDB as a persistent memory store for agents
10+
- **Knowledge Store**: Store and retrieve documents with vector search capabilities
11+
- **Tools Integration**: Perform MongoDB operations directly from agents
12+
- **Vector Search**: Leverage MongoDB Atlas Vector Search for semantic similarity
13+
- **Scalability**: Handle large datasets with MongoDB's scalable architecture
14+
- **Flexibility**: Use MongoDB as both key-value store and vector database
15+
16+
## Prerequisites
17+
18+
### Installation
19+
20+
```bash
21+
# Install PraisonAI with MongoDB support
22+
pip install 'praisonaiagents[mongodb]'
23+
```
24+
25+
### Dependencies
26+
27+
- **MongoDB**: Local MongoDB instance or MongoDB Atlas
28+
- **OpenAI API Key**: For embeddings and LLM operations
29+
- **Python 3.10+**: Required for PraisonAI
30+
31+
### Environment Setup
32+
33+
```bash
34+
# Set your OpenAI API key
35+
export OPENAI_API_KEY="your-openai-api-key"
36+
37+
# Optional: Set MongoDB connection string
38+
export MONGODB_CONNECTION_STRING="mongodb://localhost:27017/"
39+
```
40+
41+
## Examples Overview
42+
43+
### 1. [mongodb_memory_example.py](mongodb_memory_example.py)
44+
45+
Demonstrates using MongoDB as a memory provider for PraisonAI agents.
46+
47+
**Features:**
48+
- MongoDB as persistent memory storage
49+
- Quality scoring and filtering
50+
- Vector search for memory retrieval
51+
- Multi-session memory persistence
52+
- Memory search and context building
53+
54+
**Usage:**
55+
```bash
56+
python mongodb_memory_example.py
57+
```
58+
59+
### 2. [mongodb_knowledge_example.py](mongodb_knowledge_example.py)
60+
61+
Shows how to use MongoDB as a knowledge store with vector search capabilities.
62+
63+
**Features:**
64+
- MongoDB as knowledge vector store
65+
- Document processing and storage
66+
- Vector search for knowledge retrieval
67+
- Knowledge-based agent interactions
68+
- File processing with MongoDB storage
69+
70+
**Usage:**
71+
```bash
72+
python mongodb_knowledge_example.py
73+
```
74+
75+
### 3. [mongodb_tools_example.py](mongodb_tools_example.py)
76+
77+
Demonstrates MongoDB tools integration for database operations.
78+
79+
**Features:**
80+
- MongoDB CRUD operations
81+
- Vector search with embeddings
82+
- Data analysis and aggregation
83+
- Collection management
84+
- Index creation and optimization
85+
86+
**Usage:**
87+
```bash
88+
python mongodb_tools_example.py
89+
```
90+
91+
### 4. [mongodb_comprehensive_example.py](mongodb_comprehensive_example.py)
92+
93+
Complete business scenario simulation using all MongoDB features.
94+
95+
**Features:**
96+
- Full MongoDB integration (memory + knowledge + tools)
97+
- Multi-agent business workflow
98+
- Real-world e-commerce simulation
99+
- Business intelligence analytics
100+
- Customer service automation
101+
102+
**Usage:**
103+
```bash
104+
python mongodb_comprehensive_example.py
105+
```
106+
107+
## Configuration
108+
109+
### MongoDB Memory Configuration
110+
111+
```python
112+
mongodb_memory_config = {
113+
"provider": "mongodb",
114+
"config": {
115+
"connection_string": "mongodb://localhost:27017/",
116+
"database": "praisonai_memory",
117+
"use_vector_search": True, # Enable Atlas Vector Search
118+
"max_pool_size": 50,
119+
"min_pool_size": 10,
120+
"server_selection_timeout": 5000
121+
}
122+
}
123+
```
124+
125+
### MongoDB Knowledge Configuration
126+
127+
```python
128+
mongodb_knowledge_config = {
129+
"vector_store": {
130+
"provider": "mongodb",
131+
"config": {
132+
"connection_string": "mongodb://localhost:27017/",
133+
"database": "praisonai_knowledge",
134+
"collection": "knowledge_base",
135+
"use_vector_search": True
136+
}
137+
},
138+
"embedder": {
139+
"provider": "openai",
140+
"config": {
141+
"model": "text-embedding-3-small"
142+
}
143+
}
144+
}
145+
```
146+
147+
## MongoDB Atlas Vector Search
148+
149+
For production use with advanced vector search capabilities:
150+
151+
### Setup
152+
153+
1. **Create MongoDB Atlas Cluster**
154+
- Sign up for MongoDB Atlas
155+
- Create a new cluster
156+
- Get your connection string
157+
158+
2. **Enable Vector Search**
159+
- Create vector search indexes
160+
- Configure embedding dimensions (1536 for OpenAI)
161+
- Set similarity metric (cosine, euclidean, dotProduct)
162+
163+
3. **Update Connection String**
164+
```python
165+
"connection_string": "mongodb+srv://username:password@cluster.mongodb.net/"
166+
```
167+
168+
### Vector Search Index Creation
169+
170+
```python
171+
# Vector search index definition
172+
{
173+
"mappings": {
174+
"dynamic": True,
175+
"fields": {
176+
"embedding": {
177+
"type": "knnVector",
178+
"dimensions": 1536,
179+
"similarity": "cosine"
180+
}
181+
}
182+
}
183+
}
184+
```
185+
186+
## Best Practices
187+
188+
### 1. Connection Management
189+
190+
```python
191+
# Use connection pooling
192+
mongodb_config = {
193+
"connection_string": "mongodb://localhost:27017/",
194+
"max_pool_size": 50,
195+
"min_pool_size": 10,
196+
"maxIdleTimeMS": 30000,
197+
"serverSelectionTimeoutMS": 5000
198+
}
199+
```
200+
201+
### 2. Indexing Strategy
202+
203+
```python
204+
# Create appropriate indexes
205+
collection.create_index([("content", "text")]) # Text search
206+
collection.create_index([("created_at", -1)]) # Time-based queries
207+
collection.create_index([("metadata.category", 1)]) # Category filtering
208+
```
209+
210+
### 3. Error Handling
211+
212+
```python
213+
try:
214+
# MongoDB operations
215+
result = collection.insert_one(document)
216+
except PyMongoError as e:
217+
logger.error(f"MongoDB error: {e}")
218+
# Implement fallback strategy
219+
```
220+
221+
### 4. Data Validation
222+
223+
```python
224+
# Validate data before storage
225+
def validate_document(doc):
226+
required_fields = ["content", "metadata", "created_at"]
227+
return all(field in doc for field in required_fields)
228+
```
229+
230+
## Performance Considerations
231+
232+
### 1. Indexing
233+
234+
- Create indexes on frequently queried fields
235+
- Use compound indexes for complex queries
236+
- Monitor index usage with MongoDB profiler
237+
238+
### 2. Vector Search Optimization
239+
240+
- Use appropriate vector dimensions
241+
- Optimize numCandidates parameter
242+
- Consider filtering to reduce search space
243+
244+
### 3. Connection Pooling
245+
246+
- Configure appropriate pool sizes
247+
- Use connection pooling for high-concurrency scenarios
248+
- Monitor connection metrics
249+
250+
## Troubleshooting
251+
252+
### Common Issues
253+
254+
1. **Connection Errors**
255+
- Check MongoDB server is running
256+
- Verify connection string format
257+
- Ensure network connectivity
258+
259+
2. **Vector Search Issues**
260+
- Verify Atlas Vector Search indexes exist
261+
- Check embedding dimensions match
262+
- Ensure proper index configuration
263+
264+
3. **Memory Issues**
265+
- Monitor memory usage with large datasets
266+
- Use appropriate batch sizes
267+
- Consider data pagination
268+
269+
### Debugging
270+
271+
```python
272+
# Enable detailed logging
273+
import logging
274+
logging.basicConfig(level=logging.DEBUG)
275+
276+
# Monitor MongoDB operations
277+
from pymongo import monitoring
278+
monitoring.register(CommandLogger())
279+
```
280+
281+
## Production Deployment
282+
283+
### 1. Security
284+
285+
```python
286+
# Use authentication
287+
connection_string = "mongodb://username:password@host:port/database"
288+
289+
# Enable SSL/TLS
290+
client = MongoClient(connection_string, tls=True)
291+
```
292+
293+
### 2. Monitoring
294+
295+
- Use MongoDB Atlas monitoring
296+
- Implement application-level metrics
297+
- Set up alerting for critical issues
298+
299+
### 3. Backup and Recovery
300+
301+
- Configure automated backups
302+
- Test restore procedures
303+
- Implement disaster recovery plans
304+
305+
## Support
306+
307+
For additional support:
308+
309+
- [PraisonAI Documentation](https://docs.praisonai.com)
310+
- [MongoDB Documentation](https://docs.mongodb.com)
311+
- [MongoDB Atlas Vector Search](https://docs.atlas.mongodb.com/atlas-vector-search/)
312+
313+
## License
314+
315+
These examples are provided under the same license as PraisonAI.

0 commit comments

Comments
 (0)