分布式知识问答服务架构设计文档
1. 系统概述
目标:构建支持海量非结构化数据处理的分布式问答系统,实现:
- 多格式文件自动向量化存储
- 基于大语言模型的上下文感知问答
- 弹性可扩展的微服务架构
2. 整体架构设计
2.1 架构图
graph TD
subgraph 分布式集群
A[API Gateway] --> B[文件处理服务]
A --> C[问答服务]
B --> D[(向量数据库)]
C --> D
C --> E[[大模型服务]]
F[注册中心] -.-> B & C
G[监控中心] -.-> B & C & D & E
end
H[用户] -->|上传/提问| A
3. 核心组件选型
| 组件类型 | 选型方案 | 版本要求 | 选型理由 |
|---|
| 向量数据库 | Milvus | 2.3.x+ | 专为向量优化,支持GPU加速和分布式集群 |
| 文件解析引擎 | Apache Tika | 2.4.x+ | 支持超过1000种文件格式的文本提取 |
| 向量模型 | all-MiniLM-L6-v2 | ONNX格式 | 轻量级模型(384维),适合生产环境部署 |
| 大模型服务 | Ollama+DeepSeek | 0.1.x+ | 本地化部署,支持自定义模型微调 |
| 服务框架 | Spring Cloud | 2022.x+ | 完善的微服务生态,支持熔断/限流/负载均衡 |
| 消息队列 | Kafka | 3.5.x+ | 实现文件解析任务的异步处理 |
| 容器编排 | Kubernetes | 1.27.x+ | 支持自动扩缩容和滚动升级 |
4. 服务详细设计
4.1 文件处理服务
处理流程图
sequenceDiagram
participant Client
participant Gateway
participant FileService
participant Kafka
participant Tika
participant VectorDB
Client->>Gateway: POST /upload (file)
Gateway->>FileService: 转发请求
FileService->>Kafka: 提交解析任务
Kafka-->>FileService: 返回任务ID
FileService-->>Client: 202 Accepted
loop 异步处理
FileService->>Tika: 提取文本
Tika-->>FileService: 结构化文本
FileService->>VectorModel: 生成向量
VectorModel-->>FileService: 384维向量
FileService->>VectorDB: 存储向量+元数据
end
关键接口设计
@PostMapping("/upload")
ResponseEntity<UploadResponse> uploadFile(
@RequestParam("file") MultipartFile file,
@RequestHeader("X-Tenant-ID") String tenantId
);
public class VectorData {
private String docId;
private float[] embedding;
private Metadata metadata;
}
4.2 问答服务
处理流程图
sequenceDiagram
participant Client
participant Gateway
participant QAService
participant VectorDB
participant LLM
Client->>Gateway: POST /ask (question)
Gateway->>QAService: 转发请求
QAService->>VectorModel: 生成问题向量
VectorModel-->>QAService: 向量数据
QAService->>VectorDB: ANN搜索Top5
VectorDB-->>QAService: 相关上下文
QAService->>LLM: 构建Prompt请求
LLM-->>QAService: 生成回答
QAService-->>Client: 结构化响应
Prompt模板设计
你是一个专业的知识助手,请严格基于以下背景信息回答问题。
若问题与背景无关,请回答"该问题不在知识库范围内"。
[背景资料]
{{context_1}}
{{context_2}}
...
[问题]
{{question}}
[回答要求]
1. 使用中文口语化表达
2. 分点说明,不超过3点
3. 重要数据需标注来源段落
5. 数据库设计
5.1 Milvus集合结构
collection_name = "knowledge_base"
schema = {
"auto_id": True,
"description": "知识库向量存储",
"fields": [
{"name": "embedding", "type": DataType.FLOAT_VECTOR, "dim": 384},
{"name": "doc_id", "type": DataType.VARCHAR, "max_length": 64},
{"name": "content", "type": DataType.VARCHAR, "max_length": 4096},
{"name": "source", "type": DataType.VARCHAR, "max_length": 256}
]
}
5.2 索引配置
index_type: IVFLAT
metric_type: IP
params:
nlist: 1024
search_params:
nprobe: 32
6. 分布式部署方案
6.1 Kubernetes部署拓扑
graph TB
subgraph K8s Cluster
ingress[Ingress Nginx] -->|路由| svc-gateway[API Gateway Service]
svc-gateway --> pod-gateway1[Gateway Pod]
svc-gateway --> pod-gateway2[Gateway Pod]
pod-file1[File Service Pod] --> svc-milvus[Milvus Service]
pod-file2[File Service Pod] --> svc-milvus
pod-qa1[QA Service Pod] --> svc-milvus
pod-qa2[QA Service Pod] --> svc-milvus
svc-milvus --> pod-milvus1[Milvus Node]
svc-milvus --> pod-milvus2[Milvus Node]
svc-milvus --> pod-milvus3[Milvus Node]
end
6. 资源配额建议
| 服务类型 | CPU | 内存 | 副本数 | 存储 |
|---|
| API Gateway | 2核 | 4GB | 2 | 无 |
| 文件处理服务 | 4核 | 8GB | 3 | 临时存储20GB |
| 问答服务 | 8核 | 16GB | 4 | 无 |
| Milvus节点 | 16核 | 64GB | 3 | SSD 500GB |
7. 扩展性设计
7.1 性能优化策略
- 向量检索层:
- 启用GPU加速索引构建(Milvus CUDA支持)
- 使用批处理API提升吞吐量
- 模型推理层:
- 部署模型并行化(Ollama多GPU支持)
- 实现结果缓存(Redis缓存高频问答对)
- 数据预处理:
7.2 容灾方案
graph LR
primary[主数据中心] -- 数据同步 --> replica[备份数据中心]
style primary fill:#e6f3ff,stroke:#333
style replica fill:#ffe6e6,stroke:#333
primary -->|实时| S3[(S3存储)]
replica -->|异步| S3
8. 监控指标
8.1 Prometheus监控项
| 指标类型 | 具体指标 | 告警阈值 |
|---|
| 服务健康 | up{service="file-service"} | <1 持续1分钟 |
| 处理延迟 | file_process_latency_seconds | P99 > 30s |
| 向量检索 | milvus_search_duration_ms | 平均值 > 500ms |
| 模型推理 | ollama_inference_tokens_second | <100 tokens/s |
| 资源使用 | container_memory_usage_bytes | >80% 持续5分钟 |
9. 安全设计
9.1 安全控制点
graph TD
A[身份认证] --> B[OAuth2/JWT]
C[权限控制] --> D[RBAC策略]
E[数据安全] --> F[传输加密(TLS1.3)]
E --> G[存储加密(AES-256)]
H[审计追踪] --> I[操作日志+IP追踪]
10. 注意事项
- 模型版本控制:
- 数据治理:
- 设置自动清理策略(如30天未访问数据转冷存储)
- 实现敏感词过滤模块
- 法律合规:
- 添加用户协议确认流程
- 记录问题溯源日志(保留6个月)