Files
fastapi/README.md
T
2026-06-08 16:49:45 +08:00

194 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 医疗问诊 Agent FastAPI 后端
医疗问诊 Agent 是医疗教学平台中的训练服务。后端负责 Django 用户鉴权、病例读取、训练会话、流式问诊、练习提示、检查结果、诊断治疗提交、AI 评价、教学互动评价、训练记录和 PDF 下载。
病例新增、病例解析、病例导入和病例删除不在本服务中实现。本服务只读取数据库中已经维护好的病例、检查项、教学题和评分规则。
## 当前保留功能
训练页面:
- 推荐配置信息
- 训练配置信息
- 新建会话
- 流式会话
- 王主任练习提示
- 体格检查列表获取
- 辅助检查列表获取
- 体格检查某项结果
- 辅助检查某项结果
- 完成问诊
- 提交诊断
- 提交治疗
- 生成评价
- 获取评价详情
- 下载 PDF
教学互动:
- 获取教学列表,包含题目、选项、答案、解析文本和视频
- 生成评价
- 获取评价详情
- 下载 PDF
个人中心:
- 训练记录列表
- 训练记录详情
基础能力:
- Django access token 鉴权
- MySQL 数据读取和训练记录写入
- Redis 短期会话 memory
- OpenAI-compatible LLM 调用
- Swagger / OpenAPI
- 健康检查
## 项目结构
```text
fastapi/
├── app/ # FastAPI 应用、Agent、服务、模型和提示词
├── scripts/ # 数据库初始化和检查脚本
├── tests/ # 当前功能测试
├── docs/03_api_design.md # 前端联调 API 文档
├── Dockerfile
├── requirements.txt
├── .env.example
└── .env.production.example
```
## 本地启动
```powershell
python -m venv .venv
.\.venv\Scripts\activate
pip install -r requirements.txt
copy .env.example .env
uvicorn app.main:app --host 127.0.0.1 --port 9000
```
本地 Swagger
```text
http://127.0.0.1:9000/docs
```
真实密码、LLM Key 和 access token 只写入本地 `.env` 或服务器环境变量,不提交到 Git。
## 服务器部署
服务器目录示例:
```text
/home/code/medical-ai/
├── django/
├── fastapi/
├── vueapp/
├── vuecms/
└── docker-compose.yml
```
首次拉取:
```bash
cd /home/code/medical-ai
git clone http://82.157.235.104:3000/Liu_JB/LiuJinbao.git fastapi
cp fastapi/.env.production.example fastapi/.env
vi fastapi/.env
```
服务器 `.env` 至少配置:
```env
APP_ENV=production
APP_ROOT_PATH=/fastapi
DATABASE_URL=mysql+pymysql://root:1822..@mysql:3306/medical?charset=utf8mb4
REDIS_URL=redis://redis:6379/0
RUNTIME_MEMORY_BACKEND=redis
AUTH_VALIDATE_ENABLED=true
AUTH_USER_ME_URL=http://django:8000/api/user/users/me/
LLM_BASE_URL=<模型服务地址>
LLM_API_KEY=<模型密钥>
LLM_MODEL=<模型名称>
LLM_FAST_MODEL=<模型名称>
LLM_REASON_MODEL=<模型名称>
CORS_ALLOW_ORIGINS=http://8.160.178.88
```
构建并启动:
```bash
cd /home/code/medical-ai
docker compose build fastapi
docker compose up -d fastapi
docker compose logs --tail=200 fastapi
```
后续更新:
```bash
cd /home/code/medical-ai/fastapi
git pull origin main
cd ..
docker compose build fastapi
docker compose up -d fastapi
```
## 验证
公网验证:
```text
http://8.160.178.88/fastapi/docs
http://8.160.178.88/fastapi/openapi.json
http://8.160.178.88/fastapi/health/ready
```
Django 用户鉴权验证:
```bash
curl "http://8.160.178.88/fastapi/api/v1/auth/me" \
-H "Authorization: Bearer <access_token>" \
-H "X-Entry-Scene: production_vue"
```
PDF 下载验证:
```bash
curl -L "http://8.160.178.88/fastapi/api/v1/evaluations/<evaluation_id>/download-pdf" \
-H "Authorization: Bearer <access_token>" \
-H "X-Entry-Scene: production_vue" \
-o evaluation_report.pdf
```
## 测试
```powershell
python -m compileall app scripts tests
python tests\test_core_logic.py
python tests\test_api_contract.py
python tests\test_demo_flow.py
```
测试覆盖:
- Django token 鉴权和 user_id 隔离
- 推荐配置和训练配置
- 新建会话、流式问诊、王主任练习提示
- 体格检查和辅助检查列表
- 单项检查结果和重复申请幂等
- 完成问诊、提交诊断、提交治疗、生成评价
- 教学互动列表和教学互动评价
- 训练记录列表、评价详情、PDF 下载
- 跨用户访问拦截
## API 文档
前端联调文档见:
```text
docs/03_api_design.md
```