SKILL.md
readonlyread-only
name
agentic-eval
description
Patterns and techniques for evaluating and improving AI agent outputs. Use this skill when: - Implementing self-critique and reflection loops - Building evaluator-optimizer pipelines for quality-critical generation - Creating test-driven code refinement workflows - Designing rubric-based or LLM-as-judge evaluation systems - Adding iterative improvement to agent outputs (code, reports, analysis) - Measuring and improving agent response quality
代理評估模式
透過迭代評估與改善實現自我提升的模式。
概述
評估模式讓代理能夠評估並改善自己的輸出,從單次生成進化為迭代精煉循環。
生成 → 評估 → 批評 → 精煉 → 輸出
↑ │
└──────────────────────────────┘
使用時機
- 品質關鍵的生成:需要高準確度的程式碼、報告、分析
- 有明確評估標準的任務:存在定義好的成功指標
- 需要特定標準的內容:風格指南、合規性、格式要求
模式 1:基本反思
代理透過自我批評評估並改善自己的輸出。
def reflect_and_refine(task: str, criteria: list[str], max_iterations: int = 3) -> str:
"""使用反思循環生成。"""
output = llm(f"完成此任務:\n{task}")
for i in range(max_iterations):
# 自我批評
critique = llm(f"""
根據標準評估此輸出:{criteria}
輸出:{output}
每項評分:PASS/FAIL 並附上回饋,以 JSON 格式。
""")
critique_data = json.loads(critique)
all_pass = all(c["status"] == "PASS" for c in critique_data.values())
if all_pass:
return output
# 根據批評精煉
failed = {k: v["feedback"] for k, v in critique_data.items() if v["status"] == "FAIL"}
output = llm(f"改善以解決:{failed}\n原始:{output}")
return output
關鍵洞察:使用結構化的 JSON 輸出,以便可靠地解析批評結果。
模式 2:評估器-最佳化器
將生成與評估分離為不同元件,職責更清晰。
class EvaluatorOptimizer:
def __init__(self, score_threshold: float = 0.8):
self.score_threshold = score_threshold
def generate(self, task: str) -> str:
return llm(f"完成:{task}")
def evaluate(self, output: str, task: str) -> dict:
return json.loads(llm(f"""
評估輸出是否符合任務:{task}
輸出:{output}
回傳 JSON:{{"overall_score": 0-1, "dimensions": {{"accuracy": ..., "clarity": ...}}}}
"""))
def optimize(self, output: str, feedback: dict) -> str:
return llm(f"根據回饋改善:{feedback}\n輸出:{output}")
def run(self, task: str, max_iterations: int = 3) -> str:
output = self.generate(task)
for _ in range(max_iterations):
evaluation = self.evaluate(output, task)
if evaluation["overall_score"] >= self.score_threshold:
break
output = self.optimize(output, evaluation)
return output
模式 3:程式碼專用反思
測試驅動的程式碼生成精煉循環。
class CodeReflector:
def reflect_and_fix(self, spec: str, max_iterations: int = 3) -> str:
code = llm(f"為以下規格撰寫 Python 程式碼:{spec}")
tests = llm(f"為以下規格生成 pytest 測試:{spec}\n程式碼:{code}")
for _ in range(max_iterations):
result = run_tests(code, tests)
if result["success"]:
return code
code = llm(f"修正錯誤:{result['error']}\n程式碼:{code}")
return code
評估策略
基於結果
評估輸出是否達到預期結果。
def evaluate_outcome(task: str, output: str, expected: str) -> str:
return llm(f"輸出是否達到預期結果?任務:{task},預期:{expected},輸出:{output}")
LLM 作為評審
使用 LLM 比較並排序輸出。
def llm_judge(output_a: str, output_b: str, criteria: str) -> str:
return llm(f"比較輸出 A 和 B 在 {criteria} 上的表現。哪個較好?為什麼?")
基於評分標準
根據加權維度對輸出評分。
RUBRIC = {
"accuracy": {"weight": 0.4},
"clarity": {"weight": 0.3},
"completeness": {"weight": 0.3}
}
def evaluate_with_rubric(output: str, rubric: dict) -> float:
scores = json.loads(llm(f"對每個維度評分 1-5:{list(rubric.keys())}\n輸出:{output}"))
return sum(scores[d] * rubric[d]["weight"] for d in rubric) / 5
最佳實務
| 實務 | 理由 |
|---|---|
| 明確的標準 | 事先定義具體、可衡量的評估標準 |
| 迭代次數限制 | 設定最大迭代次數(3-5 次)以防止無限迴圈 |
| 收斂檢查 | 若輸出分數在迭代間未改善則停止 |
| 記錄歷史 | 保留完整軌跡以便除錯與分析 |
| 結構化輸出 | 使用 JSON 以可靠解析評估結果 |
快速入門檢查清單
## 評估實作檢查清單
### 設定
- [ ] 定義評估標準/評分標準
- [ ] 設定「夠好」的分數門檻
- [ ] 設定最大迭代次數(預設:3)
### 實作
- [ ] 實作 generate() 函式
- [ ] 實作 evaluate() 函式,輸出結構化結果
- [ ] 實作 optimize() 函式
- [ ] 串接精煉循環
### 安全
- [ ] 加入收斂偵測
- [ ] 記錄所有迭代以便除錯
- [ ] 優雅處理評估解析失敗






