Skip to content

Commit 3cbf578

Browse files
authored
feat: Add strict grounding mode to prevent AI from fabricating answers when context is insufficient (#4)
feat: 添加严格 Grounding 模式,防止 AI 在知识库信息不足时编造答案
1 parent 8b27a81 commit 3cbf578

4 files changed

Lines changed: 408 additions & 10 deletions

File tree

docs/strict_grounding_mode.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# 严格 Grounding 模式
2+
3+
## 问题背景
4+
5+
默认情况下,当知识库中的 chunks 不足以回答用户问题时,AI 可能会"强行编造"一个答案,即使这个答案没有依据。这是因为 LLM 有强大的生成能力,即使在没有足够上下文的情况下也会尝试给出"看起来合理"的回答。
6+
7+
## 解决方案
8+
9+
通过启用 **严格 Grounding 模式**,系统会使用增强版的 prompt,明确要求 AI 在信息不足时:
10+
11+
1. **评估上下文充分性**:在生成答案前,先判断上下文是否足够回答问题
12+
2. **明确拒绝回答**:如果信息不足,使用标准化的拒绝回答格式
13+
3. **禁止编造内容**:严格禁止使用 AI 的通用知识来填补知识库的空白
14+
15+
## 使用方法
16+
17+
### 方式 1:环境变量(全局配置)
18+
19+
`.env` 文件中添加:
20+
21+
```bash
22+
# 启用严格 Grounding 模式
23+
LIGHTRAG_STRICT_GROUNDING=true
24+
```
25+
26+
### 方式 2:租户配置(租户级覆盖)
27+
28+
通过 API 更新租户配置:
29+
30+
```bash
31+
curl -X PUT "http://localhost:8000/tenants/your_tenant/config" \
32+
-H "Content-Type: application/json" \
33+
-d '{
34+
"custom_prompts": {
35+
"strict_grounding": "true"
36+
}
37+
}'
38+
```
39+
40+
### 方式 3:完全自定义 Prompt
41+
42+
如果需要更精细的控制,可以完全自定义 RAG 响应 prompt:
43+
44+
```bash
45+
# 环境变量方式
46+
LIGHTRAG_RAG_RESPONSE_PROMPT="你的自定义 prompt..."
47+
LIGHTRAG_NAIVE_RAG_RESPONSE_PROMPT="你的自定义 naive 模式 prompt..."
48+
```
49+
50+
或通过租户配置 API:
51+
52+
```bash
53+
curl -X PUT "http://localhost:8000/tenants/your_tenant/config" \
54+
-H "Content-Type: application/json" \
55+
-d '{
56+
"custom_prompts": {
57+
"rag_response": "你的自定义 KG 模式 prompt...",
58+
"naive_rag_response": "你的自定义 naive 模式 prompt..."
59+
}
60+
}'
61+
```
62+
63+
## 配置优先级
64+
65+
从高到低:
66+
67+
1. **租户配置的 `rag_response`/`naive_rag_response`**(完全自定义)
68+
2. **环境变量 `LIGHTRAG_RAG_RESPONSE_PROMPT`/`LIGHTRAG_NAIVE_RAG_RESPONSE_PROMPT`**
69+
3. **`strict_grounding=true`**(使用增强版默认 prompt)
70+
4. **LightRAG 原生 prompt**(默认行为)
71+
72+
## 增强版 Prompt 的关键指令
73+
74+
启用严格 Grounding 模式后,prompt 会包含以下关键指令:
75+
76+
```markdown
77+
---Critical Grounding Rules (MUST FOLLOW)---
78+
79+
⚠️ **ABSOLUTE REQUIREMENT**: You must ONLY use information explicitly stated in the **Context**.
80+
81+
**Before generating any answer, you MUST evaluate:**
82+
1. Does the Context contain information that DIRECTLY answers the user's question?
83+
2. Is the information in the Context SUFFICIENT and RELEVANT to provide a complete answer?
84+
85+
**If the answer is NO to either question, you MUST respond with:**
86+
> 抱歉,根据当前知识库中的内容,我无法找到与您问题直接相关的信息。请尝试:
87+
> - 重新表述您的问题
88+
> - 提供更多上下文信息
89+
> - 确认相关文档是否已上传到知识库
90+
91+
**DO NOT:**
92+
- ❌ Make up or fabricate information not in the Context
93+
- ❌ Use your general knowledge to fill gaps
94+
- ❌ Provide speculative or assumed answers
95+
- ❌ Say "based on my knowledge" or similar phrases
96+
- ❌ Combine partial information to create misleading answers
97+
98+
**DO:**
99+
- ✅ Explicitly state when information is not available
100+
- ✅ Only cite facts that appear in the Context
101+
- ✅ Be honest about the limitations of the provided information
102+
```
103+
104+
## 效果对比
105+
106+
### 未启用严格 Grounding 模式
107+
108+
用户问题:`公司的年度收入是多少?`
109+
110+
(假设知识库中没有收入数据)
111+
112+
AI 可能回答:
113+
> 根据相关文档,该公司是一家成熟的企业...虽然具体年度收入数据未在文档中明确提及,但从其业务规模来看,估计年收入应该在...
114+
115+
### 启用严格 Grounding 模式
116+
117+
同样的问题,AI 会回答:
118+
> 抱歉,根据当前知识库中的内容,我无法找到与您问题直接相关的信息。请尝试:
119+
> - 重新表述您的问题
120+
> - 提供更多上下文信息
121+
> - 确认相关文档是否已上传到知识库
122+
123+
## 刷新配置
124+
125+
修改配置后,需要刷新租户实例缓存:
126+
127+
```bash
128+
# 刷新特定租户
129+
curl -X POST "http://localhost:8000/tenants/your_tenant/config/refresh"
130+
131+
# 或重启服务(全局生效)
132+
docker compose restart rag-api
133+
```
134+
135+
## Prompt 与查询模式的对应关系
136+
137+
LightRAG 有 5 种查询模式,但只使用 2 种响应 Prompt:
138+
139+
| 查询模式 | 使用的 Prompt | 说明 |
140+
|---------|--------------|------|
141+
| `naive` | `naive_rag_response` | 纯向量搜索,不使用知识图谱 |
142+
| `local` | `rag_response` | 局部知识图谱搜索 |
143+
| `global` | `rag_response` | 全局知识图谱搜索 |
144+
| `hybrid` | `rag_response` | 混合模式(local + global) |
145+
| `mix` | `rag_response` | 全功能混合(KG + 向量) |
146+
147+
因此,自定义 `rag_response` 会影响除 `naive` 以外的所有模式,而 `naive_rag_response` 仅影响 `naive` 模式。
148+
149+
## 相关配置
150+
151+
| 配置项 | 类型 | 描述 |
152+
|-------|------|------|
153+
| `LIGHTRAG_STRICT_GROUNDING` | 环境变量 | 全局启用严格 Grounding 模式 |
154+
| `strict_grounding` | 租户配置 | 租户级启用严格 Grounding 模式 |
155+
| `rag_response` | 租户配置 | 自定义 KG 模式响应 prompt(影响 local/global/hybrid/mix) |
156+
| `naive_rag_response` | 租户配置 | 自定义 naive 模式响应 prompt(仅影响 naive) |
157+
158+
## 注意事项
159+
160+
1. **语言适配**:当前默认拒绝回答消息是中文,如需英文或其他语言,请使用完全自定义 prompt
161+
2. **性能影响**:严格 Grounding 模式不会影响性能,仅修改 prompt 内容
162+
3. **兼容性**:此功能与所有查询模式(naive、local、global、hybrid、mix)兼容
163+
4. **Prompt 复用**:LightRAG 的设计中,`rag_response` 被 local/global/hybrid/mix 四种模式共享

env.example

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ RAG_CONTEXT_MODE=page
254254
# 最大上下文 tokens
255255
RAG_MAX_CONTEXT_TOKENS=3000
256256

257+
# ====== LightRAG 响应增强配置 ======
258+
# 控制 LLM 回答问题时的行为,防止在信息不足时编造答案
259+
260+
# --- 严格 Grounding 模式 ---
261+
# 启用后,当知识库中没有足够信息回答问题时,AI 会明确拒绝回答
262+
# 而不是强行编造答案
263+
# 可选值: true, false
264+
# 默认:注释掉或设置为 false(使用 LightRAG 原生行为)
265+
# LIGHTRAG_STRICT_GROUNDING=true # 取消注释以启用严格 Grounding
266+
267+
# --- 自定义 RAG 响应 Prompt(可选)---
268+
# 完全自定义 RAG 响应 prompt,覆盖默认和增强版
269+
# LIGHTRAG_RAG_RESPONSE_PROMPT="你的自定义 prompt..."
270+
# LIGHTRAG_NAIVE_RAG_RESPONSE_PROMPT="你的自定义 naive 模式 prompt..."
271+
257272
# ====== 存储后端配置 ======
258273
# LightRAG 存储层配置(必须与 docker-compose 中的服务匹配)
259274

0 commit comments

Comments
 (0)