From fba2356c8150ab6b5d543da1d12d66e9d5299e9f Mon Sep 17 00:00:00 2001 From: yangdx Date: Fri, 26 Sep 2025 10:02:01 +0800 Subject: [PATCH] Move user_prompt to system prompt - Refactor query prompt handling to separate user prompts in system context - Simplify user_query to only contain query - Apply changes to both kg_query and naive_query --- lightrag/operate.py | 20 +++++++++----------- lightrag/prompt.py | 8 ++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lightrag/operate.py b/lightrag/operate.py index 685e86a8..dc4ae1df 100644 --- a/lightrag/operate.py +++ b/lightrag/operate.py @@ -2392,15 +2392,14 @@ async def kg_query( # Build system prompt sys_prompt_temp = system_prompt if system_prompt else PROMPTS["rag_response"] sys_prompt = sys_prompt_temp.format( + user_prompt=f"```\n{query_param.user_prompt}\n```" + if query_param.user_prompt + else "n/a", context_data=context_result.context, response_type=query_param.response_type, ) - user_query = ( - "\n\n".join([query, query_param.user_prompt]) - if query_param.user_prompt - else query - ) + user_query = query if query_param.only_need_prompt: prompt_content = "\n\n".join([sys_prompt, "---User Query---", user_query]) @@ -4264,18 +4263,17 @@ async def naive_query( if query_param.only_need_context and not query_param.only_need_prompt: return QueryResult(content=context_content, raw_data=raw_data) - user_query = ( - "\n\n".join([query, query_param.user_prompt]) - if query_param.user_prompt - else query - ) - sys_prompt_temp = system_prompt if system_prompt else PROMPTS["naive_rag_response"] sys_prompt = sys_prompt_temp.format( + user_prompt=f"```\n{query_param.user_prompt}\n```" + if query_param.user_prompt + else "n/a", content_data=text_units_str, response_type=query_param.response_type, ) + user_query = query + if query_param.only_need_prompt: prompt_content = "\n\n".join([sys_prompt, "---User Query---", user_query]) return QueryResult(content=prompt_content, raw_data=raw_data) diff --git a/lightrag/prompt.py b/lightrag/prompt.py index 87f12792..c912f340 100644 --- a/lightrag/prompt.py +++ b/lightrag/prompt.py @@ -243,6 +243,7 @@ Consider the conversation history if provided to maintain conversational flow an - The Document Title in the citation must retain its original language. - Output each citation on an individual line - Provide maximum of 5 most relevant citations. + - Do not generate footnotes section or any text after the references. 5. **Reference Section Example:** ``` @@ -252,6 +253,9 @@ Consider the conversation history if provided to maintain conversational flow an * [3] Document Title Three ``` +6. **Additional Instructions**: {user_prompt} + + ---Source Data--- Knowledge Graph and Document Chunks: @@ -291,6 +295,7 @@ Consider the conversation history if provided to maintain conversational flow an - The Document Title in the citation must retain its original language. - Output each citation on an individual line - Provide maximum of 5 most relevant citations. + - Do not generate footnotes section or any text after the references. 5. **Reference Section Example:** ``` @@ -300,6 +305,9 @@ Consider the conversation history if provided to maintain conversational flow an * [3] Document Title Three ``` +6. **Additional Instructions**: {user_prompt} + + ---Source Data--- Document Chunks: