Danush-Aries/llm-fragility-lab

GitHub: Danush-Aries/llm-fragility-lab

纯标准库实现的 LLM 幻觉检测工具,通过词法信号加权计算漂移分数来量化模型输出的事实偏差。

Stars: 0 | Forks: 0

# LLM Fragility Lab — 衡量你的模型有多少在凭空捏造

LLM Fragility Lab drift score demo

build license python tests deps

## 为什么会有这个项目 你想在幻觉(hallucination)发生的那一刻就捕捉到它,但市面上每一个现成的“LLM eval”包为了对某些 embedding 计算余弦相似度,都要引入 `transformers`、`torch` 以及 4 GB 的权重文件。LLM Fragility Lab 却反其道而行之:仅使用纯标准库,提供五个轻量级的词法信号(token F1、Jaccard、二元组重叠率(bigram overlap)、新词比例(novel-token ratio)、长度惩罚)、一个混合计算的 `drift_score`,以及一个 `low / medium / high`(低/中/高)的分类风险标签。它可以在树莓派上运行。能轻松融入你的 CI 检查中,仅给你的测试套件增加 200 毫秒的开销。 ## 60 秒上手体验 ``` git clone https://github.com/Danush-Aries/llm-fragility-lab.git cd llm-fragility-lab pip install -r requirements.txt # only pytest — no runtime deps python3 main.py # built-in demo python3 main.py --batch samples.json # score a batch python3 main.py \ --response "The Eiffel Tower was built in 1750 by Napoleon." \ --truth "The Eiffel Tower was completed in 1889 by Gustave Eiffel." # → drift_score: 0.71, risk: high ``` ## 工作原理 - **`HallucinationHunter.analyze()`** 会对两个字符串进行分词(tokenise),然后并行计算五个信号:token F1(精确率/召回率的调和平均数)、Jaccard(一元组集合重叠率)、二元组重叠率(类似 ROUGE-2)、新词比例(真实答案中缺失的响应词)以及长度比(对过短或过长的惩罚)。 - **加权混合** — 默认权重 `[0.35, 0.25, 0.20, 0.15, 0.05]` 总和为 1.0;可通过构造函数的 kwargs 覆盖,以便针对不同领域(事实性问答 vs 摘要 vs 代码生成)调整敏感度。 - **风险区间** — `drift < 0.25 → low`,`< 0.5 → medium`,否则为 `high`。可通过子类化进行调优。 - **批量处理与摘要** — `batch_analyze(pairs)` 会在一个列表上运行整个流水线(pipeline);`summary(results)` 为你提供漂移的平均值/最小值/最大值,以及一个可用于绘制的 `risk_counts` 直方图。 - **CLI** — `--response`/`--truth` 用于单次运行,`--batch ` 用于批量处理,`--json` 提供适合管道传输的输出。后端包可直接导入:`from backend.app.engines.hallucination_hunter import HallucinationHunter`。 ## 截图 | CLI 演示 | 批量摘要 | 自定义权重 | |---|---|---| | ![](https://raw.githubusercontent.com/Danush-Aries/llm-fragility-lab/main/assets/screenshot-1.png) | ![](https://raw.githubusercontent.com/Danush-Aries/llm-fragility-lab/main/assets/screenshot-2.png) | ![](https://raw.githubusercontent.com/Danush-Aries/llm-fragility-lab/main/assets/screenshot-3.png) | ## 信号 | 信号 | 描述 | |---|---| | 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, 大模型评估, 大语言模型, 安全规则引擎, 对抗测试, 无后门, 质量保证, 逆向工具