上一篇文章中我們討論了 RAG 至 Agentic RAG 系統中,延遲與準確度監控的重要性。
本篇我們將實際介紹兩個最常見的開源監控組合 —— Prometheus 與 Grafana,以及針對LLM的LangSmith。
它們能幫助我們收集、儲存與可視化 RAG 系統的效能指標。
Prometheus 是一個開源的監控系統與時間序列資料庫(TSDB),由 SoundCloud 開發,現為 CNCF 畢業專案。專門用來:
主動拉取(Pull-based scrape) 各服務暴露的指標(metrics)
儲存 這些數據為時間序列資料(time series)
提供查詢語言(PromQL) 來分析與聚合資料
內建告警機制(Alertmanager)
在 RAG 系統中,我們可以讓 API 或 Agent 暴露 /metrics
端點(通常使用 Prometheus client library),
Prometheus 就會定期抓取其中的數據,如:
# 指標命名範例(遵循 Prometheus 命名規範)
embedding_generation_duration_seconds # Histogram:向量生成時間分佈
vector_search_duration_seconds # Histogram:向量檢索時間分佈
llm_generation_duration_seconds # Histogram:LLM 生成時間分佈
rag_request_duration_seconds # Histogram:端到端延遲
rag_requests_total # Counter:總請求數
rag_requests_failed_total # Counter:失敗請求數
agent_tool_calls_total # Counter:tool 調用次數(按 tool_name 標籤)
agent_tool_success_total # Counter:tool 成功次數
context_tokens_total # Counter:使用的 context tokens
generation_tokens_total # Counter:生成的 tokens
<namespace>_<subsystem>_<name>_<unit>
範例:
rag_embedding_generation_duration_seconds
│ │ │ │ │
│ │ │ │ └─ 單位(必須)
│ │ │ └─────────── 描述
│ │ └──────────────────────── 子系統
│ └────────────────────────────────── 命名空間
Grafana 是一個開源的可觀測性平台,專注於數據可視化與分析。主要特點:
多數據源支援:
豐富的視覺化元件:
進階功能:
透過 Grafana,我們能輕鬆建立專業的監控儀表板,下面給幾個建議:
效能監控面板:
rate(rag_requests_total[5m])
histogram_quantile(0.95, rate(rag_request_duration_seconds_bucket[5m]))
rate(rag_requests_failed_total[5m]) / rate(rag_requests_total[5m])
成本監控面板:
LangSmith 是由 LangChain 官方推出的 LLM 應用觀測與除錯平台。
它專為開發者設計,能夠記錄並可視化整個大型語言模型(LLM)應用的執行流程。
在 RAG 或 Agentic 系統中,LangSmith 可用於:
LangSmith 的主要組件包含:
其運作方式如下:
[User Query] → [Retriever] → [LLM/Chain] → [Response]
↘ Trace Data → LangSmith Dashboard
開發者能在 Dashboard 中觀察:
今天主要先認識一下有那些工具可以用來幫助我們監控系統,並整理這幾個套件如果要應用在Agentic RAG要怎麼做,以及各自的分工會是什麼。
筆者也還不熟悉這段,接下來會和大家一起實作研究。