from typing import List, Tuple SYSTEM_PROMPT = """You are a legal assistant specialized in the Bhartiya Nayaya Sanhita (BNS). Your role is to provide accurate legal information based on the BNS document. Always cite specific sections when providing information. Format your responses in a clear, structured manner with proper citations.""" def format_context(relevant_sections: List[Tuple[str, str, float]]) -> str: """ Format relevant sections into context for the model. Args: relevant_sections (List[Tuple[str, str, float]]): List of (section_id, content, score) tuples Returns: str: Formatted context string """ context = "Relevant BNS Sections:\n\n" for section_id, content, score in sorted(relevant_sections, key=lambda x: x[2], reverse=True): # Truncate very long sections for better context content_preview = content[:500] + "..." if len(content) > 500 else content context += f"{section_id} (Relevance: {score:.2f}):\n{content_preview}\n\n" return context def create_prompt(query: str, relevant_sections: List[Tuple[str, str, float]]) -> str: """ Create a prompt for the model combining the query and relevant sections. Args: query (str): User's legal query relevant_sections (List[Tuple[str, str, float]]): Relevant BNS sections Returns: str: Complete prompt for the model """ context = format_context(relevant_sections) prompt = f"""{SYSTEM_PROMPT} Context: {context} User Query: {query} Instructions: 1. Analyze the relevant sections provided above 2. Provide a clear and accurate response 3. Include specific section citations 4. Explain legal terms in simple language 5. If the query is outside the scope of BNS, clearly state that Response:""" return prompt def create_error_message(error_type: str) -> str: """ Create standardized error messages. Args: error_type (str): Type of error (e.g., 'out_of_scope', 'no_sections', 'invalid_query') Returns: str: Formatted error message """ error_messages = { 'out_of_scope': "I apologize, but this query appears to be outside the scope of the Bhartiya Nayaya Sanhita (BNS). The BNS primarily covers criminal law matters. Could you please rephrase your question to focus on criminal law aspects?", 'no_sections': "I couldn't find any relevant sections in the BNS for your query. Could you please rephrase your question or provide more specific details about the legal matter you're interested in?", 'invalid_query': "I'm having trouble understanding your query. Could you please rephrase it more clearly, focusing on specific legal aspects you'd like to know about?", } return error_messages.get(error_type, "An unexpected error occurred. Please try again.")