This repository is at https://github.com/dealfluence/LTTHackathon2025 . See https://lumenai.london/ for the demo company page.
AI-powered legal contract analysis with risk assessment, conversational Q&A, and plain-English summaries. This project provides an intelligent assistant to help legal professionals and business stakeholders quickly understand complex contracts.
- π Conversational Q&A: Ask questions about the contract in natural language.
β οΈ Intelligent Escalation: Automatically detects questions requiring human lawyer review and prepares a concise briefing.- π Plain-English Summaries: Generates business-friendly contract overviews.
- π Web Interface: Easy-to-use browser-based UI for analysis and chat.
- πΎ Local Storage: Securely persists analysis results and conversations locally.
Before you begin, ensure you have the following installed on your system:
Follow these steps to get the application running on your local machine.
Open your terminal and clone the project repository:
git clone <repository-url>
cd legal-contract-botThe project includes a setup script to create necessary directories and configuration files.
python setup.pyThis will create data directories, a .gitignore file, and a .env file for your API keys.
The setup script creates a file named .env in the project's root directory. You must edit this file to add your API key. Open the .env file and replace your_google_api_key_here with your actual Google AI API key.
Install the required Python packages using the provided requirements file:
pip install -r requirements/web.txtOnce the setup is complete and dependencies are installed, you can run the web application.
From the root directory of the project, run:
python run_web.pyYou should see output indicating the server has started, typically on http://localhost:8000.
Open your web browser and navigate to http://localhost:8000.
This LangGraph implementation creates a sophisticated conversational AI system that can handle legal queries, intelligently route complex questions to human lawyers, and enhance responses with contextual information from legal documents.
The state maintains the following information throughout the conversation flow:
- conversation_history: List of all messages (HumanMessage/AIMessage objects)
- user_message: Current user query (when user initiates)
- lawyer_message: Lawyer's response (when lawyer responds)
- response_to_user: Final response to send to the user
- message_to_lawyer: Briefing sent to lawyer for escalated queries
- escalated_question: Original question that was escalated (provides context)
- prepared_briefing: The briefing prepared for the lawyer
- websocket: WebSocket connection for real-time status updates
- base_response: Response before contextual enhancement
- Purpose: Analyzes incoming user queries to determine if they can be answered by AI or need lawyer escalation
- Decision Logic: Uses structured output (Pydantic model) to make binary decision
- Status Updates: Sends "escalation" or "direct_response" via WebSocket
- Output: Sets
decisionfield to either "answer_directly" or "escalate_to_lawyer"
- Purpose: Creates concise, factual responses using contract information
- Style: 1-2 sentences, direct facts, includes clause references
- Example Output: "Yes, you can terminate with 30-day written notice (Section 5.2)."
- Updates: Adds user message and AI response to conversation history
- Purpose: Prepares concise briefing for lawyer review when escalation is needed
- Format:
- User asks: [brief restatement]
- Contract says: [key fact + clause]
- Proposed answer: [1 sentence]
- Any concerns?
- User Response: "Checking with legal counsel on this one."
- Stores: Briefing, escalated question, and prepared response for later use
- Purpose: Processes lawyer's feedback on escalated queries
- Two Modes:
- Approval Mode: If lawyer approves (keywords: "approve", "yes", etc.), extracts proposed answer from briefing
- Correction Mode: If lawyer provides corrections, reformats their guidance naturally
- Output: Sets base_response for contextual enhancement
- Purpose: Analyzes base responses and potentially adds important related details
- Enhancement Rules:
- Only adds genuinely important, actionable information
- Focuses on deadlines, penalties, or connected obligations
- Keeps additions to one short sentence maximum
- Returns "NO_ENHANCEMENT_NEEDED" if nothing relevant
- Error Handling: Falls back to base response if enhancement fails
- Status Update: Sends "contextual_analysis" via WebSocket
if state.get("lawyer_message"):
return "handle_lawyer_response"
return "router"- Determines whether the conversation is initiated by a user or continuing from a lawyer's response
return "generate_briefing" if state.get("decision") == "escalate_to_lawyer" else "answer"- Routes based on the router node's decision
- User message β Router β Direct Answer β Contextual Enhancement β End
- Used for straightforward queries that can be answered from contract information
- User message β Router β Generate Briefing β End
- Sends briefing to lawyer and tells user "Checking with legal counsel"
- Conversation pauses for lawyer input
- Lawyer message β Handle Lawyer Response β Contextual Enhancement β End
- Processes lawyer's feedback and delivers final answer to user
-
Contextual Enhancement for All Responses: Both direct answers and lawyer-approved responses go through enhancement to ensure users get comprehensive information
-
Briefing Bypass: Briefings sent to lawyers skip contextual enhancement as they're already comprehensive
-
State Cleanup: After contextual enhancement, temporary state fields (base_response, escalated_question, prepared_briefing) are cleared
-
WebSocket Integration: Real-time status updates keep users informed about processing stages
-
Error Resilience: Each node has error handling to gracefully fall back to safe responses
The graph requires:
- LLM: ChatGoogleGenerativeAI instance for all language processing
- doc_context: Contract/document content for answering queries
- escalation_rules: Rules defining when to escalate to human lawyers
This architecture creates a robust, user-friendly legal assistant that knows its limitations and seamlessly involves human expertise when needed.