AI-Assisted Diagram Creation
🤖 Efficiently Creating Diagrams with Gemini CLI
Natural Language Diagram Generation
- Description Generation
- Optimize Existing Diagrams
- Batch Generation
You: I need a diagram showing the user registration process, including:
1. User fills out form
2. Email verification
3. Phone verification (optional)
4. Create account
5. Send welcome email
Gemini: I'll generate a user registration flowchart for you:
```mermaid
graph TD
Start[用户访问注册页面] --> Form[填写注册表单]
Form --> Validate{表单验证}
Validate -->|失败| FormError[显示错误信息]
FormError --> Form
Validate -->|成功| Email[发送验证邮件]
Email --> EmailCheck{用户点击验证链接?}
EmailCheck -->|否| EmailTimeout[验证超时]
EmailTimeout --> Resend[重新发送邮件]
Resend --> Email
EmailCheck -->|是| PhoneOption{需要手机验证?}
PhoneOption -->|是| Phone[输入手机号]
Phone --> SMS[发送验证码]
SMS --> PhoneVerify{验证码正确?}
PhoneVerify -->|否| PhoneError[验证失败]
PhoneError --> Phone
PhoneVerify -->|是| CreateAccount[创建账户]
PhoneOption -->|否| CreateAccount
CreateAccount --> Welcome[发送欢迎邮件]
Welcome --> Success[注册成功]
```
This flowchart clearly shows the complete registration process, including error handling and optional steps.
You: This is my diagram code, please help me optimize the layout and style:
graph LR
A --> B
B --> C
C --> D
D --> E
Gemini: I'll optimize your diagram to improve readability and aesthetics:
```mermaid
graph TB
A[开始阶段] -->|初始化| B[准备阶段]
B -->|数据收集| C[处理阶段]
C -->|分析处理| D[验证阶段]
D -->|质量检查| E[完成阶段]
style A fill:#e1f5fe,stroke:#01579b,stroke-width:2px
style B fill:#fff3e0,stroke:#e65100,stroke-width:2px
style C fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style D fill:#fff8e1,stroke:#f57f17,stroke-width:2px
style E fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
classDef default font-size:16px;
```
Optimizations:
1. Changed to top-to-bottom layout (TB), better fits reading habits for processes
2. Added descriptive text to each node
3. Added action descriptions on connecting lines
4. Used different colors to distinguish phases
5. Unified font size to improve readability
You: I need to generate sequence diagrams for 5 different API endpoints
Gemini: I'll batch generate API sequence diagrams for you:
1️⃣ User Login API:
```mermaid
sequenceDiagram
Client->>+API: POST /auth/login
API->>+DB: 查询用户信息
DB-->>-API: 返回用户数据
API->>+Redis: 创建 Session
Redis-->>-API: Session ID
API-->>-Client: JWT Token
```
2️⃣ Get User Info API:
```mermaid
sequenceDiagram
Client->>+API: GET /users/{id}
API->>+Auth: 验证 Token
Auth-->>-API: 验证成功
API->>+Cache: 查询缓存
Cache-->>API: 缓存未命中
API->>+DB: 查询用户
DB-->>-API: 用户数据
API->>Cache: 更新缓存
API-->>-Client: 用户信息
```
3️⃣ Create Order API:
```mermaid
sequenceDiagram
Client->>+API: POST /orders
API->>+Inventory: 检查库存
Inventory-->>-API: 库存充足
API->>+Payment: 创建支付
Payment-->>-API: 支付 ID
API->>+DB: 保存订单
DB-->>-API: 订单 ID
API->>+Queue: 发送通知消息
API-->>-Client: 订单创建成功
```
4️⃣ Update Product API:
```mermaid
sequenceDiagram
Client->>+API: PUT /products/{id}
API->>+Auth: 验证权限
Auth-->>-API: 权限通过
API->>+DB: 更新产品
DB-->>-API: 更新成功
API->>+Cache: 清除缓存
Cache-->>-API: 缓存已清除
API->>+Search: 更新索引
Search-->>-API: 索引已更新
API-->>-Client: 更新成功
```
5️⃣ Delete Comment API:
```mermaid
sequenceDiagram
Client->>+API: DELETE /comments/{id}
API->>+Auth: 验证用户身份
Auth-->>-API: 身份确认
API->>+DB: 软删除评论
DB-->>-API: 删除成功
API->>+Audit: 记录操作日志
Audit-->>-API: 日志已记录
API-->>-Client: 删除成功
```
📝 Intelligent Diagram Design Recommendations
Recommend Diagram Types Based on Content
You: I want to show the dependencies between system modules, what diagram should I use?
Gemini: Based on the system module dependencies you want to show, I recommend the following diagram types:
🎯 Most Recommended: Class Diagram
Suitable for showing static dependencies between modules
```mermaid
classDiagram
CoreModule <-- AuthModule : depends on
CoreModule <-- DataModule : depends on
AuthModule <-- UserModule : uses
DataModule <-- CacheModule : uses
DataModule <-- DBModule : uses
APIModule --> AuthModule : requires
APIModule --> DataModule : requires
UIModule --> APIModule : calls
```
🔄 Alternative 1: Flowchart (with grouping)
Suitable for showing module hierarchy and data flow
```mermaid
graph TB
subgraph "表现层"
UI[UI Module]
end
subgraph "业务层"
API[API Module]
Auth[Auth Module]
end
subgraph "数据层"
Data[Data Module]
Cache[Cache Module]
DB[DB Module]
end
subgraph "基础层"
Core[Core Module]
end
UI --> API
API --> Auth
API --> Data
Auth --> Core
Data --> Cache
Data --> DB
Data --> Core
```
🏗️ Alternative 2: Architecture Diagram (Draw.io)
Suitable for showing complex system architecture and deployment relationships
- Use containers to group related modules
- Use different colors for different layers
- Add icons to enhance readability
AI Optimization Recommendations
🎨 Visual Optimization
AI can help with:
- Recommending color schemes
- Optimizing layout balance
- Adjusting element spacing
- Selecting appropriate fonts
📊 Content Optimization
AI can assist with:
- Simplifying complex processes
- Completing missing steps
- Optimizing text descriptions
- Adding necessary annotations
🔧 Technical Optimization
AI can provide:
- Best practice recommendations
- Performance optimization solutions
- Compatibility checking
- Export format recommendations
🔄 Diagram Conversion and Migration
Converting Between Different Formats
- Mermaid → Draw.io
- Visio → Mermaid
You: Convert this Mermaid flowchart to Draw.io format
Gemini: I'll help you convert the Mermaid diagram to Draw.io:
Original Mermaid:
```mermaid
graph TD
A[开始] --> B{判断}
B -->|是| C[执行]
B -->|否| D[结束]
```
Draw.io implementation steps:
1. Create elements:
- Drag in "Rounded Rectangle" for A (Start)
- Drag in "Diamond" for B (Decision)
- Drag in "Rectangle" for C (Execute)
- Drag in "Rounded Rectangle" for D (End)
2. Set styles:
- A: Fill color #e1f5fe, border #01579b
- B: Fill color #fff3e0, border #e65100
- C: Fill color #f3e5f5, border #4a148c
- D: Fill color #e8f5e9, border #1b5e20
3. Add connections:
- Select "Connector" tool
- Connect from A to B
- Connect from B to C, double-click to add label "Yes"
- Connect from B to D, double-click to add label "No"
4. Layout adjustment:
- Select all elements
- Arrange → Layout → Vertical Tree
- Adjust spacing to appropriate positions
You: How do I convert Visio diagrams to Mermaid code?
Gemini: Methods for converting Visio diagrams to Mermaid:
📋 Manual conversion steps:
1. Analyze Visio diagram structure:
- Identify node types (rectangle, diamond, circle, etc.)
- Record connection relationships
- Extract text labels
2. Map to Mermaid syntax:
Visio shapes → Mermaid syntax
- Rectangle → [text]
- Diamond → {text}
- Circle → ((text))
- Arrow → -->
- Dashed arrow → -.->
3. Conversion example:
Visio: Start(rectangle) → Process(rectangle) → Decision(diamond)
Mermaid:
```mermaid
graph LR
Start[开始] --> Process[处理]
Process --> Decision{判断}
```
🤖 AI-assisted conversion:
You can:
1. Screenshot the Visio diagram
2. Send it to me with a description
3. I'll generate the corresponding Mermaid code
⚙️ Automation tools:
- vsdx2svg: Convert to SVG first, then manually to Mermaid
- Use Draw.io as an intermediate format
🚀 Advanced AI Techniques
Complex System Architecture Generation
You: Generate a complete microservices architecture diagram for an e-commerce platform, including all key components
Gemini: I'll generate a complete e-commerce microservices architecture diagram for you:
```mermaid
graph TB
subgraph "客户端层"
Web[Web应用]
Mobile[移动APP]
Admin[管理后台]
end
subgraph "接入层"
CDN[CDN]
LB[负载均衡器]
Gateway[API网关]
end
subgraph "业务服务层"
subgraph "核心服务"
UserSvc[用户服务]
ProductSvc[商品服务]
OrderSvc[订单服务]
PaymentSvc[支付服务]
end
subgraph "支撑服务"
SearchSvc[搜索服务]
RecSvc[推荐服务]
CartSvc[购物车服务]
InventorySvc[库存服务]
end
subgraph "运营服务"
PromotionSvc[促销服务]
CouponSvc[优惠券服务]
CommentSvc[评价服务]
LogisticsSvc[物流服务]
end
end
subgraph "中间件层"
MQ[消息队列<br/>RabbitMQ/Kafka]
Cache[缓存<br/>Redis Cluster]
Registry[服务注册<br/>Consul/Eureka]
Config[配置中心<br/>Apollo]
end
subgraph "数据层"
MySQL[(MySQL<br/>主从集群)]
MongoDB[(MongoDB<br/>分片集群)]
ES[(ElasticSearch<br/>搜索引擎)]
HBase[(HBase<br/>大数据存储)]
end
subgraph "基础设施"
Monitor[监控<br/>Prometheus]
Log[日志<br/>ELK Stack]
Trace[链路追踪<br/>Zipkin]
Alert[告警<br/>AlertManager]
end
%% 连接关系
Web --> CDN
Mobile --> CDN
Admin --> LB
CDN --> LB
LB --> Gateway
Gateway --> UserSvc
Gateway --> ProductSvc
Gateway --> OrderSvc
Gateway --> PaymentSvc
OrderSvc --> MQ
PaymentSvc --> MQ
MQ --> InventorySvc
MQ --> LogisticsSvc
ProductSvc --> Cache
CartSvc --> Cache
UserSvc --> MySQL
OrderSvc --> MySQL
ProductSvc --> MongoDB
SearchSvc --> ES
%% 样式
classDef client fill:#e3f2fd,stroke:#1976d2
classDef gateway fill:#fff3e0,stroke:#f57c00
classDef service fill:#f3e5f5,stroke:#7b1fa2
classDef middleware fill:#e8f5e9,stroke:#388e3c
classDef data fill:#fce4ec,stroke:#c2185b
classDef infra fill:#f5f5f5,stroke:#616161
class Web,Mobile,Admin client
class CDN,LB,Gateway gateway
class UserSvc,ProductSvc,OrderSvc,PaymentSvc,SearchSvc,RecSvc,CartSvc,InventorySvc,PromotionSvc,CouponSvc,CommentSvc,LogisticsSvc service
class MQ,Cache,Registry,Config middleware
class MySQL,MongoDB,ES,HBase data
class Monitor,Log,Trace,Alert infra
```
Architecture Features:
1. Clear layering: 6-layer architecture with distinct responsibilities
2. Service decoupling: Asynchronous communication via message queues
3. High availability: Multi-level caching, service discovery
4. Observability: Complete monitoring, logging, tracing
5. Scalability: Microservices architecture, independent deployment and scaling
Automatic Documentation Generation
AI Batch Generation of Documentation Diagrams
Use Gemini CLI to automatically generate diagrams from code or documentation:
You: Generate sequence diagrams for all endpoints based on this API documentation
Gemini: I'll batch generate sequence diagrams for your API documentation:
# API Sequence Diagram Documentation
## 1. User Authentication Module
### 1.1 Registration Endpoint
```mermaid
sequenceDiagram
participant C as Client
participant A as API
participant V as Validator
participant D as Database
participant E as Email Service
C->>A: POST /auth/register
A->>V: 验证输入数据
V-->>A: 验证通过
A->>D: 检查邮箱是否存在
D-->>A: 邮箱可用
A->>D: 创建用户记录
D-->>A: 用户创建成功
A->>E: 发送验证邮件
E-->>A: 邮件已发送
A-->>C: 201 Created
```
### 1.2 Login Endpoint
[Continue generating other endpoints...]
## Automation Scripts
You can use the following scripts for batch generation:
```javascript
// Read OpenAPI specification
const spec = require('./openapi.json');
// Generate Mermaid diagrams for each endpoint
Object.keys(spec.paths).forEach(path => {
Object.keys(spec.paths[path]).forEach(method => {
const operation = spec.paths[path][method];
generateSequenceDiagram(path, method, operation);
});
});
```
💡 Prompt Engineering
Efficient Prompt Templates
📊 Flowchart Generation
Generate a flowchart for [topic], including:
1. [Step 1]
2. [Step 2]
3. [Decision conditions]
4. [Error handling]
Requirements:
- Use Mermaid syntax
- Include start and end nodes
- Mark key decision points
- Add necessary loops
🏗️ Architecture Diagram Design
Design an architecture diagram for [system name]:
Tech stack: [list technologies]
Scale: [user volume/QPS]
Requirements:
- Layered display
- Mark technology choices
- Show data flow
- Include redundancy design
🔄 Optimization Request
Optimize this diagram:
[paste existing code]
Optimization directions:
- Improve readability
- Optimize layout
- Unify style
- Add missing information
🎯 Practical Case Studies
Case Study: From Requirements to Diagrams
Requirement Description:
"We need a complete user order process, from selecting products to receiving and reviewing"
Gemini Generation Process:
Step 1: Understanding requirements
- Identify participants: user, system, payment, logistics
- Outline main process: browse → order → pay → ship → receive
- Consider exceptions: out of stock, payment failure, returns
Step 2: Generate basic diagram
```mermaid
graph TD
Start[用户浏览商品] --> Select[选择商品]
Select --> Cart[加入购物车]
Cart --> Checkout[结算]
Checkout --> Pay[支付]
Pay --> Success[支付成功]
Success --> Ship[发货]
Ship --> Receive[收货]
Receive --> Review[评价]
Review --> End[完成]
```
Step 3: Add exception handling
```mermaid
graph TD
Start[用户浏览商品] --> Select[选择商品]
Select --> Stock{库存充足?}
Stock -->|否| StockOut[提示缺货]
StockOut --> Select
Stock -->|是| Cart[加入购物车]
Cart --> Checkout[结算]
Checkout --> Pay[支付]
Pay --> PayResult{支付成功?}
PayResult -->|否| PayFail[支付失败]
PayFail --> Pay
PayResult -->|是| Ship[发货]
Ship --> Receive[收货]
Receive --> Confirm{确认收货?}
Confirm -->|是| Review[评价]
Confirm -->|否| Return[退货流程]
Review --> End[完成]
Return --> Refund[退款]
Refund --> End
```
Step 4: Optimize style and layout
[Final optimized version...]
🚀 Next Steps
After mastering AI-assisted techniques, start practicing: