Ferganvj/llm-triager
GitHub: Ferganvj/llm-triager
结合本地 LLM 编码助手与实时日志异常检测 pipeline 的双用途工程工具包。
Stars: 0 | Forks: 0
# AI 助手
一个双用途的工程工具包,结合了本地托管的 LLM 编码助手与实时日志处理及异常检测 pipeline。
## 组件
### 1. Coder 助手 (Ollama LLM)
一个通过 [Ollama](https://ollama.com) 提供的 `coder-assistant` 模型,基于 `qwen2.5-coder:14b` 构建,温度参数为 0.2。该模型配置了严格的工程师角色设定——它作为资深软件工程师和系统架构师运行,而不是对话式聊天机器人。
**行为:**
- 在回答之前先用技术术语重述问题
- 输出结构化的响应:`PLAN` → `PATCH` → `RISK CHECK`
- 生成有针对性的 BEFORE/AFTER 补丁,而不是全文件重写
- 执行重构规则:移除重复代码、移除 Colab artifacts、改进模块化
- 将架构决策和权衡记录在 `memory/project_memory.md` 中
### 2. 日志处理 Pipeline
一个多线程 Python pipeline,实时生成、解析和分析日志消息,并在 `8000` 端口暴露 Prometheus 指标。
**数据流:**
```
log_producer (thread) → queue.Queue → log_processor (thread) → anomaly_detector
│
anomaly? ────────────────┤
│ ↓
↓ Prometheus Counters (:8000)
triage_queue → triage_worker (thread)
→ Ollama LLM (local)
→ {severity, summary,
root_cause, remediation}
```
**异常检测:** 基于关键词——任何包含 `ERROR` 或 `WARNING` 的日志都会增加 `anomalies_detected_total` 的计数。
**LLM 分类(可选):** 当触发异常时,最近日志行的滚动窗口会——离开 hot path——发送到本地 Ollama 模型,该模型会返回结构化的评估(严重性、摘要、根本原因、修复建议)。本地优先且支持优雅降级:如果 Ollama 宕机,pipeline 会继续运行。默认禁用;通过 `config/runtime_config.yaml` 中的 `triage:` 块启用。
## 技术栈
| 层级 | 技术 |
|---|---|
| LLM runtime | [Ollama](https://ollama.com) |
| Base model | `qwen2.5-coder:14b` |
| 语言 | Python 3 |
| 并发 | `threading`, `queue.Queue` |
| 可观测性 | `prometheus_client` (Counter 指标) |
| 配置 | YAML (`config/runtime_config.yaml`) |
| 依赖项 | `requests`, `pyyaml`, `prometheus_client` |
## 项目结构
```
.
├── Modelfile # Ollama model definition
├── config/
│ └── runtime_config.yaml # Model name, prompt path, memory + triage config
├── prompts/
│ └── system_prompt.txt # Assistant persona and output format rules
├── memory/
│ ├── project_memory.md # Persistent memory (auto-append currently disabled)
│ └── architecture_state.md # System state template
├── test_model.py # Log pipeline (producer + processor + Prometheus)
├── triage.py # LLM triage layer for detected anomalies
├── tests/
│ ├── test_pipeline.py # Unit tests for the pipeline
│ └── test_triage.py # Unit tests for triage (fully offline)
└── requirements.txt
```
## 设置
```
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install prometheus_client
```
## 运行
**日志处理 pipeline**(Prometheus 指标位于 `:8000`):
```
python test_model.py
```
**Coder 助手**(需要安装 Ollama):
```
ollama create coder-assistant -f Modelfile
ollama run coder-assistant
```
**使用 LLM 分类** —— 启动 Ollama 和模型,然后在 `config/runtime_config.yaml` 中
设置 `triage.enabled: true` 并运行 pipeline。检测到的异常会被发送
到本地模型进行严重性/根本原因/修复建议分析。如果关闭分类
(默认),则不会进行任何 LLM 调用。
**测试:**
```
python -m unittest discover -s tests
```
## Prometheus 指标
| 指标 | 类型 | 描述 |
|---|---|---|
| `logs_processed_total` | Counter | 处理的日志消息总数 |
| `anomalies_detected_total` | Counter | 检测到的 ERROR/WARNING 异常总数 |
| `anomaly_triage_total{severity}` | Counter | 由 LLM 分类的异常(按严重性划分) |
指标暴露在 `http://localhost:8000`。
## 备注
- 当在 `config/runtime_config.yaml` 中启用 `memory.auto_update` 时,助手的记忆层 (`memory/project_memory.md`) 会在不同会话间进行追加。目前处于**禁用**状态:该追加器没有去重或大小上限,此前曾导致文件增长至约 1,400 行的重复块。只有在解决此问题后才应重新启用。
标签:AI风险缓解, DLL 劫持, LLM评估, Ollama, Python, 大语言模型, 开发辅助工具, 异常检测, 无后门, 自定义请求头