MinAI - Về trang chủ
Lý thuyết
7/1335 phút
Đang tải...

Database Tools

Xây dựng database tools cho AI Agents - query, analyze, report

🗃️ Database Tools

0

🎯 Mục tiêu bài học

TB5 min

Sau bài học này, bạn sẽ:

✅ Hiểu Database Agent Architecture

✅ Xây dựng Text-to-SQL Tool cho natural language queries

✅ Implement SQL Executor Tool với safety validation

✅ Tạo Data Analysis Tool cho insights tự động

✅ Build Schema Discovery Tool

✅ Tạo Report Generator hoàn chỉnh

Cho phép agents truy vấn databases, phân tích data, và tạo reports tự động.

1

🔍 Database Agent Architecture

TB5 min
Diagram
Đang vẽ diagram...

Checkpoint

Database Agent pipeline gồm những bước nào? Tại sao cần tách riêng SQL generation và execution?

2

🛠️ Text-to-SQL Tool

TB5 min
JavaScript
1// Tool: text_to_sql
2// Description: "Convert natural language question to SQL query"
3
4// OpenAI prompt for SQL generation:
5const sqlPrompt = `
6You are a SQL expert. Convert the question to a SQL query.
7
8Database schema:
9- customers (id, name, email, created_at, plan)
10- orders (id, customer_id, amount, status, created_at)
11- products (id, name, price, category)
12- order_items (id, order_id, product_id, quantity)
13
14Question: "${$json.question}"
15
16Rules:
171. Use standard SQL syntax
182. Include appropriate JOINs
193. Add WHERE clauses for filters
204. Use aggregations when needed
215. Limit results to 100 rows
22
23Return only the SQL query, no explanation.`;

Checkpoint

Text-to-SQL prompt cần chứa những thông tin gì? Tại sao schema context quan trọng?

3

🛡️ SQL Executor Tool

TB5 min
JavaScript
1// Tool: execute_sql
2// Description: "Execute a SQL query safely and return results"
3
4// PostgreSQL or MySQL node:
5// Query: {{ $json.sqlQuery }}
6
7// SAFETY: Add read-only guards
8function validateQuery(sql) {
9 const dangerous = ['DROP', 'DELETE', 'UPDATE', 'INSERT', 'ALTER', 'TRUNCATE'];
10 const upperSQL = sql.toUpperCase();
11
12 for (const keyword of dangerous) {
13 if (upperSQL.includes(keyword)) {
14 return { safe: false, reason: `Query contains ${keyword} - write operations not allowed` };
15 }
16 }
17 return { safe: true };
18}
19
20const validation = validateQuery($json.sqlQuery);
21if (!validation.safe) {
22 return { json: { error: validation.reason } };
23}
24
25// Execute query via database node

Checkpoint

Tại sao SQL Executor cần validation? Những keywords nguy hiểm nào cần block?

4

📊 Data Analysis Tool

TB5 min
JavaScript
1// Tool: analyze_data
2// Description: "Analyze query results and provide insights"
3
4const analysisPrompt = `
5Analyze this data and provide insights:
6
7Query: ${$json.originalQuestion}
8SQL: ${$json.sqlQuery}
9Results (${$json.results.length} rows):
10${JSON.stringify($json.results.slice(0, 20), null, 2)}
11
12Provide:
131. Direct answer to the question
142. Key metrics (totals, averages, etc.)
153. Notable patterns or outliers
164. Recommendations if appropriate
17
18Format as a clear, concise report.`;

Checkpoint

Data Analysis Tool cần output những gì? Tại sao chỉ gửi 20 rows đầu cho LLM?

5

⚡ Complete Database Agent

TB5 min
Diagram
Đang vẽ diagram...
JavaScript
1// Agent System Prompt
2const agentSystem = `
3You are a data analyst assistant with access to our database.
4
5Available tools:
61. text_to_sql - Convert question to SQL
72. execute_sql - Run SQL query (read-only)
83. analyze_data - Analyze query results
9
10Workflow:
111. Understand the user's question
122. Generate appropriate SQL
133. Execute the query
144. Analyze and explain results
155. Present findings clearly
16
17Always explain your analysis in Vietnamese.
18Include key numbers and insights.
19`;

Checkpoint

Complete Database Agent kết hợp 3 tools như thế nào? System prompt cần hướng dẫn agent workflow ra sao?

6

🔧 Schema Discovery & Report Generator

TB5 min

Schema Discovery Tool

JavaScript
1// Tool: get_schema
2// Description: "Get database table schema information"
3
4// Query to get schema info:
5const schemaQuery = `
6SELECT table_name, column_name, data_type
7FROM information_schema.columns
8WHERE table_schema = 'public'
9ORDER BY table_name, ordinal_position;
10`;
11
12// Format for agent:
13const schema = $json.results.reduce((acc, row) => {
14 if (!acc[row.table_name]) acc[row.table_name] = [];
15 acc[row.table_name].push(`${row.column_name} (${row.data_type})`);
16 return acc;
17}, {});
18
19const formatted = Object.entries(schema)
20 .map(([table, cols]) => `${table}: ${cols.join(', ')}`)
21 .join('\n');
22
23return { json: { schema: formatted } };

Report Generator

JavaScript
1// Tool: generate_report
2// Combine query results into formatted report
3
4const reportPrompt = `
5Create a report from this data analysis:
6
7Topic: ${$json.topic}
8Data: ${JSON.stringify($json.analysisResults)}
9
10Format as Markdown report:
11# Report: [Title]
12## Summary
13[Key findings]
14## Metrics
15| Metric | Value |
16|--------|-------|
17## Details
18[Detailed analysis]
19## Recommendations
20[Action items]
21`;

Checkpoint

Schema Discovery tool giúp gì cho Agent? Report Generator format output như thế nào?

7

🛡️ Security Best Practices

TB5 min
Security Best Practices
  1. Read-only access: Database user chỉ có SELECT permission
  2. Query validation: Block DROP, DELETE, UPDATE, INSERT
  3. Row limits: Luôn LIMIT results (max 100-1000 rows)
  4. Sanitize: Không cho agent truyền raw SQL từ user
  5. Audit log: Track tất cả queries executed

Checkpoint

Những biện pháp bảo mật nào cần áp dụng cho Database Agent? Tại sao cần audit log?

8

📚 Bài tập thực hành

TB5 min
Exercises
  1. Build text-to-SQL tool với schema context
  2. Create safe SQL executor với validation
  3. Build data analysis tool cho insights
  4. Combine thành database agent hoàn chỉnh

Checkpoint

Bạn đã xây dựng được database agent hoàn chỉnh chưa? Agent có thể trả lời câu hỏi data từ natural language không?

🚀 Bài tiếp theo

Bài tiếp theo: API Tools →