refactor: streamline language instruction handling across prompts

This commit is contained in:
Tarun 2025-10-12 23:40:46 +05:30
parent 807f4055f9
commit 120d60465e
4 changed files with 18 additions and 31 deletions

View file

@ -1,10 +1,11 @@
import datetime import datetime
def _build_language_instruction(language: str | None = None):
def get_answer_outline_system_prompt(language: str | None = None) -> str:
language_instruction = ""
if language: 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""" return f"""
Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")}

View file

@ -1,5 +1,5 @@
import datetime import datetime
from ..prompts import _build_language_instruction
def get_qna_citation_system_prompt(chat_history: str | None = None, language: str | None = None): def get_qna_citation_system_prompt(chat_history: str | None = None, language: str | None = None):
chat_history_section = ( chat_history_section = (
@ -17,10 +17,7 @@ NO CHAT HISTORY PROVIDED
) )
# Add language instruction if specified # Add language instruction if specified
language_instruction = "" language_instruction = _build_language_instruction(language)
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""" return f"""
Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} 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} 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 # Add language instruction if specified
language_instruction = "" language_instruction = _build_language_instruction(language)
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""" return f"""
Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")}

View file

@ -1,6 +1,5 @@
import datetime import datetime
from ..prompts import _build_language_instruction
def get_citation_system_prompt(chat_history: str | None = None, language: str | None = None): def get_citation_system_prompt(chat_history: str | None = None, language: str | None = None):
chat_history_section = ( chat_history_section = (
f""" f"""
@ -17,9 +16,7 @@ NO CHAT HISTORY PROVIDED
) )
# Add language instruction if specified # Add language instruction if specified
language_instruction = "" language_instruction = _build_language_instruction(language)
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""" return f"""
Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")}
@ -177,9 +174,7 @@ NO CHAT HISTORY PROVIDED
) )
# Add language instruction if specified # Add language instruction if specified
language_instruction = "" language_instruction = _build_language_instruction(language)
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""" return f"""
Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")} Today's date: {datetime.datetime.now().strftime("%Y-%m-%d")}

View file

@ -75,33 +75,29 @@ async def handle_chat_data(
) )
) )
user_preference = language_result.scalars().first() user_preference = language_result.scalars().first()
print("UserSearchSpacePreference:", user_preference) # print("UserSearchSpacePreference:", user_preference)
language = None language = None
if user_preference and user_preference.search_space and user_preference.search_space.llm_configs: if user_preference and user_preference.search_space and user_preference.search_space.llm_configs:
llm_configs = 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]: 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): if preferred_llm and getattr(preferred_llm, 'language', None):
language = preferred_llm.language language = preferred_llm.language
# print(f"Using language from preferred LLM: {preferred_llm.name} -> {language}")
break break
# no preferred llM has language use first available LLM config
if not language: if not language:
first_llm_config = llm_configs[0] first_llm_config = llm_configs[0]
language = getattr(first_llm_config, 'language', None) language = getattr(first_llm_config, 'language', None)
# print(f"Using language from first LLM config: {first_llm_config.name} -> {language}")
except HTTPException: except HTTPException:
raise HTTPException( raise HTTPException(
status_code=403, detail="You don't have access to this search space" status_code=403, detail="You don't have access to this search space"
) from None ) from None
# print("Language selected:", language)
langchain_chat_history = [] langchain_chat_history = []
for message in messages[:-1]: for message in messages[:-1]:
if message["role"] == "user": if message["role"] == "user":