-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage.html
More file actions
483 lines (420 loc) · 14 KB
/
usage.html
File metadata and controls
483 lines (420 loc) · 14 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Usage Examples - MongoDB NL Query AI Agent</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #333;
background: #f5f7fa;
}
.nav {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 15px 30px;
display: flex;
justify-content: space-between;
align-items: center;
}
.nav a {
color: white;
text-decoration: none;
font-weight: 500;
}
.nav a:hover {
text-decoration: underline;
}
.container {
max-width: 1000px;
margin: 40px auto;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #667eea;
margin-bottom: 5px;
font-size: 2.5em;
}
h2 {
color: #667eea;
margin-top: 5px;
margin-bottom: 5px;
padding-bottom: 10px;
border-bottom: 2px solid #e0e0e0;
}
h3 {
color: #555;
margin-top: 5px;
margin-bottom: 5px;
}
p {
margin-bottom: 5px;
}
pre {
background: #2d3748;
color: #f7fafc;
padding: 20px;
border-radius: 8px;
overflow-x: auto;
margin: 5px 0;
}
code {
font-family: 'Courier New', monospace;
}
.example {
background: #f8f9fa;
padding: 20px;
border-radius: 8px;
margin: 5px 0;
border-left: 4px solid #667eea;
}
.example h4 {
color: #667eea;
margin-bottom: 5px;
}
.response {
background: #d1fae5;
padding: 15px;
border-radius: 5px;
margin-top: 5px;
}
.info-box {
background: #e8f4f8;
border-left: 4px solid #667eea;
padding: 15px;
margin: 5px 0;
border-radius: 5px;
}
ul, ol {
margin-left: 20px;
margin-bottom: 5px;
}
li {
margin-bottom: 8px;
}
.next-steps {
background: #f8f9fa;
padding: 20px;
border-radius: 8px;
margin-top: 5px;
}
.next-steps a {
color: #667eea;
text-decoration: none;
font-weight: 500;
}
.next-steps a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="nav">
<div><a href="index.html">← Back to Documentation Hub</a></div>
<div>MongoDB NL Query AI Agent - Usage Examples</div>
</div>
<div class="container">
<h1>💡 Usage Examples</h1>
<p>Real-world examples of natural language queries and their results.</p>
<h2>🚀 Starting the Server</h2>
<pre><code># Activate virtual environment
source venv/bin/activate
# Start MongoDB NL Query AI Agent
python3 -m mongodb_agent.api
# Server runs on http://localhost:8001</code></pre>
<h2 id="basic-queries">📊 Basic Queries</h2>
<div class="example">
<h4>Example 1: Count Documents</h4>
<p><strong>Question:</strong> "How many documents are in the B2BTransaction collection?"</p>
<p><strong>cURL:</strong></p>
<pre><code>curl -X POST http://localhost:8001/api/mongodb \
-H "Content-Type: application/json" \
-d '{
"question": "How many documents are in the B2BTransaction collection?",
"yaml_file_name": "b2btransaction_semantic_model_template.yaml",
"include_debug": true
}'</code></pre>
<div class="response">
<p><strong>Response:</strong></p>
<pre><code>{
"query_result": [
{"count": 15842}
],
"query_generated": [
{"$count": "count"}
],
"execution_time_seconds": 0.234
}</code></pre>
</div>
</div>
<div class="example">
<h4>Example 2: Simple Filter with Grouping</h4>
<p><strong>Question:</strong> "Group all transactions by state for partner name 'FEDEX_TPL'"</p>
<p><strong>cURL:</strong></p>
<pre><code>curl -X POST http://localhost:8001/api/mongodb \
-H "Content-Type: application/json" \
-d '{
"question": "Group all transactions state and partner name :'FEDEX_TPL' top 10 records?",
"yaml_file_name": "b2btransaction_semantic_model_template.yaml",
"include_debug": true
}'</code></pre>
<div class="response">
<p><strong>Response:</strong></p>
<pre><code>{
"question": "Show me 10 completed orders",
"yaml_file_name": "orders_semantic_model.yaml",
"mongodb_query": "db.orders.aggregate([{\"$match\":{\"status\":\"completed\"}},{\"$limit\":10}])",
"query_result": [
{"_id": "order_001", "status": "completed", "total": 125.50},
{"_id": "order_002", "status": "completed", "total": 89.99}
],
"natural_language_response": "• Found 10 completed orders with a total value of $1,254.50",
"execution_time_ms": 234.56,
"status": "success",
"timestamp": "2026-02-02T12:00:00"
}</code></pre>
</div>
</div>
<div class="example">
<h4>Example 3: Aggregation with Count</h4>
<p><strong>Question:</strong> "Count transactions by processing state"</p>
<p><strong>cURL:</strong></p>
<pre><code>curl -X POST http://localhost:8001/api/mongodb \
-H "Content-Type: application/json" \
-d '{
"question": "Count all transactions by processing state",
"yaml_file_name": "b2btransaction_semantic_model_template.yaml",
"include_debug": false
}'</code></pre>
<div class="response">
<p><strong>Response:</strong></p>
<pre><code>{
"question": "Count all transactions by processing state",
"yaml_file_name": "b2btransaction_semantic_model_template.yaml",
"mongodb_query": "db.transactions.aggregate([{\"$group\":{\"_id\":\"$processing_state\",\"count\":{\"$sum\":1}}},{\"$project\":{\"state\":\"$_id\",\"count\":1,\"_id\":0}}])",
"query_result": [
{"state": "COMPLETED", "count": 1250},
{"state": "PENDING", "count": 89},
{"state": "ERROR", "count": 12}
],
"natural_language_response": "• COMPLETED: 1,250 transactions\\n• PENDING: 89 transactions\\n• ERROR: 12 transactions",
"execution_time_ms": 456.78,
"status": "success",
"timestamp": "2026-02-02T12:00:00"
}</code></pre>
</div>
</div>
<h2 id="advanced-queries">🎯 Advanced Queries</h2>
<div class="example">
<h4>Example 4: Date Range Filter</h4>
<p><strong>Question:</strong> "Show me transactions from last month in ERROR state"</p>
<p><strong>cURL:</strong></p>
<pre><code>curl -X POST http://localhost:8001/api/mongodb \
-H "Content-Type: application/json" \
-d '{
"question": "Show me transactions from last month in ERROR state",
"yaml_file_name": "b2btransaction_semantic_model_template.yaml",
"include_debug": true
}'</code></pre>
<div class="response">
<p><strong>Generated Query:</strong></p>
<pre><code>[
{
"$match": {
"created_date": {
"$gte": "2024-11-01",
"$lte": "2024-11-30"
},
"total": {"$gt": 100}
}
},
{"$sort": {"created_date": -1}},
{"$limit": 50}
]</code></pre>
</div>
</div>
<div class="example">
<h4>Example 5: Multi-Stage Aggregation</h4>
<p><strong>Question:</strong> "What are the top 5 partner names by transaction count?"</p>
<p><strong>cURL:</strong></p>
<pre><code>curl -X POST http://localhost:8001/api/mongodb \
-H "Content-Type: application/json" \
-d '{
"question": "What are the top 5 partner names by transaction count?",
"yaml_file_name": "b2btransaction_semantic_model_template.yaml",
"include_debug": true
}'</code></pre>
<div class="response">
<p><strong>Generated Query:</strong></p>
<pre><code>[
{
"$match": {
"order_date": {
"$gte": "2024-10-01",
"$lte": "2024-12-31"
}
}
},
{"$unwind": "$items"},
{
"$group": {
"_id": "$items.product_id",
"total_sales": {"$sum": "$items.quantity"},
"revenue": {"$sum": {"$multiply": ["$items.price", "$items.quantity"]}}
}
},
{"$sort": {"revenue": -1}},
{"$limit": 5},
{
"$project": {
"product_id": "$_id",
"total_sales": 1,
"revenue": 1,
"_id": 0
}
}
]</code></pre>
</div>
</div>
<div class="example">
<h4>Example 6: Complex Join-like Query</h4>
<p><strong>Question:</strong> "Show me customers who placed more than 10 orders"</p>
<p><strong>Generated Query:</strong></p>
<pre><code>[
{
"$group": {
"_id": "$customer_id",
"order_count": {"$sum": 1},
"total_spent": {"$sum": "$total"}
}
},
{
"$match": {
"order_count": {"$gt": 10}
}
},
{"$sort": {"order_count": -1}},
{
"$project": {
"customer_id": "$_id",
"order_count": 1,
"total_spent": 1,
"_id": 0
}
}
]</code></pre>
</div>
<h2>🐍 Python Example</h2>
<pre><code>import requests
def query_mongodb(question, yaml_file_name, include_debug=True):
"""Query MongoDB using natural language"""
url = "http://localhost:8001/api/mongodb"
payload = {
"question": question,
"yaml_file_name": yaml_file_name,
"include_debug": include_debug
}
response = requests.post(url, json=payload)
return response.json()
# Example usage
result = query_mongodb(
"Group all transactions state and partner name :'FEDEX_TPL' top 10 records?",
"b2btransaction_semantic_model_template.yaml",
include_debug=True
)
print(f"Query: {result['mongodb_query']}")
print(f"Results: {result['query_result']}")
print(f"Summary: {result['natural_language_response']}")
print(f"Time: {result['execution_time_ms']}ms")</code></pre>
<h2>📱 JavaScript/Node.js Example</h2>
<pre><code>const axios = require('axios');
async function queryMongoDB(question, yamlFileName, includeDebug = true) {
const response = await axios.post('http://localhost:8001/api/mongodb', {
question: question,
yaml_file_name: yamlFileName,
include_debug: includeDebug
});
return response.data;
}
// Example usage
(async () => {
const result = await queryMongoDB(
'Group all transactions state and partner name :FEDEX_TPL top 10 records?',
'b2btransaction_semantic_model_template.yaml',
true
);
console.log('Generated Query:', result.mongodb_query);
console.log('Results:', result.query_result);
console.log('Summary:', result.natural_language_response);
console.log('Execution Time:', result.execution_time_ms + 'ms');
})();</code></pre>
<h2>💬 Natural Language Tips</h2>
<div class="info-box">
<h3>✅ Good Questions</h3>
<ul>
<li>"How many orders were completed last month?"</li>
<li>"Show me customers with over $1000 in purchases"</li>
<li>"What's the average order value by region?"</li>
<li>"Find all pending orders from the last 7 days"</li>
<li>"Top 10 products by revenue in Q4"</li>
</ul>
<h3>❌ Questions to Avoid</h3>
<ul>
<li>Too vague: "Show me data"</li>
<li>Missing context: "How many?" (how many what?)</li>
<li>Ambiguous: "Find stuff from yesterday" (what stuff?)</li>
</ul>
</div>
<h2>📋 Common Query Patterns</h2>
<h3>Counting</h3>
<ul>
<li>"How many [items] are there?"</li>
<li>"Count [items] with [condition]"</li>
<li>"Total number of [items]"</li>
</ul>
<h3>Filtering</h3>
<ul>
<li>"Show me [items] where [condition]"</li>
<li>"Find all [items] with [attribute] = [value]"</li>
<li>"Get [items] from [date range]"</li>
</ul>
<h3>Aggregation</h3>
<ul>
<li>"What's the total/average/sum of [field]?"</li>
<li>"Group [items] by [field]"</li>
<li>"Calculate [metric] per [dimension]"</li>
</ul>
<h3>Ranking</h3>
<ul>
<li>"Top N [items] by [metric]"</li>
<li>"Most/least [attribute]"</li>
<li>"Highest/lowest [value]"</li>
</ul>
<h2>⚡ Performance Tips</h2>
<ul>
<li>Be specific in your questions to get more optimized queries</li>
<li>Mention time ranges to leverage indexed fields</li>
<li>Ask for limited results when exploring data</li>
<li>Use collection-specific terminology for better accuracy</li>
</ul>
<div class="next-steps">
<h3>📚 Next Steps</h3>
<ul>
<li><a href="api-reference.html">📖 API Reference</a> - Complete endpoint documentation</li>
<li><a href="troubleshooting.html">🔧 Troubleshooting</a> - Fix common issues</li>
<li><a href="architecture.html">🏗️ Architecture</a> - Understand how it works</li>
</ul>
</div>
</div>
</body>
</html>