Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 1, 2025

  • 分析问题:当 chunks 不足以回答问题时,AI 仍然强行生成答案
  • 定位问题源头:LightRAG 的 rag_responsenaive_rag_response prompts
  • 发现根本原因:
    • 现有 prompts 中虽然有指令让 AI 在信息不足时承认无法回答
    • 但指令位于长文本中间,容易被忽视
    • 指令不够强调,缺乏明确的"无法回答"格式
  • 增强 prompts:在 prompt_manager.py 中添加对 rag_responsenaive_rag_response 的自定义支持
  • 更新 prompts 内容:强化"信息不足时拒绝回答"的指令
  • 更新环境变量配置示例 (env.example)
  • 更新租户配置模型 (tenant_config.py)
  • 添加功能文档 (docs/strict_grounding_mode.md)
  • 代码审查 - 修复:
    • 统一引用格式(使用 - [n] 而非 * [n]
    • 提取 _is_strict_grounding_enabled 辅助函数减少代码重复
    • 修复布尔值类型处理(支持 trueTrue
    • 改进 env.example 文档,默认注释掉 strict_grounding
  • CodeQL 安全扫描通过
  • 添加查询模式与 Prompt 对应关系文档说明
Original prompt

检查当前prompts实现,为什么在chunks不足以回答某问题的时候,AI还是会强行变出一个答案来?


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 1, 2025 04:01
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Copilot AI changed the title [WIP] Investigate AI response behavior for insufficient chunks feat: Add strict grounding mode to prevent AI from fabricating answers when context is insufficient Dec 1, 2025
Copilot AI requested a review from BukeLy December 1, 2025 04:07
@BukeLy BukeLy requested review from Copilot and removed request for BukeLy December 1, 2025 04:15
@BukeLy BukeLy marked this pull request as ready for review December 1, 2025 04:15
@BukeLy BukeLy self-requested a review December 1, 2025 04:15
Copy link
Owner

@BukeLy BukeLy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why there are only navie mode and global mode customized prompts?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a strict grounding mode feature to prevent LLM hallucination when the knowledge base lacks sufficient information to answer queries. The implementation adds enhanced prompt templates with explicit grounding rules that require the AI to acknowledge when context is insufficient rather than fabricating answers using general knowledge.

Key Changes:

  • Added two enhanced prompt templates (ENHANCED_RAG_RESPONSE and ENHANCED_NAIVE_RAG_RESPONSE) with strict grounding instructions prominently positioned at the top
  • Implemented configurable strict grounding mode via environment variable (LIGHTRAG_STRICT_GROUNDING) or per-tenant configuration (strict_grounding)
  • Maintained proper configuration priority: tenant custom prompts > environment custom prompts > strict grounding enhanced prompts > LightRAG defaults

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/prompt_manager.py Adds enhanced prompt templates with strict grounding rules and implements the configuration logic to enable/disable strict mode globally or per-tenant
src/tenant_config.py Updates the configuration model documentation example to include the new strict_grounding, rag_response, and naive_rag_response options
env.example Documents the new LIGHTRAG_STRICT_GROUNDING environment variable and custom prompt override options
docs/strict_grounding_mode.md Comprehensive documentation explaining the problem, solution, usage, configuration priority, and effects with examples in Chinese

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +122 to +125
> 抱歉,根据当前知识库中的内容,我无法找到与您问题直接相关的信息。请尝试:
> - 重新表述您的问题
> - 提供更多上下文信息
> - 确认相关文档是否已上传到知识库
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "unable to answer" message is hardcoded in Chinese. This conflicts with the instruction on line 155 that states "The response MUST be in the same language as the user query."

Consider either:

  1. Making this message a template variable that can be localized, or
  2. Adding a note in the prompt that this specific fallback message is an exception to the language matching rule and should be translated to match the query language

Copilot uses AI. Check for mistakes.
tenant_key="strict_grounding",
tenant_config=tenant_config
)
return use_strict is not None and use_strict.lower() in ("true", "1", "yes", "on")
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string comparison with .lower() could raise an AttributeError if use_strict is not a string. The _get_prompt_value function returns str | None, but if someone passes a boolean value through tenant configuration (e.g., {"strict_grounding": true} instead of {"strict_grounding": "true"}), this will fail.

Consider adding type checking:

return use_strict is not None and str(use_strict).lower() in ("true", "1", "yes", "on")
Suggested change
return use_strict is not None and use_strict.lower() in ("true", "1", "yes", "on")
return use_strict is not None and str(use_strict).lower() in ("true", "1", "yes", "on")

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +70
- Strictly adhere to the provided context from the **Context**; DO NOT invent, assume, or infer any information not explicitly stated.
- If the answer cannot be found in the **Context**, state that you do not have enough information to answer. Do not attempt to guess.
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The grounding instructions are duplicated between the "Critical Grounding Rules" section (lines 31-55) and the "Content & Grounding" section (lines 68-70). While repetition can reinforce important instructions, this creates maintenance overhead and potential inconsistencies.

Consider:

  1. Keeping only the more detailed "Critical Grounding Rules" section and removing the redundant "Content & Grounding" section, or
  2. Adding a reference in "Content & Grounding" that points back to the critical rules rather than repeating them

This same issue exists in ENHANCED_NAIVE_RAG_RESPONSE (lines 113-137 vs 150-152).

Suggested change
- Strictly adhere to the provided context from the **Context**; DO NOT invent, assume, or infer any information not explicitly stated.
- If the answer cannot be found in the **Context**, state that you do not have enough information to answer. Do not attempt to guess.
- See the **Critical Grounding Rules (MUST FOLLOW)** section above for all requirements regarding use of context and handling insufficient information.

Copilot uses AI. Check for mistakes.
env.example Outdated
# 启用后,当知识库中没有足够信息回答问题时,AI 会明确拒绝回答
# 而不是强行编造答案
# 可选值: true, false(默认 false,不改变 LightRAG 原有行为)
LIGHTRAG_STRICT_GROUNDING=true
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Documentation inconsistency: The documentation states the default behavior is false when strict grounding is not set (line 263: "默认 false,不改变 LightRAG 原有行为"), and line 264 shows the example as LIGHTRAG_STRICT_GROUNDING=true.

However, looking at the code in prompt_manager.py, when use_strict_grounding is False, the code doesn't set any prompt at all (lines 285-287, 298-300), which means it will use whatever LightRAG's default prompt is. This is correct behavior, but the env.example should make it clearer that:

  1. When not set or set to false: uses LightRAG's original prompts (no change)
  2. When set to true: uses enhanced prompts with strict grounding

Consider updating line 264 to show the disabled state as the example:

# LIGHTRAG_STRICT_GROUNDING=true  # Uncomment to enable

Or add a comment clarifying the default:

# 默认:注释掉或设置为 false(使用 LightRAG 原生行为)
LIGHTRAG_STRICT_GROUNDING=true
Suggested change
LIGHTRAG_STRICT_GROUNDING=true
# 默认:注释掉或设置为 false(使用 LightRAG 原生行为)
# LIGHTRAG_STRICT_GROUNDING=true # 取消注释以启用严格 Grounding

Copilot uses AI. Check for mistakes.
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 1, 2025

Why there are only navie mode and global mode customized prompts?

LightRAG 库中只定义了两种响应 Prompt:rag_responsenaive_rag_response

  • rag_responselocalglobalhybridmix 四种模式共享使用
  • naive_rag_response 仅用于 naive 模式

这是 LightRAG 的设计决定,而非遗漏。已在文档中添加了"Prompt 与查询模式的对应关系"表格来说明这一点(commit 4cc8df8)。

Copilot AI requested a review from BukeLy December 1, 2025 04:22
Copy link
Owner

@BukeLy BukeLy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前有实现过自定义Prompts的功能,在租户配置中。现在是否有功能重复?

Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 1, 2025

之前有实现过自定义Prompts的功能,在租户配置中。现在是否有功能重复?

没有功能重复。原有的 prompt_manager.py 只支持实体提取相关的 prompts(entity_extraction_system_promptentity_extraction_user_prompt 等),用于控制 LightRAG 如何从文档中提取实体和关系。

这次更新扩展了功能,增加了对查询响应 prompts 的支持(rag_responsenaive_rag_response),用于控制 LLM 如何基于检索到的上下文生成回答。

简而言之:

  • 原有功能:自定义文档入库时的实体提取行为
  • 新增功能:自定义查询时的响应生成行为

两者互补,不重复。

Copilot AI requested a review from BukeLy December 1, 2025 04:28
Copy link
Owner

@BukeLy BukeLy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LightRAG原有的行为就是在没有明确上下文的情况下生成No context xxxx的,为什么换其他LLM会导致这个功能失效?

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

BukeLy and others added 2 commits December 1, 2025 12:42
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@BukeLy BukeLy merged commit 3cbf578 into main Dec 1, 2025
9 checks passed
@BukeLy BukeLy deleted the copilot/investigate-prompt-response-issues branch December 2, 2025 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants