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
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")}

View file

@ -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")}

View file

@ -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")}

View file

@ -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":