Hoseinrajabi1997/incident-response-agent

GitHub: Hoseinrajabi1997/incident-response-agent

一款基于ReAct模式的AI驱动事件响应Agent,能自动关联告警、日志、指标和部署信息,为工程团队提供根因分析和处置建议。

Stars: 0 | Forks: 0

# 事件响应 Agent 🚨 一款面向工程团队的 AI 驱动事件响应 Agent,能够读取告警、日志、仪表盘和部署历史,随后提供可能的根本原因及后续操作建议。 本项目旨在演示结合了 **ReAct (Reason + Act) 模式** 与 **LLM 工具调用** 的 **智能体 AI 工作流**。 ## 架构 ``` ┌─────────────────────────────────────────────────────────┐ │ CLI Interface │ │ (investigate / alerts / timeline / postmortem) │ └──────────────────────┬──────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────┐ │ Incident Agent (ReAct Loop) │ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │ │ │ REASON │──▶│ ACT │──▶│ OBSERVE │ │ │ │ (LLM │ │ (call │ │ (feed results │ │ │ │ thinks) │ │ tools) │ │ back to LLM) │ │ │ └──────────┘ └──────────┘ └──────────────────┘ │ │ ▲ │ │ │ └──────────────────────────────┘ │ │ repeat until diagnosis │ └──────────────────────┬──────────────────────────────────┘ │ ┌────────────┼────────────┐ ▼ ▼ ▼ ┌──────────────┐ ┌──────────┐ ┌──────────────┐ │ Tools │ │ Generators│ │ Integrations │ │ │ │ │ │ │ │ • get_alerts │ │ • timeline│ │ • Datadog │ │ • get_logs │ │ • postmort│ │ • Grafana │ │ • get_metrics│ │ │ │ • CloudWatch │ │ • get_deploys│ └───────────┘ │ • GitLab │ │ • detect_ │ └──────────────┘ │ changes │ │ • suggest_ │ │ actions │ └──────────────┘ ``` ## Agent 运行机制 Agent 遵循 **ReAct 模式** —— 这也是生产级 AI Agent 所采用的相同方案: 1. **系统提示词**: Agent 接收一个包含调查指令的 SRE 角色设定 2. **用户触发**: "api-gateway 出现高延迟" 或 "调查当前告警" 3. **推理**: LLM 决定调用哪个工具及调用原因 4. **行动**: Agent 调用工具(例如 `get_alerts`, `get_logs`) 5. **观察**: 工具执行结果作为上下文反馈给 LLM 6. **循环**: 重复步骤 3-5,直到 Agent 收集到足够的证据 7. **诊断**: Agent 输出结构化的事件分析结果 该机制通过 **OpenAI function calling** 实现 —— LLM 解析工具的 schema 结构,并发出由 Agent 执行的结构化工具调用请求。 ## 快速开始 ### 1. 安装 ``` cd Incident-Response-agent python -m venv .venv source .venv/bin/activate pip install -e . ``` ### 2. 配置 ``` cp .env.example .env # 编辑 .env — 设置你的 OPENAI_API_KEY # 默认开启 Demo 模式(使用 mock 数据,无需真实 integrations) ``` ### 3. 运行 ``` # 完整 agent 调查(需要 OpenAI API key) incident-agent investigate "High latency on the api-gateway, payment errors rising" # 快速 commands(在 demo 模式下无需 API key 即可工作) incident-agent alerts # Show active alerts incident-agent deploys # Show recent deployments incident-agent changes # Detect recent changes by risk incident-agent timeline # Generate incident timeline incident-agent postmortem # Generate postmortem draft incident-agent status # Show configuration status ``` ## 演示场景 内置的 Demo 模拟了一场贴近现实的生产事件: Agent 将会: 1. 拉取告警并发现有多个服务受到影响 2. 检查日志并发现连接池耗尽的错误 3. 审查监控指标,展示出流量的激增模式 4. 检测到最近的部署为最高风险变更 5. 关联时间线并建议执行回滚操作 ## 项目结构 ``` src/incident_agent/ ├── main.py # CLI entry point (Typer) ├── agent.py # Core ReAct agent loop ← the key learning file ├── config.py # Configuration from environment ├── models.py # Pydantic data models ├── tools/ # Tools the LLM can call │ ├── registry.py # Tool registration & execution │ ├── alerts.py # Fetch alerts from monitoring │ ├── logs.py # Query application logs │ ├── metrics.py # Query metric time-series │ ├── deploys.py # Query deployment history │ ├── changes.py # Detect recent changes │ └── actions.py # Suggest SRE playbook actions ├── integrations/ # Real API clients (optional) │ ├── datadog_client.py │ ├── grafana_client.py │ ├── cloudwatch_client.py │ └── gitlab_client.py ├── generators/ # Output generators │ ├── timeline.py # Incident timeline builder │ └── postmortem.py # Postmortem draft generator └── demo/ └── mock_data.py # Realistic mock incident data ``` ## 核心学习概念 ### 1. ReAct 模式 (`agent.py`) 这是 LLM 进行推理、调用工具、观察结果并不断循环的核心逻辑。这也是大多数生产级 Agent 的运行方式(LangChain、CrewAI、AutoGPT 均采用了此模式的变体)。 ### 2. 工具注册 (`tools/registry.py`) 工具使用 JSON Schema 进行定义,以便 LLM 知晓其所需参数。注册中心(registry)负责将工具名映射到对应的 Python 函数并处理执行逻辑。 ### 3. Function Calling (`agent.py` → OpenAI API) LLM 本身并不执行代码 —— 它只输出结构化的工具调用请求。Agent 框架负责执行这些请求并将结果反馈给 LLM。这就是所谓的 "tool use"(工具使用)模式。 ### 4. 数据关联 (`tools/changes.py`) 通过将部署时间戳与告警时间戳进行关联比对,来检测"最近的变更内容"。这是大多数事件调查的核心环节。 ### 5. 结构化输出 (`generators/`) Agent 会生成结构化的产出物(如时间线、复盘报告),这些内容可直接应用于事件响应工作流中。 ## 扩展 Agent ### 添加新工具 1. 创建 `src/incident_agent/tools/your_tool.py` 2. 定义包含 `name`、`description`、`parameters` 和 `function` 的 `TOOL_DEF` 字典 3. 在 `agent.py` 中导入,并将其添加到 `build_tool_registry()` 中 ### 接入真实系统集成 1. 在 `src/incident_agent/integrations/` 中实现相应的客户端 2. 将配置字段添加到 `config.py` 中 3. 更新对应的工具,使其在 `config.demo_mode` 为 `False` 时调用真实的客户端 ### 自定义 Agent 行为 编辑 `agent.py` 中的 `SYSTEM_PROMPT` 即可更改 Agent 的调查策略、输出格式或角色设定。 ## 技术栈 - **Python 3.11+** — 使用 async/await 处理并发操作 - **OpenAI API** — 使用支持 function calling 的 GPT-4o 作为 Agent 的大脑 - **Pydantic** — 数据校验与序列化 - **Typer + Rich** — 包含表格和面板的美观 CLI 界面 - **httpx** — 用于系统集成的异步 HTTP 客户端
标签:Agentic AI, AIOps, AI智能体, DLL 劫持, IT运维, LLM工具调用, Petitpotam, Postmortem, ReAct模式, Socks5代理, SRE, 事故复盘, 人工智能, 偏差过滤, 告警分析, 大语言模型, 子域名变形, 密码管理, 工程团队, 库, 应急响应, 故障排查, 智能运维, 机器推理, 根本原因分析, 用户模式Hook绕过, 网络安全审计, 自动化运维, 运行时操纵, 逆向工具, 部署历史