OnCallAgent:Go语言智能运维值班代理系统
GitHub: https://github.com/yinxiangpingfan/OnCallAgent
技术栈: Go / Gin / CloudWeGo Eino / Qdrant / Ollama / Prometheus
发布平台: 程序员茄子(chenxutan.com)
标签: AI Agent, 智能运维, RAG, ReAct, DevOps, 告警处理
引言
运维值班是每个技术团队的痛点。
当告警响起时,值班人员需要:
- 快速判断告警类型
- 查找历史处理方案
- 执行修复操作
- 记录处理过程
OnCallAgent 应运而生。
这是一个融合 RAG、ReAct 与 Plan-Execute-Replan 三大 AI 范式的智能运维平台,让 AI 帮你值班!
一、项目简介
OnCallAgent 是一个面向运维场景的智能代理系统,深度融合三种 AI Agent 核心技术:
| 核心技术 | 说明 |
|---|---|
| RAG | 检索增强生成,基于向量数据库的知识检索 |
| ReAct | 推理+行动,对话场景下的工具调用范式 |
| Plan-Execute-Replan | 规划-执行-重规划,复杂任务的自主执行框架 |
三者协同工作
RAG → 提供知识基础
ReAct → 处理单轮工具调用
Plan-Execute-Replan → 编排多步骤任务流程
共同实现:告警发现 → 知识检索 → 处理建议生成 → 全链路自动化运维
二、解决的问题
| 痛点 | 传统方式 | OnCallAgent 解决方案 |
|---|---|---|
| 知识分散 | 文档散落各处 | 统一知识库,向量检索 |
| 处理经验流失 | 靠个人经验 | RAG 知识库沉淀 |
| 告警响应慢 | 人工逐个排查 | 自动获取 Prometheus 告警 |
| 处理方案难找 | 搜索效率低 | 语义相似度匹配 |
| 复杂任务处理 | 手动分步执行 | Plan-Execute-Replan 自动编排 |
三、核心特性
1. 智能对话
ReAct Agent 驱动的多轮对话:
| 特性 | 说明 |
|---|---|
| 流式响应 | SSE 实时输出,体验流畅 |
| 工具自主调用 | Agent 自主选择工具 |
| 会话记忆 | 记住上下文,支持多轮对话 |
| 意图理解 | 精准理解用户意图 |
2. 知识库管理
| 功能 | 说明 |
|---|---|
| Markdown 自动解析 | 上传文档自动处理 |
| 向量化存储 | Embedding 模型转换 |
| 索引构建 | 自动建立向量索引 |
3. 告警分析
| 功能 | 说明 |
|---|---|
| 自动获取告警 | Prometheus 活跃告警 |
| 智能匹配 | 匹配内部处理方案 |
| 分析报告 | 生成处理建议 |
4. RAG 检索
| 特性 | 说明 |
|---|---|
| 语义相似度 | 基于向量匹配 |
| 精准检索 | 从知识库精准获取处理步骤 |
| 动态更新 | 知识库实时更新 |
5. 自主规划
Plan-Execute-Replan 架构:
| 阶段 | 说明 |
|---|---|
| Plan | 制定执行计划 |
| Execute | 按计划逐步执行 |
| Replan | 根据结果动态调整 |
四、技术架构
架构图
┌─────────────────────────────────────────────────────────────┐
│ OnCallAgent │
├─────────────────────────────────────────────────────────────┤
│ API Layer (Gin) │
│ ├── /upload - 文件上传 & 知识库索引 │
│ ├── /chat - 智能对话 │
│ ├── /chatStream - 流式对话 │
│ └── /plan - 运维计划分析 │
├─────────────────────────────────────────────────────────────┤
│ Agent Layer (CloudWeGo Eino) │
│ ├── ReAct Agent - 对话代理 (工具调用) │
│ └── Plan-Execute - 运维分析代理 (多步骤任务) │
├─────────────────────────────────────────────────────────────┤
│ Tools │
│ ├── Time Tool - 获取当前时间 │
│ ├── RAG Tool - 知识库检索 │
│ └── Prometheus Tool - 告警查询 │
├─────────────────────────────────────────────────────────────┤
│ Storage │
│ ├── Qdrant - 向量数据库 │
│ └── Ollama - Embedding 模型服务 │
└─────────────────────────────────────────────────────────────┘
技术栈
| 层级 | 技术 | 说明 |
|---|---|---|
| 框架 | Gin + CloudWeGo Eino | HTTP 框架 + Agent 框架 |
| 向量数据库 | Qdrant | 高性能向量检索 |
| Embedding | Ollama (nomic-embed-text) | 本地 Embedding 模型 |
| LLM | OpenAI 兼容 API | 任意大模型 |
| 监控 | Prometheus | 告警数据源 |
五、快速开始
前置依赖
| 依赖 | 版本 | 说明 |
|---|---|---|
| Go | 1.25+ | 运行环境 |
| Ollama | - | Embedding 服务 |
| Qdrant | - | 向量数据库 |
| OpenAI 兼容 API | - | LLM 服务 |
| Prometheus | 可选 | 告警分析 |
安装步骤
1. 克隆项目
git clone https://github.com/yinxiangpingfan/OnCallAgent
cd OnCallAgent
2. 启动依赖服务
# 启动 Ollama 并下载 Embedding 模型
ollama pull nomic-embed-text
# 启动 Qdrant
docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant
3. 配置文件
cp config/config_template.json config/config.json
编辑 config/config.json:
{
"server": {
"host": "localhost",
"port": 8819
},
"embedder": {
"host": "127.0.0.1",
"port": 11434,
"model": "nomic-embed-text",
"dimension": 384
},
"qdrant": {
"host": "127.0.0.1",
"port": 6334,
"collection": "oncallagent"
},
"openai": {
"api_key": "your-api-key",
"model": "your-model-name",
"api_base": "https://api.openai.com/v1"
},
"prometheus": {
"url": "http://localhost:9090"
}
}
4. 运行服务
go mod tidy
go run cmd/main.go
服务将在 http://localhost:8819 启动。
六、API 文档
健康检查
GET /ping
响应:
{"message": "pong"}
文件上传(知识库索引)
POST /upload
Content-Type: multipart/form-data
file: <markdown-file>
响应:
{"message": "上传成功"}
对话
POST /chat
Content-Type: application/json
{
"question": "如何处理服务下线告警?",
"id": "session-id"
}
响应:
{
"message": "根据知识库,服务下线可能因为服务 panic..."
}
流式对话
POST /chatStream
Content-Type: application/json
{
"question": "当前有哪些告警?",
"id": "session-id"
}
响应:Server-Sent Events (SSE) 流式数据
data: 根据 Prometheus 查询结果...
data: [DONE]
运维计划分析
GET /plan
自动获取 Prometheus 活跃告警,检索内部知识库,生成分析报告。
响应:
{
"message": "分析结果...",
"details": ["步骤1...", "步骤2..."]
}
七、项目结构
OnCallAgent/
├── cmd/
│ └── main.go # 程序入口
├── config/
│ ├── config.json # 配置文件
│ └── config_template.json # 配置模板
├── docs/ # 知识库文档目录
├── internal/
│ ├── handler/ # HTTP 处理器
│ │ ├── chat.go
│ │ ├── file.go
│ │ └── plan.go
│ ├── router/ # 路由配置
│ │ └── init.go
│ ├── repo/ # 数据访问层
│ │ └── qrdant/ # Qdrant 向量库
│ │ ├── init/
│ │ ├── indexer/
│ │ └── retriever/
│ └── server/ # 业务逻辑层
│ ├── ai/
│ │ ├── agent/ # AI Agent 实现
│ │ │ ├── chat/ # 对话 Agent
│ │ │ ├── knowledge_index/ # 知识库索引 Agent
│ │ │ └── plan_execute_replan/ # 运维 Agent
│ │ ├── embeder/ # Embedding 服务
│ │ ├── model/ # LLM 模型封装
│ │ └── tools/ # Agent 工具
│ │ ├── metrics_alerts.go # Prometheus 工具
│ │ ├── rag.go # RAG 检索工具
│ │ └── time.go # 时间工具
│ ├── chatServer/ # 对话服务
│ ├── knowledge_index/ # 知识库索引服务
│ └── plan/ # 运维计划服务
├── pkg/
│ ├── config/ # 配置解析
│ ├── log/ # 日志组件
│ └── tool/ # 工具函数
├── prometheus_config/ # Prometheus 配置
├── prometheusTestServer/ # 测试服务器
├── docker-compose.prometheus.yml
└── go.mod
八、核心组件
1. ReAct Agent(对话代理)
基于 ReAct (Reasoning + Acting) 模式的智能代理:
思考 (Reasoning) → 行动 (Acting) → 观察 (Observation) → 循环
能力:
- 自动选择合适的工具
- 多步推理和执行
- 记忆会话上下文
2. Plan-Execute-Replan Agent(运维代理)
多步骤任务执行框架:
| 阶段 | 说明 |
|---|---|
| Plan | 根据目标生成执行计划 |
| Execute | 按计划逐步执行 |
| Replan | 根据执行结果动态调整计划 |
3. RAG 工具
基于 Qdrant 向量数据库的检索工具:
- 文档向量化存储
- 语义相似度检索
- 知识库自动更新
4. Prometheus 工具
告警查询工具:
- 获取所有活跃告警
- 告警信息去重
- 持续时间计算
九、开发指南
添加新工具
在 internal/server/ai/tools/ 目录创建新工具:
package tools
import (
"context"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/components/tool/utils"
)
type MyToolInput struct {
Query string `json:"query" jsonschema:"description=查询参数"`
}
func NewMyTool() (tool.InvokableTool, error) {
return utils.InferTool("my_tool",
"工具描述",
func(ctx context.Context, input MyToolInput) (string, error) {
// 实现逻辑
return "result", nil
})
}
扩展知识库
将 Markdown 文档放入 docs/ 目录,通过 /upload 接口上传。
文档格式建议:
- 使用一级标题作为文档标题
- 标题会获得更高的检索权重
- 保持文档结构清晰
十、使用场景
场景1:智能问答
用户:如何处理 CPU 告警?
Agent:
1. RAG 检索知识库
2. 获取历史处理方案
3. 结合当前告警信息
4. 生成处理建议
场景2:告警自动分析
GET /plan
→ 获取 Prometheus 活跃告警
→ RAG 检索处理方案
→ Plan-Execute-Replan 生成分析报告
→ 返回处理步骤
场景3:值班辅助
值班人员:当前有哪些告警?
Agent:
1. 调用 Prometheus 工具获取告警
2. RAG 检索相关处理方案
3. 流式返回处理建议
十一、Docker 环境
启动 Prometheus 测试环境
docker-compose -f docker-compose.prometheus.yml up -d
十二、总结
OnCallAgent 填补了智能运维领域的空白:
核心价值
| 价值 | 说明 |
|---|---|
| 7×24 小时值班 | AI 永不疲劳 |
| 知识沉淀 | RAG 知识库积累处理经验 |
| 快速响应 | 秒级响应告警查询 |
| 自动化处理 | Plan-Execute-Replan 自动编排 |
技术亮点
| 亮点 | 说明 |
|---|---|
| 三大范式融合 | RAG + ReAct + Plan-Execute-Replan |
| CloudWeGo Eino | 现代化 Agent 框架 |
| 向量检索 | Qdrant 高性能向量数据库 |
| 本地部署 | Ollama 无需外部服务 |
本文首发于「程序员茄子」博客,原文链接:https://chenxutan.com