Danush-Aries/llm-fragility-lab
GitHub: Danush-Aries/llm-fragility-lab
纯标准库实现的 LLM 幻觉检测工具,通过词法信号加权计算漂移分数来量化模型输出的事实偏差。
Stars: 0 | Forks: 0
# LLM Fragility Lab — 衡量你的模型有多少在凭空捏造
` 用于批量处理,`--json` 提供适合管道传输的输出。后端包可直接导入:`from backend.app.engines.hallucination_hunter import HallucinationHunter`。
## 截图
| CLI 演示 | 批量摘要 | 自定义权重 |
|---|---|---|
|  |  |  |
## 信号
| 信号 | 描述 |
|---|---|
| Token F1 | token 级别精确率和召回率的调和平均数 |
| Jaccard 相似度 | 响应与真实答案之间的一元组集合重叠率 |
| 二元组重叠率 | 连续双词对的覆盖率(类似于 ROUGE-2) |
| 新词比例 | 响应词汇中缺失于真实答案词表的比例 |
| 长度比 | 对响应长度远短于或远长于预期的情况进行惩罚 |
接近 **0** 的 `drift_score` 意味着响应与真实答案高度匹配。接近 **1** 的分数表示高差异(divergence),极有可能是幻觉。
## Python API
```
from backend.app.engines.hallucination_hunter import HallucinationHunter
hunter = HallucinationHunter()
# 单个分析
result = hunter.analyze(
response="The Eiffel Tower was built in 1750 by Napoleon.",
ground_truth="The Eiffel Tower was completed in 1889 by Gustave Eiffel.",
)
# 批量
pairs = [
("Paris is in France.", "Paris is the capital of France."),
("Water boils at 50 C.", "Water boils at 100 degrees Celsius."),
]
results = hunter.batch_analyze(pairs)
summary = hunter.summary(results)
# {'n': 2, 'mean_drift_score': ..., 'risk_counts': {'low': 1, 'medium': 1, 'high': 0}}
```
### 自定义权重
```
hunter = HallucinationHunter(
weight_f1=0.40,
weight_jaccard=0.30,
weight_bigram=0.15,
weight_novel=0.10,
weight_length=0.05, # weights must sum to 1.0
)
```
## 运行测试
```
python3 -m pytest tests/ -v
```
## 项目结构
```
llm-fragility-lab/
├── backend/
│ └── app/
│ └── engines/
│ ├── __init__.py
│ └── hallucination_hunter.py # core scoring engine
├── tests/
│ └── test_hallucination_hunter.py # 29 unit tests
├── main.py # CLI entry point
├── samples.json # example batch input
├── requirements.txt
└── README.md
```
## 技术栈
仅使用 Python 3.9+ 标准库(`re`、`math`、`collections`)· 使用 `pytest` 运行包含 29 个测试的套件 · 如果你需要扩展向量指标,可选安装 `numpy>=1.24`。
## 许可证
MIT — 查看 [许可证](LICENSE)。
### 来自 Danush 的更多内容
- [ponytail-for-python](https://github.com/Danush-Aries/ponytail-for-python) — 针对 Python 代码库的代码智能工具
- [Agentic_Systems](https://github.com/Danush-Aries/Agentic_Systems) — Agent 模式的参考实现
- [autonomous-coding-agent](https://github.com/Danush-Aries/autonomous-coding-agent) — 全自动工程 Agent
- [computer-use-agent](https://github.com/Danush-Aries/computer-use-agent) — Claude 通过 VNC 操控你的桌面
- [browser-automation-agent](https://github.com/Danush-Aries/browser-automation-agent) — Claude 操控 Playwright
- [blinkchat](https://github.com/Danush-Aries/blinkchat) — 实时聊天,带感

标签:AI测试, Clair, DLL 劫持, Python, 大模型评估, 大语言模型, 安全规则引擎, 对抗测试, 无后门, 质量保证, 逆向工具