-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix: skip reference instructions in LLM prompt when include_references=false #2833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
majiayu000
wants to merge
2
commits into
HKUDS:main
Choose a base branch
from
majiayu000:fix/issue-2832-skip-reference-prompt-when-disabled
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−19
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Tests that include_references=False removes reference instructions from LLM prompts.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from lightrag.prompt import PROMPTS | ||
|
|
||
| pytestmark = pytest.mark.offline | ||
|
|
||
|
|
||
| class TestRagResponsePromptVariants: | ||
| """Verify rag_response and rag_response_no_ref prompt templates.""" | ||
|
|
||
| def test_rag_response_contains_reference_instructions(self): | ||
| prompt = PROMPTS["rag_response"] | ||
| assert "References Section Format" in prompt | ||
| assert "reference_id" in prompt | ||
| assert "Reference Document List" in prompt | ||
|
|
||
| def test_rag_response_no_ref_omits_reference_instructions(self): | ||
| prompt = PROMPTS["rag_response_no_ref"] | ||
| assert "References Section Format" not in prompt | ||
| assert "reference_id" not in prompt | ||
| assert "Reference Document List" not in prompt | ||
|
|
||
| def test_rag_response_no_ref_has_required_placeholders(self): | ||
| prompt = PROMPTS["rag_response_no_ref"] | ||
| assert "{response_type}" in prompt | ||
| assert "{user_prompt}" in prompt | ||
| assert "{context_data}" in prompt | ||
|
|
||
|
|
||
| class TestNaiveRagResponsePromptVariants: | ||
| """Verify naive_rag_response and naive_rag_response_no_ref prompt templates.""" | ||
|
|
||
| def test_naive_rag_response_contains_reference_instructions(self): | ||
| prompt = PROMPTS["naive_rag_response"] | ||
| assert "References Section Format" in prompt | ||
| assert "reference_id" in prompt | ||
|
|
||
| def test_naive_rag_response_no_ref_omits_reference_instructions(self): | ||
| prompt = PROMPTS["naive_rag_response_no_ref"] | ||
| assert "References Section Format" not in prompt | ||
| assert "reference_id" not in prompt | ||
|
|
||
| def test_naive_rag_response_no_ref_has_required_placeholders(self): | ||
| prompt = PROMPTS["naive_rag_response_no_ref"] | ||
| assert "{response_type}" in prompt | ||
| assert "{user_prompt}" in prompt | ||
| assert "{content_data}" in prompt | ||
|
|
||
|
|
||
| class TestContextTemplateVariants: | ||
| """Verify context templates with and without reference sections.""" | ||
|
|
||
| def test_kg_context_contains_reference_list(self): | ||
| template = PROMPTS["kg_query_context"] | ||
| assert "Reference Document List" in template | ||
| assert "{reference_list_str}" in template | ||
|
|
||
| def test_kg_context_no_ref_omits_reference_list(self): | ||
| template = PROMPTS["kg_query_context_no_ref"] | ||
| assert "Reference Document List" not in template | ||
| assert "{reference_list_str}" not in template | ||
|
|
||
| def test_naive_context_contains_reference_list(self): | ||
| template = PROMPTS["naive_query_context"] | ||
| assert "Reference Document List" in template | ||
| assert "{reference_list_str}" in template | ||
|
|
||
| def test_naive_context_no_ref_omits_reference_list(self): | ||
| template = PROMPTS["naive_query_context_no_ref"] | ||
| assert "Reference Document List" not in template | ||
| assert "{reference_list_str}" not in template |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This branch makes
kg_querygenerate a different prompt wheninclude_referencesis false, but the query cache key still omits that flag in the subsequentcompute_args_hash(...)call. Withenable_llm_cache=true, a response cached for one setting can be served to the other (e.g., cached cited answer returned when citations are disabled), violating the API contract forinclude_references.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already fixed in f956920 — added
include_referencesto thecompute_args_hashcall forkg_query.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already fixed in f956920 — added
include_referencesto thecompute_args_hashcall forkg_query.