AI测试框架:怎么保证LLM应用质量
🩺 摘要
写普通程序可以写单元测试、集成测试。但AI应用的输出是自然语言,同一个prompt每次可能返回不同的结果。怎么测?
📝 详情
AI测试为什么难
普通代码:输入1+1,永远输出2。可以写断言。 AI应用:输入「写一首诗」,每次可能不同。不能写断言。
所以AI测试的核心不是「对不对」,而是「好不好」。
测试的三个层次
1. 功能测试(最基本)
测试AI能不能正常响应:
def test_ai_responds():
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "你好"}]
)
assert response.choices[0].message.content is not None
assert len(response.choices[0].message.content) > 0
2. 质量测试
用另一个AI来评价AI的输出:
def test_quality():
prompt = "解释一下什么是RAG"
response = call_llm(prompt)
eval_prompt = f"""评价以下回答是否准确、完整、易懂。
问题:{prompt}
回答:{response}
评分1-10分。
"""
score = call_llm(eval_prompt)
assert int(score) >= 7
这种方法叫LLM as a Judge。DeepEval和RAGAS都是干这个的。
3. 回归测试
维护一组标准问题和期望回答,每次修改Prompt后跑一遍:
[
{"question": "什么是RAG", "expected_keywords": ["检索", "生成", "知识库"]},
{"question": "Ollama怎么装", "expected_keywords": ["ollama.com", "curl", "docker"]}
]
每次修改Prompt后跑回归,确保没变差。
推荐工具
| 工具 | 用途 |
|---|---|
| DeepEval | AI应用质量评估框架 |
| RAGAS | RAG系统评估 |
| LangFuse | 生产环境监控 |
| PromptFoo | Prompt测试和红队测试 |
一句话
AI应用也要测试。不是测对不对,是测好不好。
💬 评论 (0)