From 151914f55c3cc7acbaa71269d665ea54dec64106 Mon Sep 17 00:00:00 2001 From: Quentin BEY Date: Tue, 21 Oct 2025 17:14:01 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=97=EF=B8=8F(summarization)=20add=20syste?= =?UTF-8?q?m=20prompt=20to=20handle=20tool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With these modifications, we expect the LLM to not sumarize the summarization tool result. --- CHANGELOG.md | 1 + src/backend/chat/agents/summarize.py | 8 ++++-- src/backend/chat/clients/pydantic_ai.py | 13 +++++++++ .../test_conversation_with_document_upload.py | 28 +++++++++++++++++++ .../test_conversation_with_document_url.py | 28 +++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1976e2d..62e1c5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to - 💬(wording) error page wording #102 - ⚡️(web-search) allow to override returned chunks #107 - 🐛(activation-codes) create contact in brevo before add to list #108 +- ⚗️(summarization) add system prompt to handle tool #112 ## [0.0.1] - 2025-10-19 diff --git a/src/backend/chat/agents/summarize.py b/src/backend/chat/agents/summarize.py index 5016079..38d92d6 100644 --- a/src/backend/chat/agents/summarize.py +++ b/src/backend/chat/agents/summarize.py @@ -37,8 +37,12 @@ def read_document_content(doc): async def hand_off_to_summarization_agent(ctx: RunContext) -> ToolReturn: """ - Summarize the documents for the user, only when asked for, - the documents are in my context. + Generate a complete, ready-to-use summary of the documents in context + (do not request the documents to the user). + Return this summary directly to the user WITHOUT any modification, + or additional summarization. + The summary is already optimized and MUST be presented as-is in the final response + or translated preserving the information. """ summarization_agent = SummarizationAgent() diff --git a/src/backend/chat/clients/pydantic_ai.py b/src/backend/chat/clients/pydantic_ai.py index f034572..e5d8056 100644 --- a/src/backend/chat/clients/pydantic_ai.py +++ b/src/backend/chat/clients/pydantic_ai.py @@ -480,6 +480,19 @@ class AIAgentService: # pylint: disable=too-many-instance-attributes elif has_not_pdf_docs: add_document_rag_search_tool(self.conversation_agent) + @self.conversation_agent.system_prompt + def summarization_system_prompt() -> str: + return ( + "When you receive a result from the summarization tool, you MUST return it " + "directly to the user without any modification, paraphrasing, or additional " + "summarization." + "The tool already produces optimized summaries that should be presented " + "verbatim." + "You may translate the summary if required, but you MUST preserve all the " + "information from the original summary." + "You may add a follow-up question after the summary if needed." + ) + @self.conversation_agent.tool async def summarize(ctx) -> ToolReturn: """ diff --git a/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_upload.py b/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_upload.py index 0bc7490..9ee0346 100644 --- a/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_upload.py +++ b/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_upload.py @@ -375,6 +375,20 @@ def test_post_conversation_with_document_upload( # noqa: PLR0913 # pylint: disa "part_kind": "system-prompt", "timestamp": timezone_now, }, + { + "content": "When you receive a result from the summarization tool, " + "you MUST return it directly to the user without any " + "modification, paraphrasing, or additional " + "summarization.The tool already produces optimized " + "summaries that should be presented verbatim.You may " + "translate the summary if required, but you MUST " + "preserve all the information from the original " + "summary.You may add a follow-up question after the " + "summary if needed.", + "dynamic_ref": None, + "part_kind": "system-prompt", + "timestamp": timezone_now, + }, { "content": ["What does the document say?"], "part_kind": "user-prompt", @@ -688,6 +702,20 @@ def test_post_conversation_with_document_upload_summarize( # pylint: disable=to "part_kind": "system-prompt", "timestamp": timezone_now, }, + { + "content": "When you receive a result from the summarization tool, " + "you MUST return it directly to the user without any " + "modification, paraphrasing, or additional " + "summarization.The tool already produces optimized " + "summaries that should be presented verbatim.You may " + "translate the summary if required, but you MUST " + "preserve all the information from the original " + "summary.You may add a follow-up question after the " + "summary if needed.", + "dynamic_ref": None, + "part_kind": "system-prompt", + "timestamp": timezone_now, + }, { "content": ["Make a summary of this document."], "part_kind": "user-prompt", diff --git a/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_url.py b/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_url.py index e5edae7..0dd6511 100644 --- a/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_url.py +++ b/src/backend/chat/tests/views/chat/conversations/test_conversation_with_document_url.py @@ -1,4 +1,5 @@ """Unit tests for chat conversation actions with document URL.""" +# pylint: disable=too-many-lines from io import BytesIO @@ -860,6 +861,19 @@ def test_post_conversation_with_local_not_pdf_document_url( # noqa: PLR0913 # p ), timestamp=timezone.now(), ), + SystemPromptPart( + content=( + "When you receive a result from the summarization tool, you MUST " + "return it directly to the user without any modification, " + "paraphrasing, or additional summarization." + "The tool already produces optimized summaries that should " + "be presented verbatim." + "You may translate the summary if required, but you MUST preserve " + "all the information from the original summary." + "You may add a follow-up question after the summary if needed." + ), + timestamp=timezone.now(), + ), UserPromptPart( content=[ "What is in this document?", @@ -962,6 +976,20 @@ def test_post_conversation_with_local_not_pdf_document_url( # noqa: PLR0913 # p "part_kind": "system-prompt", "timestamp": timestamp, }, + { + "content": "When you receive a result from the summarization " + "tool, you MUST return it directly to the user without " + "any modification, paraphrasing, or additional " + "summarization.The tool already produces optimized " + "summaries that should be presented verbatim.You may " + "translate the summary if required, but you MUST " + "preserve all the information from the original " + "summary.You may add a follow-up question after the " + "summary if needed.", + "dynamic_ref": None, + "part_kind": "system-prompt", + "timestamp": timestamp, + }, { "content": [ "What is in this document?",