一个用于检测检索增强生成(RAG)系统中错误信息与知识投毒的评估代理。
本项目实现了一个 Trustworthy RAG 框架,其中集成有评估代理(Evaluation Agent),通过三个互补的分析组件评估 RAG 响应的可靠性:
系统为每条 RAG 响应生成信任评分(Trust Score)(0-1),从而能够自动检测知识投毒攻击与幻觉内容。
本仓库是 ICSEA 2026 会议同名论文的研究工件。LaTeX 源码请参见 [Conference_ICSEA2026/](https://github.com/gpt-laboratory/trustworthyrag/blob/main/Conference_ICSEA2026),以及下方的论文一节。
User Query
|
v
+-------------------+
| RETRIEVER | Embedding (MiniLM-L6-v2 or Snowflake Arctic Embed2) + FAISS
+--------+----------+
|
v
+-------------------+
| GENERATOR | Llama 3.3 70B / Qwen 3.5 35B / Mistral 7B Instruct via FARMI API
+--------+----------+
|
v
+--------------------------------------------+
| EVALUATION AGENT |
| |
| NLI Verifier Poison Detector |
| (factuality) (5 detection methods) |
| | | |
| v v |
| Trust Index Calculator |
| T = 0.4*F + 0.35*C + 0.25*(1-P) |
+--------------------------------------------+
|
v
Answer + Trust Score + Evaluation Report
src/
retriever/ # Document retrieval (embeddings, FAISS, chunking)
generator/ # LLM response generation (FARMI client, prompts)
evaluation_agent/ # Core evaluation (NLI, poison detection, trust index)
experiments/ # Experiment framework (poisoned datasets, runner)
pipeline/ # End-to-end RAG pipeline orchestrator
tests/ # Unit tests (78 tests, pytest)
configs/config.yaml # All configuration parameters
figures/ # Auto-generated charts (7 figures, 300 DPI)
run_experiment.py # Experiment CLI (main entry point)
generate_charts.py # Visualization generator
requirements.txt # Python dependencies
# Create and activate virtual environment
python -m venv venv
.\venv\Scripts\Activate.ps1 # Windows PowerShell
# source venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
# Configure FARMI API access (required for LLM generation)
# Copy the template and fill in your own credentials:
# cp .env.example .env # then edit .env and set FARMI_API_KEY
# The .env file is .gitignored - never commit it or hardcode keys in source.
python run_experiment.py --all
这将运行所有实验并自动生成图表:
python run_experiment.py --quick # Quick test (10 samples)
python run_experiment.py --samples 30 # Custom sample count
python run_experiment.py --per-strategy # Per-strategy breakdown only
python run_experiment.py --fever # Include FEVER dataset
python run_experiment.py --ablation # Ablation study only
python run_experiment.py --grid # 2x2 factorial grid (all LLM x embedding combos)
python run_experiment.py --grid --samples 50 # Full grid run (100 samples per config)
交互式提示允许你选择:
你也可以通过 LLM_MODEL 环境变量以非交互方式固定生成器
(与 configs/config.yaml 及 run_experiment.py 中的 MODEL_DESCRIPTIONS 一致):
$env:LLM_MODEL = "mistral-7b-instruct" # or "qwen3.5:35b", "llama3.3:70b"
python run_experiment.py --all
在项目根目录重现安全编码助手实验(40 条 OWASP/CWE 规则)。
该实验复用现有的 ExperimentRunner 和 PoisonedDatasetGenerator,并将结果写入
data/experiments/seccode_*.json:
$env:LLM_MODEL = "llama3.3:70b"; $env:HF_HUB_OFFLINE = "1"; $env:TRANSFORMERS_OFFLINE = "1"
python Conference_ICSEA2026/run_seccode_usecase.py
# Set $env:SECCODE_N = "4" first for a quick smoke run over a few rules.
python generate_charts.py
pytest # All tests (includes slow model-loading tests)
pytest -m "not slow" # Fast tests only (~0.4s)
pytest --cov=src # With coverage report
朴素的全信任基线:85%(TruthfulQA)、85%(FEVER)。FEVER 的表现不及基线——信任指数需要对简短的事实性陈述进行特定领域的校准。
在 95% 置信区间下(比例采用 Wilson 区间,F1 采用百分位自助法,B=20,000),TruthfulQA 混合投毒的主要结果为:准确率 91%(CI 83.8–95.2)、召回率 40%(CI 19.8–64.3)、F1 57.1%(CI 25.0–80.0),Δ=0.225。由于每次运行只有 15 个投毒样本,区间较宽,因此我们将其报告出来,以免夸大结果的精确性。
主要发现:
该代理在软件工程领域的一个应用:一个安全编码助手,从**包含 40 条精选规则(源自 OWASP Top 10 和 CWE)**的知识库中检索相关信息(例如针对 SQL 注入的参数化查询、自适应密码哈希、TLS 配置)。开发者查询会检索出相应指导,评估代理会在 LLM 给出推荐之前对这些指导进行筛查。我们用五种策略对 30% 的规则注入了安全攻击载荷(例如伪造的 CORRECTION: 指令、被替换的 CWE 标识符)。
按策略的检测结果(Llama 3.3 70B + all-MiniLM-L6-v2,K=5):
在 τ=0.5 工作点之外,信任指数在三个 LLM 上展现出很强的阈值无关信号(混合策略运行的合并结果):
Trust = alpha * Factuality + beta * Consistency + gamma * (1 - PoisonProbability)
Default weights: alpha=0.4, beta=0.35, gamma=0.25
当投毒概率超过 0.7 时,会应用非线性阻尼器:
delta = 1 - 0.4 * (P - 0.70) / 0.30 —— 当 P=1.0 时,信任度降低 40%。
| 信任等级 | 分数范围 | 含义 |
|---|
[Conference_ICSEA2026/](https://github.com/gpt-laboratory/trustworthyrag/blob/main/Conference_ICSEA2026)。本发布版有意不包含编译好的 PDF;请从 LaTeX 源码自行构建。
ICSEA 2026 会议论文作者 —— 坦佩雷大学(TUNI)。
Balkrishna Giri,理学硕士研究员,坦佩雷大学信息技术与通信科学学院
邮箱:[email protected]、[email protected]
Md Toufique Hasan,博士研究员,GPT 实验室,坦佩雷大学信息技术与通信科学学院
邮箱:[email protected]
共同作者 —— 坦佩雷大学信息技术与通信科学学院:
开发于坦佩雷大学 GPT 实验室。
| 数据集 | 准确率 | 精确率 | 召回率 | F1 分数 | 对比基线 |
|---|
| TruthfulQA(混合) | 91% | 100% | 40% | 57.1% | +7% |
| FEVER(完整运行) | 73% | 20% | 26.7% | 22.9% | −12% |
| 策略 | 准确率 | 精确率 | 召回率 | F1 | 分离度 |
|---|
| 注入 | 99% | 93.8% | 100% | 96.8% | 0.498 |
| 矛盾 | 92% | 88.9% | 53.3% | 66.7% | 0.311 |
| 隐蔽 | 88% | 100% | 20.0% | 33.3% | 0.149 |
| 实体替换 | 85% | — | 0% | 0% | 0.053 |
| LLM | 嵌入模型 | 准确率 | 精确率 | 召回率 | F1 | 干净信任分 | 分离度 |
|---|
| Llama 3.3 70B | all-MiniLM-L6-v2 | 91% | 100% | 40% | 57.1% | 0.830 | 0.240 |
| Llama 3.3 70B | snowflake-arctic-embed2 | 91% | 100% | 40% | 57.1% | 0.799 | 0.188 |
| Qwen 3.5 35B | all-MiniLM-L6-v2 | 71% | 25.0% | 46.7% | 32.6% | 0.633 | 0.161 |
| Qwen 3.5 35B | snowflake-arctic-embed2 | 71% | 28.1% | 60.0% | 38.3% | 0.653 | 0.199 |
| 策略 | 准确率 | 精确率 | 召回率 | F1 | Δ |
|---|
| 指令注入 | 97.5% | 85.7% | 100.0% | 92.3% | 0.542 |
| 矛盾 | 85.0% | — | 0.0% | 0.0% | 0.156 |
| 隐蔽操纵 | 85.0% | — | 0.0% | 0.0% | 0.066 |
| 实体替换 | 72.5% | 29.2% | 58.3% | 38.9% | 0.291 |
| 混合 | 86.2% | 100.0% | 8.3% | 15.4% | 0.163 |
| LLM | 准确率(τ=0.5) | ROC-AUC | 最优 τ* |
|---|
| Llama 3.3 70B | 91% | 0.81 | 0.71 |
| Mistral 7B Instruct | 87% | 0.79 | 0.58 |
| Qwen 3.5 35B | 69–71% | 0.73 | 0.43 |
| HIGH | > 0.8 | 可靠 |
| MEDIUM | 0.5 - 0.8 | 重要场合请核实 |
| LOW | 0.3 - 0.5 | 可能存在问题 |
| VERY_LOW | < 0.3 | 不可信任 |