with one click
llm-evaluation
LLM 系统评估框架——构建评估数据集、检索/答案质量指标、LLM-as-Judge、CI 集成自动评估
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
LLM 系统评估框架——构建评估数据集、检索/答案质量指标、LLM-as-Judge、CI 集成自动评估
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
Execute the check workflow from this AI development team preset. Use when the user writes /check, asks for check, or wants the corresponding team process in Codex.
Execute the dev workflow from this AI development team preset. Use when the user writes /dev, asks for dev, or wants the corresponding team process in Codex.
Execute the fix workflow from this AI development team preset. Use when the user writes /fix, asks for fix, or wants the corresponding team process in Codex.
Execute the plan workflow from this AI development team preset. Use when the user writes /plan, asks for plan, or wants the corresponding team process in Codex.
Execute the project-preset workflow from this AI development team preset. Use when the user writes /project-preset, asks for project-preset, or wants the corresponding team process in Codex.
Execute the review-all workflow from this AI development team preset. Use when the user writes /review-all, asks for review-all, or wants the corresponding team process in Codex.
| name | llm-evaluation |
| description | LLM 系统评估框架——构建评估数据集、检索/答案质量指标、LLM-as-Judge、CI 集成自动评估 |
评估 RAG 系统的检索质量和答案质量。
| 维度 | 含义 | 评估方式 |
|---|---|---|
| Retrieval Precision | 检索到的文档有多少是相关的 | 人工标注 or LLM 打分 |
| Retrieval Recall | 相关文档有多少被检索到 | 需要 ground truth |
| Answer Faithfulness | 答案是否完全基于检索内容 | LLM 打分 |
| Answer Relevance | 答案是否回答了问题 | LLM 打分 |
| Latency P95 | 端到端响应时间 | 性能测试 |
# evaluation/dataset.py
from dataclasses import dataclass
@dataclass
class EvalCase:
question: str
expected_answer: str # 参考答案
relevant_doc_ids: list[str] # 应该检索到的文档 ID
# 构建方式(二选一):
# 1. 人工编写(最准确,但费时)
# 2. 用 LLM 从文档自动生成问题(快速,但有偏差)
async def generate_eval_dataset(chunks: list[Chunk], n: int = 50) -> list[EvalCase]:
cases = []
for chunk in random.sample(chunks, n):
prompt = f"基于以下内容生成一个问题和答案:\n\n{chunk.content}"
result = await llm.generate(prompt)
# parse result into EvalCase
cases.append(parse_qa(result, chunk))
return cases
# evaluation/runner.py
import time
from statistics import mean
class RAGEvaluator:
async def evaluate(self, cases: list[EvalCase]) -> EvalReport:
results = []
latencies = []
for case in cases:
start = time.perf_counter()
retrieved = await retrieve(case.question)
answer = await generate_answer(case.question, retrieved)
latency = time.perf_counter() - start
result = EvalResult(
question=case.question,
answer=answer,
retrieved_ids=[c.id for c in retrieved],
latency=latency,
precision=self._calc_precision(retrieved, case.relevant_doc_ids),
faithfulness=await self._eval_faithfulness(answer, retrieved),
relevance=await self._eval_relevance(case.question, answer),
)
results.append(result)
latencies.append(latency)
return EvalReport(
precision=mean(r.precision for r in results),
faithfulness=mean(r.faithfulness for r in results),
relevance=mean(r.relevance for r in results),
latency_p95=sorted(latencies)[int(len(latencies) * 0.95)],
total_cases=len(cases),
)
async def _eval_faithfulness(self, answer: str, chunks: list[Chunk]) -> float:
context = "\n".join(c.content for c in chunks)
prompt = f"""判断以下答案是否完全基于给定的上下文(不包含上下文之外的信息)。
上下文:{context}
答案:{answer}
输出 0.0-1.0 的分数(1.0=完全基于上下文):"""
score_str = await llm.generate(prompt, max_tokens=10)
return float(score_str.strip())
# .github/workflows/eval.yml
- name: Run RAG evaluation
run: python -m evaluation.runner --threshold-precision 0.8 --threshold-faithfulness 0.9
env:
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
EVAL_THRESHOLDS = {
"precision": 0.80, # 检索精度
"faithfulness": 0.90, # 忠实度(关键,防幻觉)
"relevance": 0.85, # 相关性
"latency_p95": 3.0, # 秒
}