RAG聊天防幻觉

SKILLAI 工具
AI 工具 未标注权限 许可证不明确
安装这个技能

Use this skill when building or debugging a local/offline RAG chat assistant with llama_index (CondensePlusContextChatEngine / as_chat_engine) and the answers hallucinate, ignore configured retrieval, or feel "dumber" in multi-turn chat than in single-turn eval. It documents the critical pitfall where index.as_chat_engine(query_engine=...) silently discards the injected hybrid retriever and rerank/compression node postprocessors (falling back to a default dense VectorIndexRetriever), and provides the fix plus a zero-extra-LLM-call anti-hallucination hardening pattern: system-prompt hard constraints, rerank threshold plus lexical-overlap guard, policy anchor-term guard, answer-backtrace guard, fact-table injection, month/topic knowledge-base organization, and widened top-k. Trigger phrases: RAG 幻觉 / 答错 / 编造, as_chat_engine 不生效 / 没用上混合检索 / 重排, chat engine ignores retriever, llama_index 多轮对话检索退化, 政务 RAG 答非所问, offline RAG hallucination, CondensePlusContextChatEngine retriever not honored.

一句话选型结论

按资料字段整理

RAG聊天防幻觉是AI 工具类的 Agent Skill,安装方式未标注,权限级别未标注,许可证不明确,有可直接复制的安装命令。技能本质是给模型读的说明书,效果取决于模型与客户端;装完先用一个小任务验证,再放进正式流程。

分类AI 工具 安装方式未标注 权限级别未标注权限 许可证许可证不明确
累计安装 未标注 本周 未标注
GitHub 星标 未标注 仓库 未标注
下载量 未标注 技能市场下载数
权限级别 未标注权限 安装前请确认风险范围
许可证 许可证不明确 未说明

安装

未标注
在支持 Skills 的客户端里执行,装完问一句「你有哪些技能」验证
skillhub install user_9d55ab87/rag-chat-engine-hardening
  1. 1
    确认客户端支持

    确认你的客户端支持 Skills(Claude、Codex、Cursor 等),并升级到较新的版本。

  2. 2
    拿到技能文件

    执行上面的命令安装;也可以把仓库里的 SKILL.md 手动放进客户端的 skills 目录。

  3. 3
    刷新技能列表

    重新打开客户端,或刷新技能列表,让新放进来的技能被加载。

  4. 4
    验证是否生效

    问一句「你有哪些技能」,确认列表里出现它,再拿一个小任务试跑。

技能档案

AI 工具
字段来自技能仓库与公开索引,中文说明由本站标注
作者 / 维护方user_9d55ab87仓库 未标注 · 版本 未标注
宿主客户端未标注技能可被这些客户端加载
安装方式未标注安装包 community
权限级别未标注权限安装前请确认风险范围
许可证未说明许可证不明确,商用前请自行确认条款
仓库可用性available技能仓库当前是否可访问
最近核验未标注上游最后核验时间 · 数据更新 1791623961
正文体量9.7 KBSKILL.md 原文大小,越大说明说明书写得越细

典型使用场景

3 类
按分类整理的常见用法,具体以 SKILL.md 为准
组合多个 AI 能力

把写作、检索、总结串成一条流程。

搭工作流

把固定流程做成可重复调用的步骤。

做提示词与评测

整理提示词并对比输出效果。

选择建议

装之前先看这几条
按条目字段给出的判断,不替你决定用不用

值得选的地方

  • 有可直接复制的安装命令,不用自己翻文档找入口。

先想清楚的地方

  • 没有标注权限级别,实际能做什么要装好后确认。
  • 许可证不明确,商用或二次分发前先到仓库确认条款。
  • 没有做站内核验,内容以作者仓库为准。
  • 技能只是说明书,实际效果取决于模型与客户端,装完先用小任务试一遍。

更适合

  • 需要在AI 工具场景里给模型补一套做法的人。
  • 愿意先读一遍 SKILL.md、确认它会让模型做什么的人。

不太适合

  • 对许可证要求很严、必须明确可商用的人。
  • 只想装完就自动变强、不打算看说明书的人。

同类对比

6 个同类
同分类里挑最接近的几条,字段以各自详情页为准
技能权限级别安装方式累计安装宿主客户端安装命令
RAG聊天防幻觉当前 未标注权限 未标注 未标注 未标注 有
执行计划 只读 GitHub 仓库 151.5 万 Codex、Claude、Cursor 有
Playwright MCP 测试 浏览器控制 ClawHub 市场 136.5 万 OpenClaw 有
完成前的验证 未标注权限 GitHub 仓库 131.9 万 Codex、Claude、Cursor 有
子 Agent 驱动开发 只读 GitHub 仓库 131.2 万 Codex、Claude、Cursor 有
前端设计 执行命令 GitHub 仓库 125.6 万 Codex、Claude、Cursor 有

