自动重启AI代理(模块化)- 最终版本包
这个项目实现了一个模块化的自动重启代理,它:
- Tails 文本和 JSON 日志
- 通过LangChain使用本地Ollama模型(phi3)分析日志
- 生成MCP风格的操作(重启systemd服务、Docker容器、Kubernetes部署)
- 验证操作是否符合运行手册(使用FAISS进行快速近似搜索)并防止状态频繁切换
- 支持干运行模式,以进行安全测试
警告: 执行器运行实际命令(systemctl、docker、kubectl)。保持 DRY_RUN=True 用于测试。
布局
config.py- 配置(日志路径、模型、模拟运行、阈值)main.py- 调度器(或编曲者,根据上下文可具体翻译)tailer.py- 尾部文本和JSON日志ai_agent.py- LangChain/Ollama 代理封装器(模型 phi3)mcp_actions.py- 处理MCP(管理控制平面)操作并调用执行器executor.py- 执行命令(服务/容器/Pod 重启)runbook_rag.py- RAG(检索增强生成)索引构建器和查询(使用sentence-transformers + faiss)runbook.py- 运行手册验证器(如有可用,使用RAG)memory_store.py- 扰流板保护utils.py- 助手auto-restart-ai.service- 为用户配置的 systemd 单元aiagent和路径/opt/autorestart_airequirements.txt- Python 依赖项
快速安装(推荐)
- 将项目复制到
/opt/autorestart_ai:
sudo mkdir -p /opt/autorestart_ai
sudo chown $(whoami):$(whoami) /opt/autorestart_ai
unzip auto_restart_ai_final.zip -d /opt/autorestart_ai- 创建一个专用用户(如果不存在)并调整所有权:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin aiagent
sudo chown -R aiagent:aiagent /opt/autorestart_ai- 设置虚拟环境并安装依赖项:
cd /opt/autorestart_ai
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt- (可选)构建运行手册索引:
source venv/bin/activate
python -c "from runbook_rag import build_runbook_index; build_runbook_index()"- 复制 systemd 单元并启用:
sudo cp auto-restart-ai.service /etc/systemd/system/auto-restart-ai.service
sudo systemctl daemon-reload
sudo systemctl enable auto-restart-ai.service
sudo systemctl start auto-restart-ai.service- 监控日志:
sudo journalctl -u auto-restart-ai.service -fOllama 设置
运行代理所使用的本地模型需要Ollama。
- 安装Ollama:
- macOS: brew install ollama - Linux: curl -fsSL https://ollama.com/install.sh | sh - Windows: winget install Ollama.Ollama
- 启动服务器:
ollama serve(默认在http://localhost:11434) - 拉动模型:
ollama pull phi3 - 快速测试:
ollama run phi3 -p "hello" - 在(某处)进行配置
config.py:
- 设置 OLLAMA_BASE_URL = "http://localhost:11434" - 设定;套装;一套 OLLAMA_MODEL = "phi3"
笔记
- 与……进行彻底测试
DRY_RUN=True。 - 配置
config.py以匹配您的环境(日志路径、模型、Ollama URL)。 - RAG组件需要在本地构建FAISS索引。
