FHIR聊天应用

一个基于Python和PydanticAI的结构化应用,使用Aidbox FHIR MCP服务器实现实时聊天和临床数据查询功能。

  • by awead
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 awead/fhir-chat

使用说明

FHIR边聊

一个结构化的Python应用程序,使用PydanticAI和一个Aidbox kubectl MCP服务器。

配置

您将需要:

  • 辅助箱许可证
  • OpenAI访问
    • 端点
    • 钥匙
    • 模型(目前正在使用gpt-5-mini进行测试)
    • API版本(目前正在测试2024-12-01预览版)

看 env-sample.txt

用法

先决条件

  • 后端: Python 3.13+, uv 包管理器
  • 前端: Node.js 18+和npm
  • 服务: Docker和Docker Compose

快速开始

  1. 启动服务:
docker compose up -d
  1. 启动API后端:
uv run uvicorn src:app --host 0.0.0.0 --port 8000 --reload
  1. 启动前端(在新终端中):
cd frontend
npm install  # First time only
npm run dev
  1. 打开聊天界面:

访问http://localhost:5173在浏览器中

前端开发

前端是用Vite构建的React+TypeScript应用程序。

可用脚本:

  • npm run dev -启动开发服务器(http://localhost:5173)
  • npm run build -生产建设(产出 frontend/dist/)
  • npm run preview -预览生产构建
  • npm run lint -运行ESLint

特征:

  • 实时WebSocket聊天通信
  • 具有本地存储持久性的会话管理
  • 自动滚动聊天界面
  • 支持带打字机效果的流媒体消息
  • 实时遥测面板显示OpenAI和MCP/Aidbox轨迹(延迟<100ms)
  • 具有自动重新连接功能的连接状态指示灯
  • 加载状态和错误处理

故障排除:

  • CORS错误: 确保后端正在运行,并且为localhost:5173配置了CORS
  • 后端连接失败: 检查后端是否在端口8000上运行
  • 端口5173正在使用中: Vite将自动递增到5174,或使用 npm run dev -- --port 3000

生产建设

要从FastAPI后端构建和服务前端:

  1. 构建前端:
cd frontend
npm run build

这将在中创建优化的静态文件 frontend/dist/.

  1. 启动后端:
uv run uvicorn src:app --host 0.0.0.0 --port 8000
  1. 访问应用程序:

访问http://localhost:8000在您的浏览器中。如果满足以下条件,后端会自动为前端提供服务 frontend/dist/ 存在。

笔记:

  • API路线(/ws, /patient)优先于静态文件
  • 前端使用客户端路由和SPA回退
  • 捆绑包大小:CSS ~12KB,JS ~250KB(gzip压缩:~3KB CSS,~79KB JS)

Websocket API

聊天界面使用WebSocket进行实时双向通信。

连接URL:

ws://localhost:8000/ws?session_id=<your-session-id>

消息格式:

所有消息均为JSON格式,并带有 type 路由字段。

客户端→ 服务器(用户消息):

{
  "type": "message",
  "session_id": "your-session-id",
  "content": "What is quintin cole's diagnosis?"
}

服务器→ 客户:

助理回应:

{
  "type": "assistant",
  "session_id": "your-session-id",
  "content": "Based on the clinical data...",
  "streaming": false
}

OpenAI呼叫事件:

{
  "type": "openai_call",
  "session_id": "your-session-id",
  "model": "gpt-4",
  "timestamp": "2025-11-20T22:00:00Z"
}

OpenAI响应事件:

{
  "type": "openai_response",
  "session_id": "your-session-id",
  "model": "gpt-4",
  "duration_ms": 1234,
  "prompt_tokens": 150,
  "completion_tokens": 50,
  "total_tokens": 200,
  "timestamp": "2025-11-20T22:00:01Z"
}

错误消息:

{
  "type": "error",
  "session_id": "your-session-id",
  "error": "Invalid message format"
}

使用wscat进行测试:

npm install -g wscat
wscat -c "ws://localhost:8000/ws?session_id=test123"
> {"type": "message", "session_id": "test123", "content": "Hello"}

遥测:

  • WebSocket流中出现实时事件(延迟<100ms)
  • 历史遥测数据可用 Jaeger用户界面

生成患者临床病史

请求:

curl -X POST http://localhost:8000/patient \
  -H 'Content-Type: application/json' \
  -d '{"patient_id":"<FHIR Patient UUID>"}'

示例响应(截断):

{
  "patient_id": "123e4567-e89b-12d3-a456-426614174000",
  "patient_name": "Jane Doe",
  "clinical_summary": "Patient with a history of hypertension and diabetes, currently stable.",
  "key_conditions": ["Hypertension", "Type 2 Diabetes Mellitus"],
  "active_medications": ["Lisinopril", "Metformin"],
  "recent_encounters": ["2025-11-01: Routine follow-up", "2025-10-10: Lab work"],
  "generated_at": "2025-11-14T20:00:00Z"
}

⚠️ 安全警告: WebSocket遥测事件可能包含敏感数据,包括:

  • OpenAI提示和完成
  • MCP Contoso查询和响应
  • 潜在PHI(受保护的健康信息)

当前配置:

  • WebSocket连接接受任何session_id(无身份验证)
  • CORS已启用 localhost:3000 和 localhost:5173 (前端开发服务器)
  • 仅限开发 -无身份验证或加密
  • 生产要求: 使用 wss:// (TLS),实现JWT身份验证,添加速率限制,从遥测中过滤PHI

API 文档

交互式API文档可在以下网址获取:

  • Swagger用户界面:http://localhost:8000/docs
  • 重新记录:http://localhost:8000/redoc

备注

  • 仅限记忆会话;替换 ChatService 为了坚持。
  • 确保Aidbox健康检查通过;SSE端点为 http://localhost:8080/sse.