内容
# Healthcare MCP Server
一个复杂的模型上下文协议(Model Context Protocol,MCP)服务器,将Google的Gemini LLM与先进的医疗AI能力集成,用于医疗应用。该系统利用最先进的机器学习模型提供智能医疗分析、诊断辅助和健康洞察。
## 特性
- 🏥 **高级医疗分析**
- 使用多模型方法进行症状分析
- 医学图像处理和分类
- 基于证据的治疗建议
- 健康指标监测和分析
- 🤖 **AI集成**
- Google Gemini LLM集成
- 医学特定的NLP模型
- 医学成像的计算机视觉
- 专业的医学嵌入
- 👨⚕️ **医疗管理**
- 患者和医生门户
- 预约调度
- 医疗历史跟踪
- 安全认证系统
- 📊 **健康洞察**
- 个性化健康建议
- 风险因素识别
- 生命体征监测
- 趋势分析和报告
## 先决条件
- Python 3.8+
- pip包管理器
- Google Gemini API密钥
- 虚拟环境(推荐)
- 存储要求:
- 至少20GB的可用磁盘空间用于AI模型
- 模型缓存存储在 `C:\Users\<username>\.cache\huggingface\hub`
- 首次运行将下载所需模型(15-30分钟)
## 设置
1. 创建并激活虚拟环境:
```bash
python -m venv venv
# 在Windows上:
.\venv\Scripts\activate
# 在Unix/MacOS上:
source venv/bin/activate
```
2. 安装依赖:
```bash
pip install -r requirements.txt
```
3. 配置环境:
- 在项目根目录创建一个 `.env` 文件
- 添加所需的环境变量:
```
GOOGLE_API_KEY=your-gemini-api-key
SECRET_KEY=your-flask-secret-key
```
## 运行服务器
1. 初始化数据库:
```bash
python server.py --init-db
```
2. 启动服务器:
```bash
python launcher.py
```
服务器将在 `http://localhost:5000` 可用
## 命令行使用
服务器提供了多个命令行选项,用于管理AI模型和运行系统:
### 基本用法
1. 启动服务器(自动下载模型):
```bash
python launcher.py
```
2. 启动时不下载缺失的模型:
```bash
python launcher.py --download-models=false
```
### 模型管理
1. 查看模型信息和存储使用情况:
```bash
python launcher.py --model-info
```
显示:
- 总存储使用量
- 已下载的模型
- 缺失的必需模型
2. 清除模型缓存:
```bash
# 清除特定模型缓存:
python launcher.py --manage-models --clear-cache medical_nlp
python launcher.py --manage-models --clear-cache vision
python launcher.py --manage-models --clear-cache medical_bert
# 清除所有模型缓存:
python launcher.py --manage-models --clear-cache
```
3. 强制下载缺失的模型:
```bash
python launcher.py --download-models
```
### 高级选项
- `--manage-models`:进入模型管理模式
- `--clear-cache MODEL_ID`:清除特定模型的缓存
- `--model-info`:显示模型存储信息
- `--download-models`:强制下载缺失的模型
### 示例
1. 检查系统状态而不启动:
```bash
python launcher.py --model-info
```
2. 清除所有缓存并重新开始:
```bash
python launcher.py --manage-models --clear-cache
python launcher.py --download-models
```
3. 最小启动(仅限云功能):
```bash
python launcher.py --download-models=false
```
## 文档
全面的文档可在 `/docs` 目录中找到:
### 1. 技术指南
[技术指南](docs/technical_guide.md)
- 系统架构和组件
- 开发设置和指南
- 部署说明
- 安全考虑
- 故障排除指南
- 性能监控
### 2. API参考
[API参考](docs/api_reference.md)
- 完整的API端点文档
- 认证方法
- 请求/响应格式
- 错误处理
- 速率限制
- API版本控制
### 3. 用户指南
[用户指南](docs/user_guide.md)
- 入门指南
- 患者门户说明
- 医生门户指南
- 常见功能演练
- 安全最佳实践
- 紧急程序
有关代码库的内联文档:
- 检查 `server.py` 中的文档字符串以获取详细的函数和类文档
- 查看 `config/mcp_config.py` 中的配置选项
- 检查 `resources/` 目录中的医学资源
## Docker部署
### 先决条件
- 系统上安装Docker和Docker Compose
- Google Gemini API密钥
### 使用Docker Compose(推荐)
1. 创建一个包含凭据的 `.env` 文件:
```
GOOGLE_API_KEY=your-gemini-api-key
SECRET_KEY=your-flask-secret-key
```
2. 启动服务:
```bash
docker-compose up -d
```
3. 查看日志:
```bash
docker-compose logs -f
```
4. 停止服务:
```bash
docker-compose down
```
### 替代方案:使用Docker CLI
```bash
docker build -t healthcare-mcp .
```
### 运行容器
1. 创建一个包含凭据的 `.env` 文件:
```
GOOGLE_API_KEY=your-gemini-api-key
SECRET_KEY=your-flask-secret-key
```
2. 运行容器:
```bash
docker run -d \
--name healthcare-mcp \
-p 5000:5000 \
--env-file .env \
-v healthcare_models:/root/.cache/huggingface/hub \
-v healthcare_data:/app/instance \
healthcare-mcp
```
此命令:
- 将5000端口映射到主机
- 从 .env 加载环境变量
- 为AI模型和数据库创建持久卷
- 以分离模式运行容器
3. 查看日志:
```bash
docker logs -f healthcare-mcp
```
### 存储卷
- `healthcare_models`:存储下载的AI模型(约20GB)
- `healthcare_data`:存储SQLite数据库和应用数据
### 维护
- 重启容器:`docker restart healthcare-mcp`
- 停止容器:`docker stop healthcare-mcp`
- 删除容器:`docker rm healthcare-mcp`
- 删除卷:`docker volume rm healthcare_models healthcare_data`
## 项目结构
```
.
├── launcher.py # 应用入口
├── server.py # 主要的MCP服务器实现
├── requirements.txt # 项目依赖
│
├── config/
│ └── mcp_config.py # 配置设置
│
├── resources/
│ ├── medical_ontology.json # 医学知识库
│ └── prompt_templates.json # LLM提示模板
│
├── templates/ # Web界面模板
│ ├── base.html
│ ├── index.html
│ ├── login.html
│ ├── register.html
│ ├── doctor_dashboard.html
│ └── patient_dashboard.html
│
└── tools/ # 专业医疗AI工具
├── medical_nlp.py # 自然语言处理
├── medical_imaging.py # 图像分析
└── health_insights.py # 健康指标分析
```
## 可用工具
### 1. 医疗分析工具
#### 症状分析
```python
analyze_symptoms(symptoms: List[str], ctx: Context, patient_data: Optional[dict] = None) -> Dict
```
- 使用医学NLP模型分析症状
- 提供潜在诊断及其置信水平
- 评估紧急性并建议立即采取的行动
#### 医学图像分析
```python
medical_image_analysis(image_path: str, ctx: Context) -> Dict
```
- 使用计算机视觉处理医学图像
- 支持各种医学成像格式
- 提供详细的分析和分类
#### 治疗建议
```python
get_treatment_suggestions(condition: str, patient_history: str, ctx: Context) -> Dict
```
- 生成基于证据的治疗计划
- 考虑患者历史和上下文
- 提供替代治疗选项
### 2. 健康洞察工具
#### 健康指标分析
```python
generate_health_insights(patient_data: Dict, ctx: Context) -> Dict
```
- 分析患者健康指标
- 识别风险因素
- 提供个性化建议
### 3. LLM集成
#### 文本生成
```python
generate_text(prompt: str, ctx: Context) -> str
```
- 与Gemini LLM集成
- 适当地处理医学上下文
- 实施安全过滤器
## 安全特性
- 安全认证系统
- 基于角色的访问控制
- 密码哈希
- 会话管理
- 环境变量保护
## 医疗数据资源
该系统使用全面的医疗资源:
1. **医学本体**
- 症状类别
- 疾病分类
- 治疗方案
- 风险评估指南
2. **提示模板**
- 诊断模板
- 治疗计划
- 患者教育
- 健康监测
## 贡献
1. Fork该仓库
2. 创建一个功能分支
3. 进行更改
4. 提交拉取请求
## 许可证
MIT许可证 - 详见LICENSE文件
## 支持
如需支持和问题,请在仓库中打开一个问题。
## 致谢
- Google Gemini AI
- 医学NLP社区
- 医疗AI研究人员
- 开源医疗数据集
## 版本历史
- v1.0.0 - 初始发布
- v1.1.0 - 添加医学成像支持
- v1.2.0 - 增强健康洞察
- v1.3.0 - 改进NLP能力