|
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 |
|
|
|
from vector_db import VectorDatabase |
|
|
|
|
|
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") |
|
|
|
self.vector_db = vector_db if vector_db else VectorDatabase() |
|
self.llm = None |
|
self.chain = None |
|
|
|
|
|
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.") |
|
|
|
|
|
|
|
|
|
if self.llm and self.chain: |
|
|
|
|
|
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, |
|
request_timeout=45 |
|
) |
|
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)}") |
|
|
|
self.llm = None |
|
self.chain = None |
|
raise |
|
|
|
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. |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
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' |
|
|
|
|
|
statutes = re.findall(statute_pattern, text, re.IGNORECASE) |
|
|
|
valid_statutes = [] |
|
|
|
for statute in statutes: |
|
|
|
statute = statute.strip() |
|
if '§' in statute and any(char.isdigit() for char in statute): |
|
|
|
if not re.match(r'^\([\w\.]+\)$', statute) and 'http' not in statute: |
|
valid_statutes.append(statute) |
|
|
|
if valid_statutes: |
|
|
|
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) |
|
|
|
logging.info("No statutes found matching the pattern in the context.") |
|
return "No specific statutes found in the provided context." |
|
|
|
|
|
@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}") |
|
|
|
|
|
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" |
|
} |
|
|
|
|
|
try: |
|
|
|
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" |
|
} |
|
|
|
|
|
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" |
|
} |
|
|
|
|
|
|
|
context = "No relevant context found." |
|
try: |
|
results = self.vector_db.query(query, state=state, n_results=n_results) |
|
logging.info(f"Vector database query successful for state '{state}'.") |
|
|
|
|
|
context_parts = [] |
|
|
|
doc_results = results.get("document_results", {}) |
|
docs = doc_results.get("documents", [[]])[0] |
|
metadatas = doc_results.get("metadatas", [[]])[0] |
|
|
|
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.") |
|
|
|
|
|
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) |
|
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.") |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
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." |
|
|
|
|
|
|
|
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." |
|
|
|
|
|
|
|
try: |
|
logging.info("Invoking LLMChain...") |
|
llm_input = { |
|
"query": query, |
|
"context": context, |
|
"state": state, |
|
"statutes": statutes_from_context |
|
} |
|
|
|
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.") |
|
|
|
|
|
return { |
|
"answer": answer_text, |
|
"context_used": context |
|
} |
|
except Exception as e: |
|
logging.error(f"LLM processing failed: {str(e)}", exc_info=True) |
|
|
|
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)}" |
|
|
|
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 |
|
} |
|
|
|
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.") |
|
|
|
return ["California", "New York", "Texas", "Florida", "Oregon", "Alabama", "Select State..."] |
|
logging.info(f"Retrieved {len(states)} states from VectorDatabase.") |
|
return sorted(list(set(states))) |
|
except Exception as e: |
|
logging.error(f"Failed to get states from VectorDatabase: {str(e)}") |
|
return ["Error fetching states"] |
|
|
|
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) |
|
|
|
return 0 |
|
|
|
|
|
def gradio_interface(self): |
|
"""Creates and returns the Gradio interface.""" |
|
|
|
|
|
def query_interface(api_key: str, query: str, state: str) -> str: |
|
|
|
|
|
logging.info(f"Gradio interface received query: '{query}', state: '{state}'") |
|
result = self.process_query(query=query, state=state, openai_api_key=api_key) |
|
|
|
|
|
answer = result.get("answer", "**Error:** No answer generated.") |
|
|
|
|
|
|
|
formatted_response = f"### Answer for {state}:\n\n{answer}" |
|
|
|
|
|
|
|
|
|
return formatted_response |
|
|
|
|
|
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.") |
|
|
|
available_states = ["Error: Could not load states"] |
|
|
|
|
|
|
|
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 = """ |
|
/* 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; |
|
} |
|
} |
|
""" |
|
|
|
|
|
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: |
|
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"): |
|
|
|
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.", |
|
|
|
) |
|
query_input = gr.Textbox( |
|
label="Your Question", |
|
placeholder="e.g., What are the rules for security deposit returns?", |
|
lines=4, |
|
info="Enter your question about landlord-tenant law here.", |
|
|
|
) |
|
state_input = gr.Dropdown( |
|
label="Select State", |
|
choices=available_states, |
|
value=available_states[0] if available_states else None, |
|
allow_custom_value=False, |
|
info="Select the state your question applies to.", |
|
|
|
) |
|
|
|
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...", |
|
elem_classes="output-markdown" |
|
) |
|
|
|
gr.Markdown("---") |
|
gr.Markdown("### Examples") |
|
gr.Examples( |
|
examples=example_queries, |
|
inputs=[query_input, state_input], |
|
|
|
|
|
|
|
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") |
|
|
|
|
|
submit_button.click( |
|
fn=query_interface, |
|
inputs=[api_key_input, query_input, state_input], |
|
outputs=output, |
|
api_name="submit_query" |
|
) |
|
|
|
clear_button.click( |
|
fn=lambda: ( |
|
"", |
|
"", |
|
available_states[0] if available_states else None, |
|
"Cleared. Ready for new query..." |
|
), |
|
inputs=[], |
|
outputs=[api_key_input, query_input, state_input, output] |
|
) |
|
|
|
logging.info("Gradio interface created successfully.") |
|
return demo |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
logging.info("Starting application setup...") |
|
try: |
|
|
|
PDF_PATH = os.getenv("PDF_PATH", "data/tenant-landlord.pdf") |
|
VECTOR_DB_PATH = os.getenv("VECTOR_DB_PATH", "./vector_db_store") |
|
|
|
|
|
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(1) |
|
|
|
|
|
|
|
|
|
|
|
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH) |
|
|
|
|
|
rag = RAGSystem(vector_db=vector_db_instance) |
|
|
|
|
|
|
|
|
|
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): |
|
logging.warning("PDF loading reported 0 states. Check PDF content and vector_db implementation.") |
|
|
|
|
|
|
|
logging.info("Setting up Gradio interface...") |
|
app_interface = rag.gradio_interface() |
|
|
|
logging.info("Launching Gradio app...") |
|
|
|
|
|
|
|
app_interface.launch(server_name="0.0.0.0", server_port=7860, share=False) |
|
|
|
|
|
except FileNotFoundError as fnf_error: |
|
logging.error(f"Initialization failed: {str(fnf_error)}") |
|
|
|
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: |
|
|
|
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.") |