union-py-app-stream-chat
v1.1.0
Published
Source package for the union operations stream chat Python app.
Readme
GLM 运维智能助手 (OpsAssistant)
基于智谱 GLM 大模型的运维平台运行表现问答服务。
项目结构
glm-ops-assistant/
├── config/
│ └── config.yaml # 配置文件(API Key、模型参数、过滤规则等)
├── src/
│ ├── api/
│ │ └── routes.py # View 层 - FastAPI 路由接口
│ ├── service/
│ │ └── chat_service.py # Service 层 - 业务逻辑(问题筛选、流程编排)
│ ├── manager/
│ │ └── toolcall_manager.py # Manager 层 - 工具调用封装
│ ├── core/
│ │ ├── config_loader.py # 核心工具 - 配置加载(支持环境变量覆盖)
│ │ └── logging_config.py # 核心工具 - 日志配置
│ └── models/
│ └── schemas.py # Pydantic 数据模型
├── tests/
│ └── test_chat_service.py # 单元测试
├── main.py # 服务入口
├── requirements.txt # 依赖清单
└── README.md快速开始
1. 安装依赖
pip install -r requirements.txt2. 配置
编辑 config/config.yaml,填入你的 GLM API Key:
llm:
api_key: "your-api-key-here"
model: "glm-4-flash"或通过环境变量覆盖:
export APP_CONFIG_PATH="/path/to/config.yaml"
export LLM_API_KEY="your-api-key"
export LLM_MODEL="glm-4-flash"
export RAG_ENABLED="true"3. 启动服务
uvicorn main:app --host 0.0.0.0 --port 8000 --reload启动后访问文档:http://localhost:8000/docs
API 接口
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | /api/v1/chat/stream | 流式对话(SSE) |
| GET | /api/v1/health | 健康检查 |
多轮对话
通过 session_id 标识会话,系统自动维护上下文历史。历史记录保存在内存中,支持 TTL 过期清理。
问题筛选
服务会自动判断用户问题是否与系统运行表现相关(CPU、内存、磁盘、网络、告警、故障等)。
不符合条件的问题会被直接拒绝,返回预设提示语。筛选关键词和提示语可在 config.yaml 中自定义。
流式输出
调用 /api/v1/chat/stream 接口,服务端使用 SSE 协议逐字推送模型生成内容。
每条 message 事件的 data 为 ChatResponse JSON:
{
"session_id": "sess-xxx",
"content": "正式回复增量",
"reasoning_content": null,
"tool_call": null,
"tool_result": null,
"finish_reason": null
}模型正式回复写入 content,推理内容写入 reasoning_content,工具过程写入 tool_call / tool_result。
