Nischal Subedi
added disclaimer and enchanced UI
bfee845
raw
history blame
28.6 kB
import os
import json
import logging
from typing import Dict, List, Optional
from functools import lru_cache
import re
import gradio as gr
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Make sure vector_db.py is in the same directory or accessible via PYTHONPATH
from vector_db import VectorDatabase # <-- This now imports the placeholder
# Enhanced logging for better debugging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
)
class RAGSystem:
def __init__(self, vector_db: Optional[VectorDatabase] = None):
logging.info("Initializing RAGSystem")
# If no vector_db instance is passed, create one (uses placeholder for now)
self.vector_db = vector_db if vector_db else VectorDatabase()
self.llm = None
self.chain = None
# Using f-string for potentially better readability/maintenance if template gets complex
self.prompt_template_str = """You are a legal assistant specializing in tenant rights and landlord-tenant laws. Your goal is to provide accurate, detailed, and helpful answers grounded in legal authority. Use the provided statutes as the primary source when available. If no relevant statutes are found in the context, rely on your general knowledge to provide a pertinent and practical response, clearly indicating when you are doing so and prioritizing state-specific information over federal laws for state-specific queries.
Instructions:
* Use the context and statutes as the primary basis for your answer when available.
* For state-specific queries, prioritize statutes or legal principles from the specified state over federal laws.
* Cite relevant statutes (e.g., (AS § 34.03.220(a)(2))) explicitly in your answer when applicable.
* If multiple statutes apply, list all relevant ones.
* If no specific statute is found in the context, state this clearly (e.g., 'No specific statute was found in the provided context'), then provide a general answer based on common legal principles or practices, marked as such.
* Include practical examples or scenarios to enhance clarity and usefulness.
* Use bullet points or numbered lists for readability when appropriate.
* Maintain a professional and neutral tone.
Question: {query}
State: {state}
Statutes from context:
{statutes}
Context information:
--- START CONTEXT ---
{context}
--- END CONTEXT ---
Answer:"""
self.prompt_template = PromptTemplate(
input_variables=["query", "context", "state", "statutes"],
template=self.prompt_template_str
)
logging.info("RAGSystem initialized.")
def initialize_llm(self, openai_api_key: str):
"""Initializes the LLM and the processing chain."""
if not openai_api_key:
logging.error("Attempted to initialize LLM without API key.")
raise ValueError("OpenAI API key is required.")
# Avoid re-initializing if already done with the same key implicitly
# Note: This simple check doesn't handle key changes well.
# A more robust approach might involve checking if self.llm's key matches.
if self.llm and self.chain:
# Check if the key is the same (conceptually - can't directly read from ChatOpenAI instance easily)
# If key changes are expected, need a more complex re-initialization logic.
logging.info("LLM and Chain already initialized.")
return
try:
logging.info("Initializing OpenAI LLM...")
self.llm = ChatOpenAI(
temperature=0.2,
openai_api_key=openai_api_key,
model_name="gpt-3.5-turbo",
max_tokens=1500, # Max response tokens
request_timeout=45 # Increased timeout
)
logging.info("OpenAI LLM initialized successfully.")
self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
logging.info("LLMChain created successfully.")
except Exception as e:
logging.error(f"Failed to initialize OpenAI LLM or Chain: {str(e)}")
# Reset llm/chain if initialization failed partially
self.llm = None
self.chain = None
raise # Re-raise the exception to be caught by the caller
def extract_statutes(self, text: str) -> str:
"""
Extract statute citations from the given text using a refined regex pattern.
Returns a string of valid statutes, one per line, or a message if none are found.
"""
# Refined Regex: Aims to capture common US statute formats. May need tuning.
# - Allows state abbreviations (e.g., CA, NY) or full names (e.g, California)
# - Looks for common terms like Code, Laws, Statutes, CCP, USC, ILCS
# - Requires section symbol (§) followed by numbers/hyphens/parenthesized parts
# - Tries to avoid matching simple parenthetical remarks like '(a)' or '(found here)'
statute_pattern = r'\b(?:[A-Z]{2,}\.?\s+(?:Rev\.\s+)?Stat\.?|Code(?:\s+Ann\.?)?|Ann\.?\s+Laws|Statutes|CCP|USC|ILCS|Civ\.\s+Code|Penal\s+Code|Gen\.\s+Oblig\.\s+Law)\s+§\s*[\d\-]+(?:\.\d+)?(?:\([\w\.]+\))?|Title\s+\d+\s+USC\s+§\s*\d+(?:-\d+)?\b'
# Use finditer for more control if needed later, findall is simpler for now
statutes = re.findall(statute_pattern, text, re.IGNORECASE)
valid_statutes = []
# Basic filtering (can be improved)
for statute in statutes:
# Remove potential leading/trailing spaces and ensure it looks like a statute
statute = statute.strip()
if '§' in statute and any(char.isdigit() for char in statute):
# Avoid things that might just be section references like '(a)' or URLs mistakenly caught
if not re.match(r'^\([\w\.]+\)$', statute) and 'http' not in statute:
valid_statutes.append(statute)
if valid_statutes:
# Deduplicate while preserving order
seen = set()
unique_statutes = [s for s in valid_statutes if not (s in seen or seen.add(s))]
logging.info(f"Extracted {len(unique_statutes)} unique statutes.")
return "\n".join(f"- {s}" for s in unique_statutes) # Format as list
logging.info("No statutes found matching the pattern in the context.")
return "No specific statutes found in the provided context."
# Cache results for the same query, state, and key (key isn't explicitly cached but influences init)
@lru_cache(maxsize=100)
def process_query(self, query: str, state: str, openai_api_key: str, n_results: int = 5) -> Dict[str, any]:
"""Processes the user query using RAG."""
logging.info(f"Processing query: '{query}' for state: '{state}' with n_results={n_results}")
# 1. Input Validation
if not state:
logging.warning("No state provided for query.")
return {
"answer": "**Error:** Please select a state to proceed with your query.",
"context_used": "N/A"
}
if not query:
logging.warning("No query provided.")
return {
"answer": "**Error:** Please enter your question in the Query box.",
"context_used": "N/A"
}
if not openai_api_key:
logging.warning("No OpenAI API key provided.")
return {
"answer": "**Error:** Please provide an OpenAI API key to proceed.",
"context_used": "N/A"
}
# 2. Initialize LLM (if needed)
try:
# Initialize LLM here, ensuring it's ready before DB query or LLM call
self.initialize_llm(openai_api_key)
except Exception as e:
logging.error(f"LLM Initialization failed: {str(e)}")
return {
"answer": f"**Error:** Failed to initialize the AI model. Please check your API key and network connection. ({str(e)})",
"context_used": "N/A"
}
# Ensure chain is initialized after initialize_llm() call succeeds
if not self.chain:
logging.error("LLM Chain is not initialized after attempting LLM initialization.")
return {
"answer": "**Error:** Internal system error. Failed to prepare the processing chain.",
"context_used": "N/A"
}
# 3. Query Vector Database
context = "No relevant context found." # Default context
try:
results = self.vector_db.query(query, state=state, n_results=n_results)
logging.info(f"Vector database query successful for state '{state}'.")
# logging.debug(f"Raw query results: {json.dumps(results, indent=2)}")
context_parts = []
# Process document results carefully, checking list structure
doc_results = results.get("document_results", {})
docs = doc_results.get("documents", [[]])[0] # Safely access first list
metadatas = doc_results.get("metadatas", [[]])[0] # Safely access first list
if docs and metadatas and len(docs) == len(metadatas):
for i, doc_content in enumerate(docs):
metadata = metadatas[i]
state_label = metadata.get('state', 'Unknown State')
chunk_id = metadata.get('chunk_id', 'N/A')
context_parts.append(f"[{state_label} - Chunk {chunk_id}] {doc_content}")
else:
logging.warning("No document results or mismatch in docs/metadata lengths.")
# Process state summary results
state_results_data = results.get("state_results", {})
state_docs = state_results_data.get("documents", [[]])[0]
state_metadatas = state_results_data.get("metadatas", [[]])[0]
if state_docs and state_metadatas and len(state_docs) == len(state_metadatas):
for i, state_doc_content in enumerate(state_docs):
metadata = state_metadatas[i]
state_label = metadata.get('state', state) # Use provided state if not in metadata
context_parts.append(f"[{state_label} - Summary] {state_doc_content}")
else:
logging.warning("No state summary results found.")
if context_parts:
context = "\n\n---\n\n".join(context_parts)
logging.info(f"Constructed context with {len(context_parts)} parts.")
# Limit context length if necessary (though max_tokens helps)
# max_context_len = 5000 # Example limit
# if len(context) > max_context_len:
# context = context[:max_context_len] + "\n... [Context Truncated]"
# logging.warning("Context truncated due to length.")
else:
logging.warning("No relevant context parts found after processing DB results.")
context = "No relevant context could be retrieved from the available documents for your query and selected state."
except Exception as e:
logging.error(f"Vector database query or context processing failed: {str(e)}", exc_info=True)
# Fallback to general knowledge if DB fails, but inform the user
context = f"An error occurred while retrieving specific legal documents ({str(e)}). I will attempt to answer based on general knowledge, but it may lack state-specific details."
statutes_from_context = "Statute retrieval skipped due to context error."
# 4. Extract Statutes from Retrieved Context (if context retrieval succeeded)
statutes_from_context = "No specific statutes found in the provided context."
if "An error occurred while retrieving" not in context and "No relevant context found" not in context:
try:
statutes_from_context = self.extract_statutes(context)
logging.info(f"Statutes extracted: {statutes_from_context}")
except Exception as e:
logging.error(f"Error extracting statutes: {e}")
statutes_from_context = "Error occurred during statute extraction."
# 5. Generate Answer using LLM
try:
logging.info("Invoking LLMChain...")
llm_input = {
"query": query,
"context": context,
"state": state,
"statutes": statutes_from_context
}
# logging.debug(f"Input to LLMChain: {json.dumps(llm_input, indent=2)}") # Be careful logging sensitive data
answer_dict = self.chain.invoke(llm_input)
answer_text = answer_dict.get('text', '').strip()
if not answer_text:
logging.warning("LLM returned an empty answer.")
answer_text = "I received an empty response from the AI model. This might be a temporary issue. Please try rephrasing your question or try again later."
logging.info("LLM generated answer successfully.")
# logging.debug(f"Raw answer text from LLM: {answer_text}")
return {
"answer": answer_text,
"context_used": context # Return the context for potential display or debugging
}
except Exception as e:
logging.error(f"LLM processing failed: {str(e)}", exc_info=True)
# Provide a more informative error message
error_message = f"**Error:** An error occurred while generating the answer. This could be due to issues with the AI model connection, API key limits, or the complexity of the request. Please try again later.\n\nDetails: {str(e)}"
# Check for common API errors
if "authentication" in str(e).lower():
error_message = "**Error:** Authentication failed. Please check if your OpenAI API key is correct and active."
elif "rate limit" in str(e).lower():
error_message = "**Error:** You've exceeded your OpenAI API usage limit. Please check your plan or try again later."
return {
"answer": error_message,
"context_used": context # Still return context for debugging
}
def get_states(self) -> List[str]:
"""Retrieves the list of available states from the VectorDatabase."""
try:
states = self.vector_db.get_states()
if not states:
logging.warning("No states returned from vector_db.get_states(). Using default list.")
# Provide a fallback list if needed
return ["California", "New York", "Texas", "Florida", "Oregon", "Alabama", "Select State..."]
logging.info(f"Retrieved {len(states)} states from VectorDatabase.")
return sorted(list(set(states))) # Ensure uniqueness and sort
except Exception as e:
logging.error(f"Failed to get states from VectorDatabase: {str(e)}")
return ["Error fetching states"] # Indicate error in the dropdown
def load_pdf(self, pdf_path: str) -> int:
"""Loads and processes the PDF using the VectorDatabase."""
if not os.path.exists(pdf_path):
logging.error(f"PDF file not found at path: {pdf_path}")
raise FileNotFoundError(f"PDF file not found: {pdf_path}")
try:
logging.info(f"Attempting to load PDF: {pdf_path}")
num_states = self.vector_db.process_and_load_pdf(pdf_path)
if num_states > 0:
logging.info(f"Successfully processed PDF. Found data for {num_states} states.")
else:
logging.warning(f"Processed PDF, but found no state-specific data according to vector_db implementation.")
return num_states
except Exception as e:
logging.error(f"Failed to load or process PDF '{pdf_path}': {str(e)}", exc_info=True)
# Depending on vector_db, this might leave the DB in a partial state.
return 0 # Indicate failure
def gradio_interface(self):
"""Creates and returns the Gradio interface."""
# Define the core function that Gradio will call
def query_interface(api_key: str, query: str, state: str) -> str:
# Clear cache for each new request if desired, or rely on LRU cache parameters
# self.process_query.cache_clear()
logging.info(f"Gradio interface received query: '{query}', state: '{state}'")
result = self.process_query(query=query, state=state, openai_api_key=api_key)
# Format the response clearly using Markdown
answer = result.get("answer", "**Error:** No answer generated.")
# context_used = result.get("context_used", "N/A") # Optional: show context
# Simple formatting for now, can be enhanced
formatted_response = f"### Answer for {state}:\n\n{answer}"
# Optional: Include context for debugging/transparency (can be long)
# formatted_response += f"\n\n---\n<details><summary>Context Used (Debug)</summary>\n\n```\n{context_used}\n```\n</details>"
return formatted_response
# Get states for the dropdown
available_states = self.get_states()
if not available_states or "Error" in available_states[0]:
logging.error("Could not load states for dropdown. Interface might be unusable.")
# Handle case where states couldn't be loaded
available_states = ["Error: Could not load states"]
# Define example queries (only query and state needed for examples UI)
example_queries = [
["What is the rent due date law?", "California"],
["What are the rules for security deposit returns?", "New York"],
["Can a landlord enter without notice?", "Texas"],
["What are the eviction notice requirements?", "Florida"],
["Are there rent control laws?", "Oregon"]
]
# Custom CSS (minor adjustments for clarity if needed, your CSS is quite comprehensive)
custom_css = """
/* Your existing CSS here */
.gr-form { max-width: 900px; margin: 0 auto; padding: 30px; /* ... */ }
.gr-title { font-size: 2.5em; font-weight: 700; color: #1a3c34; /* ... */ }
/* ... rest of your CSS ... */
.output-markdown {
background: #f9fafb; /* Light mode bg */
color: #1f2937; /* Light mode text */
padding: 20px;
border-radius: 12px;
border: 1px solid #e5e7eb;
font-size: 1.05em; /* Slightly larger text */
line-height: 1.7; /* More spacing */
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
margin-top: 20px; /* Add space above output */
}
.output-markdown h3 { /* Style the 'Answer for STATE:' heading */
margin-top: 0;
margin-bottom: 15px;
color: #1a3c34; /* Match title color */
border-bottom: 1px solid #e5e7eb;
padding-bottom: 8px;
}
.output-markdown p { margin-bottom: 1em; }
.output-markdown ul, .output-markdown ol { margin-left: 20px; margin-bottom: 1em; }
.output-markdown li { margin-bottom: 0.5em; }
/* Dark mode adjustments */
@media (prefers-color-scheme: dark) {
.output-markdown {
background: #374151 !important; /* Dark mode bg */
color: #f3f4f6 !important; /* Dark mode text */
border-color: #4b5563 !important;
}
.output-markdown h3 {
color: #f3f4f6; /* Dark title */
border-bottom: 1px solid #4b5563;
}
}
"""
# Build the Gradio Blocks interface
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: # Use a slightly different theme
gr.Markdown(
"""
<div style="text-align: center;">
<img src="https://img.icons8.com/plasticine/100/000000/document.png" alt="Icon" style="vertical-align: middle; height: 50px;">
<h1 class='gr-title' style='display: inline-block; margin-bottom: 0; vertical-align: middle; margin-left: 10px;'>Landlord-Tenant Rights Bot</h1>
</div>
<p class='gr-description'>
Ask questions about tenant rights and landlord-tenant laws based on state-specific legal documents.
Provide your OpenAI API key, select a state, and enter your question.
Get your key from <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.
</p>
"""
)
with gr.Column(elem_classes="gr-form"): # Use elem_classes if defined in CSS, otherwise elem_id
# Input Components defined *within* gr.Blocks()
api_key_input = gr.Textbox(
label="OpenAI API Key",
type="password",
placeholder="Enter your OpenAI API key (e.g., sk-...)",
info="Required to process your query.", # Use info parameter
#elem_classes="input-field" # Use if defined in CSS
)
query_input = gr.Textbox(
label="Your Question",
placeholder="e.g., What are the rules for security deposit returns?",
lines=4, # Increased lines slightly
info="Enter your question about landlord-tenant law here.",
#elem_classes="input-field"
)
state_input = gr.Dropdown(
label="Select State",
choices=available_states,
value=available_states[0] if available_states else None, # Default to first state or None
allow_custom_value=False,
info="Select the state your question applies to.",
#elem_classes="input-field"
)
with gr.Row():
clear_button = gr.Button("Clear Inputs", variant="secondary")
submit_button = gr.Button("Submit Query", variant="primary")
output = gr.Markdown(
label="Answer",
value="Your answer will appear here...", # Initial placeholder text
elem_classes="output-markdown" # Apply custom class for styling
)
gr.Markdown("---") # Separator
gr.Markdown("### Examples")
gr.Examples(
examples=example_queries,
inputs=[query_input, state_input], # Examples only fill these two
# outputs=output, # Output is handled by the main submit button click
# fn=query_interface, # Don't run the function directly on example click
# cache_examples=False, # Don't cache example runs if fn were used
label="Click an example to load it",
examples_per_page=5
)
gr.Markdown(
"""
<div class='footnote' style='margin-top: 30px; padding-top: 15px; border-top: 1px solid #e5e7eb; text-align: center; font-size: 0.9em; color: #6c757d;'>
Developed by Nischal Subedi. Follow on
<a href='https://www.linkedin.com/in/nischal1/' target='_blank' style='color: #007bff; text-decoration: none;'>LinkedIn</a> |
Read insights on <a href='https://datascientistinsights.substack.com/' target='_blank' style='color: #007bff; text-decoration: none;'>Substack</a>.
<br>Disclaimer: This bot provides informational summaries based on AI interpretation and retrieved data. It is not a substitute for professional legal advice.
</div>
"""
, elem_classes="footnote") # Apply footnote class if defined in CSS
# Connect Actions to Functions
submit_button.click(
fn=query_interface,
inputs=[api_key_input, query_input, state_input],
outputs=output,
api_name="submit_query" # Add API name for potential programmatic use
)
# Clear button clears all inputs and the output
clear_button.click(
fn=lambda: ( # Return empty values for each output component
"", # api_key_input
"", # query_input
available_states[0] if available_states else None, # state_input (reset to default)
"Cleared. Ready for new query..." # output
),
inputs=[], # No inputs needed for clear
outputs=[api_key_input, query_input, state_input, output]
)
logging.info("Gradio interface created successfully.")
return demo # Corrected return variable name
# Main execution block
if __name__ == "__main__":
logging.info("Starting application setup...")
try:
# --- Configuration ---
PDF_PATH = os.getenv("PDF_PATH", "data/tenant-landlord.pdf") # Use env var or default
VECTOR_DB_PATH = os.getenv("VECTOR_DB_PATH", "./vector_db_store") # For persistent DBs
# Check if PDF exists
if not os.path.exists(PDF_PATH):
logging.error(f"FATAL: PDF file not found at the specified path: {PDF_PATH}")
logging.error("Please ensure the PDF file exists or set the PDF_PATH environment variable correctly.")
# Exit if the core data file is missing
exit(1) # Or raise an exception
# --- Initialization ---
# Initialize VectorDatabase (using placeholder for now)
# Pass path if your implementation uses it (like ChromaDB)
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)
# Initialize RAGSystem with the database instance
rag = RAGSystem(vector_db=vector_db_instance)
# --- Data Loading ---
# Load the PDF data into the vector database
# This step is crucial and needs the *real* vector_db.py
logging.info(f"Loading data from PDF: {PDF_PATH}")
states_loaded = rag.load_pdf(PDF_PATH)
if states_loaded == 0 and not isinstance(vector_db_instance, VectorDatabase): # Check if using placeholder
logging.warning("PDF loading reported 0 states. Check PDF content and vector_db implementation.")
# Decide if you want to proceed without data or halt
# --- Interface Setup & Launch ---
logging.info("Setting up Gradio interface...")
app_interface = rag.gradio_interface()
logging.info("Launching Gradio app...")
# Launch the Gradio app
# share=True generates a public link (use with caution)
# server_name="0.0.0.0" makes it accessible on the network
app_interface.launch(server_name="0.0.0.0", server_port=7860, share=False)
# For cloud deployments (like Hugging Face Spaces), you might not need server_name/port
except FileNotFoundError as fnf_error:
logging.error(f"Initialization failed: {str(fnf_error)}")
# No need to raise again, already logged. Maybe print a message.
print(f"Error: {str(fnf_error)}. Please check file paths.")
except ImportError as import_error:
logging.error(f"Import error: {str(import_error)}. Please ensure all required libraries (gradio, langchain, openai, etc.) and the 'vector_db.py' file are installed and accessible.")
print(f"Import Error: {str(import_error)}. Check your dependencies.")
except Exception as e:
# Catch any other unexpected errors during setup or launch
logging.error(f"An unexpected error occurred during application startup: {str(e)}", exc_info=True)
print(f"Fatal Error: {str(e)}. Check logs for details.")