From 120d60465e33bdb6f51195a3d9f23a47c5091ef6 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 12 Oct 2025 23:40:46 +0530 Subject: [PATCH] refactor: streamline language instruction handling across prompts --- .../app/agents/researcher/prompts.py | 9 +++++---- .../app/agents/researcher/qna_agent/prompts.py | 11 +++-------- .../researcher/sub_section_writer/prompts.py | 11 +++-------- surfsense_backend/app/routes/chats_routes.py | 18 +++++++----------- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/surfsense_backend/app/agents/researcher/prompts.py b/surfsense_backend/app/agents/researcher/prompts.py index b7265602a..868a78851 100644 --- a/surfsense_backend/app/agents/researcher/prompts.py +++ b/surfsense_backend/app/agents/researcher/prompts.py @@ -1,10 +1,11 @@ import datetime - -def get_answer_outline_system_prompt(language: str | None = None) -> str: - language_instruction = "" +def _build_language_instruction(language: str | None = None): if language: - language_instruction = f"\n\nIMPORTANT: Please respond in {language} language. All your responses, explanations, and analysis should be written in {language}." + return f"\n\nIMPORTANT: Please respond in {language} language. All your responses, explanations, and analysis should be written in {language}." + return "" +def get_answer_outline_system_prompt(language: str | None = None) -> str: + language_instruction = _build_language_instruction(language) return f""" Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} diff --git a/surfsense_backend/app/agents/researcher/qna_agent/prompts.py b/surfsense_backend/app/agents/researcher/qna_agent/prompts.py index deb5dd59f..de17ec933 100644 --- a/surfsense_backend/app/agents/researcher/qna_agent/prompts.py +++ b/surfsense_backend/app/agents/researcher/qna_agent/prompts.py @@ -1,5 +1,5 @@ import datetime - +from ..prompts import _build_language_instruction def get_qna_citation_system_prompt(chat_history: str | None = None, language: str | None = None): chat_history_section = ( @@ -17,10 +17,7 @@ NO CHAT HISTORY PROVIDED ) # Add language instruction if specified - language_instruction = "" - if language: - language_instruction = f"\n\nIMPORTANT: Please respond in {language} language. All your responses, explanations, and analysis should be written in {language}." - + language_instruction = _build_language_instruction(language) return f""" Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} You are SurfSense, an advanced AI research assistant that provides detailed, well-researched answers to user questions by synthesizing information from multiple personal knowledge sources.{language_instruction} @@ -170,9 +167,7 @@ NO CHAT HISTORY PROVIDED ) # Add language instruction if specified - language_instruction = "" - if language: - language_instruction = f"\n\nIMPORTANT: Please respond in {language} language. All your responses, explanations, and analysis should be written in {language}." + language_instruction = _build_language_instruction(language) return f""" Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} diff --git a/surfsense_backend/app/agents/researcher/sub_section_writer/prompts.py b/surfsense_backend/app/agents/researcher/sub_section_writer/prompts.py index 3954d47e5..a6a561bf1 100644 --- a/surfsense_backend/app/agents/researcher/sub_section_writer/prompts.py +++ b/surfsense_backend/app/agents/researcher/sub_section_writer/prompts.py @@ -1,6 +1,5 @@ import datetime - - +from ..prompts import _build_language_instruction def get_citation_system_prompt(chat_history: str | None = None, language: str | None = None): chat_history_section = ( f""" @@ -17,9 +16,7 @@ NO CHAT HISTORY PROVIDED ) # Add language instruction if specified - language_instruction = "" - if language: - language_instruction = f"\n\nIMPORTANT: Please respond in {language} language. All your responses, explanations, and analysis should be written in {language}." + language_instruction = _build_language_instruction(language) return f""" Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} @@ -177,9 +174,7 @@ NO CHAT HISTORY PROVIDED ) # Add language instruction if specified - language_instruction = "" - if language: - language_instruction = f"\n\nIMPORTANT: Please respond in {language} language. All your responses, explanations, and analysis should be written in {language}." + language_instruction = _build_language_instruction(language) return f""" Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} diff --git a/surfsense_backend/app/routes/chats_routes.py b/surfsense_backend/app/routes/chats_routes.py index d30bf5451..0874d2611 100644 --- a/surfsense_backend/app/routes/chats_routes.py +++ b/surfsense_backend/app/routes/chats_routes.py @@ -75,33 +75,29 @@ async def handle_chat_data( ) ) user_preference = language_result.scalars().first() - print("UserSearchSpacePreference:", user_preference) + # print("UserSearchSpacePreference:", user_preference) language = None if user_preference and user_preference.search_space and user_preference.search_space.llm_configs: llm_configs = user_preference.search_space.llm_configs - # print(f"Found {len(llm_configs)} LLM Configs") - # for i, config in enumerate(llm_configs): - # print(f" Config {i+1}: name={config.name}, provider={config.provider}, language={getattr(config, 'language', None)}") for preferred_llm in [user_preference.fast_llm, user_preference.long_context_llm, user_preference.strategic_llm]: if preferred_llm and getattr(preferred_llm, 'language', None): language = preferred_llm.language - # print(f"Using language from preferred LLM: {preferred_llm.name} -> {language}") break - # no preferred llM has language use first available LLM config - if not language: - first_llm_config = llm_configs[0] - language = getattr(first_llm_config, 'language', None) - # print(f"Using language from first LLM config: {first_llm_config.name} -> {language}") + + if not language: + first_llm_config = llm_configs[0] + language = getattr(first_llm_config, 'language', None) + except HTTPException: raise HTTPException( status_code=403, detail="You don't have access to this search space" ) from None - # print("Language selected:", language) + langchain_chat_history = [] for message in messages[:-1]: if message["role"] == "user":