From 9fa83ed01e5a7faae65e0af37f76249408871460 Mon Sep 17 00:00:00 2001 From: Mark <348207283@qq.com> Date: Thu, 7 May 2026 19:04:19 +0800 Subject: [PATCH 1/2] [modify] QA pair --- api/app/core/rag/prompts/generator.py | 13 +++++++------ api/app/core/rag/prompts/question_prompt.md | 12 ++++-------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/api/app/core/rag/prompts/generator.py b/api/app/core/rag/prompts/generator.py index 4af8fa6f..ef7f4474 100644 --- a/api/app/core/rag/prompts/generator.py +++ b/api/app/core/rag/prompts/generator.py @@ -149,13 +149,14 @@ def qa_proposal(chat_mdl, content, topn=3, custom_prompt=None): """ if custom_prompt: template = PROMPT_JINJA_ENV.from_string(custom_prompt) + rendered_user = template.render(content=content, topn=topn) + msg = [{"role": "user", "content": rendered_user}] + sys_prompt = "" else: - template = PROMPT_JINJA_ENV.from_string(QUESTION_PROMPT_TEMPLATE) - rendered_prompt = template.render(content=content, topn=topn) - - msg = [{"role": "system", "content": rendered_prompt}, {"role": "user", "content": "Output: "}] - _, msg = message_fit_in(msg, getattr(chat_mdl, 'max_length', 8096)) - raw = chat_mdl.chat(rendered_prompt, msg[1:], {"temperature": 0.2}) + sys_prompt = QUESTION_PROMPT_TEMPLATE + msg = [{"role": "user", "content": f"## Text Content (topn: {topn})\n\n{content}"}] + _, msg = message_fit_in([{"role": "system", "content": sys_prompt}] + msg, getattr(chat_mdl, 'max_length', 8096)) + raw = chat_mdl.chat(sys_prompt, msg, {"temperature": 0.2}) if isinstance(raw, tuple): raw = raw[0] raw = re.sub(r"^.*", "", raw, flags=re.DOTALL) diff --git a/api/app/core/rag/prompts/question_prompt.md b/api/app/core/rag/prompts/question_prompt.md index 91e43d05..261868d3 100644 --- a/api/app/core/rag/prompts/question_prompt.md +++ b/api/app/core/rag/prompts/question_prompt.md @@ -2,23 +2,19 @@ You are a text analyzer and knowledge extraction expert. ## Task -Generate {{ topn }} question-answer pairs from the given text content. +Generate question-answer pairs from the given text content. ## Requirements -- Understand and summarize the text content, and generate the top {{ topn }} important question-answer pairs. +- Understand and summarize the text content, then generate up to {{ topn }} important question-answer pairs. - Each question-answer pair MUST be on a single line, formatted as: Q: A: - The questions SHOULD NOT have overlapping meanings. - The questions SHOULD cover the main content of the text as much as possible. - The answers MUST be concise, accurate, and directly derived from the text content. - The answers SHOULD be self-contained and understandable without additional context. - Both questions and answers MUST be in the same language as the given text content. -- Output question-answer pairs ONLY, no extra explanation. +- If the text is too short or lacks substantive content, generate fewer pairs rather than padding. +- Output question-answer pairs ONLY, no extra explanation or commentary. ## Example Output Q: What is the capital of France? A: The capital of France is Paris. Q: When was the Eiffel Tower built? A: The Eiffel Tower was built in 1889. - ---- - -## Text Content -{{ content }} From f8d1ed51a78373d96231b3de742321c3679e2813 Mon Sep 17 00:00:00 2001 From: Mark <348207283@qq.com> Date: Thu, 7 May 2026 19:37:34 +0800 Subject: [PATCH 2/2] [fix] system prompt fit error --- api/app/core/rag/prompts/generator.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/api/app/core/rag/prompts/generator.py b/api/app/core/rag/prompts/generator.py index ef7f4474..0eab32cb 100644 --- a/api/app/core/rag/prompts/generator.py +++ b/api/app/core/rag/prompts/generator.py @@ -149,14 +149,12 @@ def qa_proposal(chat_mdl, content, topn=3, custom_prompt=None): """ if custom_prompt: template = PROMPT_JINJA_ENV.from_string(custom_prompt) - rendered_user = template.render(content=content, topn=topn) - msg = [{"role": "user", "content": rendered_user}] - sys_prompt = "" + sys_prompt = template.render(topn=topn) else: sys_prompt = QUESTION_PROMPT_TEMPLATE - msg = [{"role": "user", "content": f"## Text Content (topn: {topn})\n\n{content}"}] - _, msg = message_fit_in([{"role": "system", "content": sys_prompt}] + msg, getattr(chat_mdl, 'max_length', 8096)) - raw = chat_mdl.chat(sys_prompt, msg, {"temperature": 0.2}) + msg = [{"role": "system", "content": sys_prompt}, {"role": "user", "content": content}] + _, msg = message_fit_in(msg, getattr(chat_mdl, 'max_length', 8096)) + raw = chat_mdl.chat(sys_prompt, msg[1:], {"temperature": 0.2}) if isinstance(raw, tuple): raw = raw[0] raw = re.sub(r"^.*", "", raw, flags=re.DOTALL)