SKILL.md 全文

9.7 KB
模型实际读到的说明书原文,来自技能仓库

RAG Chat Engine Hardening

Overview

Local/offline RAG assistants (llama_index + Chroma/bge + Ollama qwen2.5:3b-class
CPU models) are extremely prone to silent retrieval degradation in the chat
path
and small-model hallucination. This skill captures two hard-won lessons
from a production government Q&A RAG:

  1. The as_chat_engine trap — the single most common reason a carefully
    configured Advanced RAG (hybrid retrieval + rerank) "works in eval but not in
    chat". It fails silently, so answers look plausible while retrieval is actually
    broken.
  2. The anti-hallucination hardening pattern — a set of rule-based guards and
    prompt/fact-table/data-organization changes that cost zero extra LLM calls and
    stop a 3B model from fabricating policy numbers.

Apply this skill whenever a RAG chat assistant returns wrong/garbled answers,
especially after "I added hybrid retrieval + rerank but it still answers badly",
or when a known-correct document exists in the knowledge base yet is never cited.

When To Use

  • User reports the chat bot "makes things up", answers off-topic, or cites the
    wrong policy.
  • Retrieval was upgraded (dense → hybrid, added rerank/compression) but answer
    quality did not improve — suspect the chat engine is not using it.
  • Building a new offline RAG chat assistant from scratch and want the guards baked
    in from day one.
  • Debugging "why does the standalone query engine find the right doc but the chat
    engine doesn't".

The Core Pitfall: as_chat_engine Discards Your Retriever

Symptom

index.as_chat_engine(query_engine=...) (or query_engine.as_chat_engine(...))
returns a chat engine, but at runtime chat_engine._retriever is a plain
VectorIndexRetriever (default similarity_top_k, no hybrid, no rerank).
Your carefully built retriever + node_postprocessors (rerank, compression,
LongContextReorder) are ignored. Advanced RAG only runs in the standalone
query_engine.query(...) / eval path — never in multi-turn chat. Result: relevant
special-topic docs get crowded out of top-k and the small LLM fabricates from
irrelevant snippets. This passes eval (which uses the query engine directly) and
fails in production (which uses the chat engine).

Root Cause

In the installed llama_index version, passing query_engine= to
as_chat_engine does not forward the engine's retriever / node_postprocessors
into the chat engine's retrieval step. The chat engine reconstructs a default
retriever from the index instead.

The Fix — Build The Chat Engine Directly

Do not go through as_chat_engine. Construct the chat engine with the
configured retriever and postprocessors passed explicitly:

from llama_index.core.chat_engine import CondensePlusContextChatEngine

def get_or_create_chat_engine(session_id="default"):
    index = _get_index()                      # your VectorStoreIndex
    retriever, postprocessors, rerank_ok = _build_query_kwargs(index)
    # retriever == your HybridRetriever (possibly wrapped by PolicyFilterRetriever)
    # postprocessors == [MetadataReplacement, ConditionalRerank, Compression, LongContextReorder]
    engine = CondensePlusContextChatEngine.from_defaults(
        retriever=retriever,                  # <-- honored in chat now
        node_postprocessors=postprocessors,   # <-- rerank/compression honored now
        llm=LlamaSettings.llm,
        memory=memory,                        # SimpleChatMemory / externalized store
        system_prompt=SYSTEM_PROMPT,          # your hardened prompt
    )
    return engine

CondensePlusContextChatEngine keeps multi-turn context (it condenses prior
turns into the retrieval query) while respecting the injected retriever and
postprocessors. Verify immediately after building: assert
type(engine._retriever).__name__ != "VectorIndexRetriever" (or equals your
custom retriever class).

> Note: RetrieverQueryEngine has no .as_chat_engine() method — calling it
> raises AttributeError. Always use CondensePlusContextChatEngine.from_defaults.

How To Detect The Trap Fast

Run this diagnostic against a live engine and inspect the retriever type:

ce = get_or_create_chat_engine()
print(type(ce._retriever).__name__)        # expect your Hybrid/PolicyFilter retriever
print([type(p).__name__ for p in getattr(ce, "node_postprocessors", [])])
# If it prints "VectorIndexRetriever" with an empty postprocessor list,
# the trap is active — apply the fix above.

Then stream a known-bad query and confirm the sources event lists the correct
policy document and that no anchor guard false-fires (see references).

Anti-Hallucination Hardening Pattern (zero extra LLM calls)

Layer these so a small model cannot fabricate. All are rule-based and add no
latency. Implement in order; each is independent and can be toggled.

  1. System-prompt hard constraint. Replace soft "don't fabricate" wording with
    three strict rules: answer only from retrieved content; if retrieved material
    lacks the authoritative clause, treat as "not found"; when unable, reply with
    exactly one fixed fallback sentence (e.g. "知识库中暂无相关政策的权威信息,建议拨打 12345 或到政务窗口咨询。").
    See references/anti_hallucination_guards.md for the exact prompt.

  2. rerank threshold guard + lexical-overlap anti-false-kill. After rerank, if
    top1 score < threshold (e.g. 0.2) and question↔snippet lexical overlap is
    weak → degrade to the fallback. The overlap check prevents误杀 of legitimately
    low-scoring in-domain hits (bge-reranker-base scores run low on Chinese).

  3. Policy anchor-term guard (third gate). Maintain an anchor-term list
    (产假/婚假/社保/工伤/补贴/退休/落户/营业执照 …). If the query names an anchor
    term but none of the retrieved corpus snippets contain it → force degrade.
    This catches the case where rerank thinks it's "related" by word overlap but the
    actual policy is absent. Always-on (no config flag); complements rule 1.

  4. Answer-backtrace guard (soft). After generation, check whether key numbers
    in the answer appear in the retrieved snippets (or fact-table hits). If none
    match, emit a gentle "核对官方最新政策" hint. Do not suppress the answer —
    it is a warning, not a block. Skip when a fact-table hit already supplied the
    number.

  5. Fact-table injection (SQL). Keep a SQLite table of precise facts
    (hotlines, deadlines, leave days, subsidy amounts). At query time, query it in
    parallel with vector retrieval; if hit, inject the authoritative text into the
    LLM context ("优先直接采用、不要改写数字") and show it as a distinct "📊 事实库"
    source. Small models extract numbers from prose poorly; a fact table makes them
    exact. Schema + loader in references.

  6. Widen top-k candidate pools. Hybrid VECTOR/BM25/FUSION_TOP_K 4 → 8,
    RERANK_TOP_N 4 → 6. A relevant special-topic doc that ranks #6 by dense
    similarity is otherwise excluded before rerank even sees it.

  7. Organize the knowledge base by month/topic. Name policy docs
    YYYY年M月_&lt;主题&gt;.md (e.g. 2026年8月_产假与生育津贴.md); add a
    README_知识库组织说明.md that is excluded from indexing (skip files whose
    name starts with README/说明/知识库组织说明 in the loader) so the human
    index is never answered as policy. Clear naming also makes maintenance auditable.

Detailed pseudocode for guards 1–5, the fact-table schema, the SYSTEM_PROMPT text,
and the KB loader skip rule live in references/anti_hallucination_guards.md.

Verification Recipe

After applying the fix, prove it end-to-end (do not trust eval alone):

  1. Run the retriever-type diagnostic above — confirm custom retriever + non-empty
    postprocessors.
  2. Pick a query whose correct doc previously ranked low (e.g. a special-topic
    question). Stream the answer and inspect the sources event: the correct
    policy doc must appear, and anchor_missing must be empty (no false degrade).
  3. Confirm the answer contains the authoritative number from the fact table (if
    applicable) and contains no numbers absent from sources.
  4. py_compile all changed modules; clean up temporary verification scripts.

Resources

  • references/anti_hallucination_guards.md — exact SYSTEM_PROMPT, guard
    implementations (rerank threshold + overlap, anchor-term, answer-backtrace),
    fact-table schema + query snippet, KB loader skip rule, and the
    CondensePlusContextChatEngine wiring in full.

常见问题

5 问
答案由本站依据条目字段整理
RAG聊天防幻觉怎么安装?

在支持 Skills 的客户端里执行:skillhub install user_9d55ab87/rag-chat-engine-hardening。安装方式为未标注。装完刷新技能列表或重开客户端。

RAG聊天防幻觉支持哪些客户端?

资料没有标注宿主客户端,但只要客户端支持 Skills(Claude、Codex、Cursor 等),一般都能加载。

RAG聊天防幻觉权限高吗,安全吗?

权限级别为未标注权限。安装前先看它会读取或写入哪些内容。

RAG聊天防幻觉收费吗?

本站不代收费用,也没有标注具体价格。是否收费取决于技能本身和所用的模型或客户端,正式使用前按作者说明确认。

RAG聊天防幻觉用什么许可证?

许可证标注为 未说明(许可证不明确)。商用或二次分发前请到仓库确认条款原文。

热门专题

按场景挑技能
全部专题 →

技能内容以作者仓库为准;分类、权限级别与许可证状态的中文说明为中文整理,最近核验 未标注。