Skip to content

AIResearchForge/AI-LEGAL-ADVISOR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧠 AI Legal Advisor — Legal Assistant (RAG + LangGraph)

An AI agent that analyzes legal documents (contracts, terms and conditions, statutes) and answers user questions, highlighting risks and recommendations.

The system is built using **LangGraph**, which provides a robust, stateful workflow for multi-agent orchestration. Unlike a simple linear chain, LangGraph allows for complex flows with conditional edges, persistent state, and checkpointing, making it ideal for legal analysis where context and reasoning steps must be preserved.

🎯 Idea

The system guides the user through a structured legal analysis process:

1. Query Agent – parses the user's question and extracts key legal concepts.
2. RAG Agent – searches the vector database for relevant legal document fragments.
3. Analysis Agent – analyzes the retrieved fragments in the context of the question.
4. Risk Agent – identifies potential legal risks and unfavorable clauses.
5. Report Agent – generates a comprehensive legal report with recommendations.

📄 Document Database — UK Consumer Rights Act 2015

The system is pre-configured to work with the 'UK Consumer Rights Act 2015', a comprehensive piece of legislation that governs consumer rights in the United Kingdom. It covers:

- Rights regarding faulty goods
- Digital content and services
- Unfair contract terms
- Remedies (repair, replacement, refund)
- Exclusions of liability
- Unfair commercial practices

The full text of the Act has been indexed in the RAG vector database, allowing the system to answer detailed questions about consumer rights, liability exclusions, and compliance obligations.

🛠️ Technologies

- LangChain 1.2.18 – framework for building AI agents
- LangGraph 1.2.6 – stateful graph-based workflow orchestration
- ChromaDB – vector database for RAG (Retrieval-Augmented Generation)
- OpenAI API – GPT models and embeddings
- PyPDF – PDF parsing and loading

📦 Installation

1. Clone
git clone https://github.com/AIResearchForge/AI-LEGAL-ADVISOR.git
cd AI-LEGAL-ADVISOR

2. Virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate   # Windows

3. Install dependencies
pip install langchain==1.2.18
pip install langchain-community==0.3.0
pip install langchain-openai==0.2.0
pip install langgraph==1.2.6
pip install langchain-chroma
pip install pypdf
pip install tavily-python python-dotenv pydantic requests beautifulsoup4 openai

4. Configuration
Edit the .env file and enter your API keys

5. Run
python -m src.main

💬 Example Usage:

❓ Ask a legal question: Can a business exclude liability for breach of contract under UK consumer law?

============================================================
📋 LEGAL REPORT:
============================================================

LEGAL REPORT

1. Executive Summary
Under UK consumer law, particularly the Consumer Rights Act 2015, businesses cannot exclude liability for breach of contract if such exclusions are deemed unfair. This law protects consumers by ensuring that any attempts to limit liability, especially concerning death or personal injury due to negligence, are invalid. Businesses must carefully assess their contract terms to avoid legal challenges and ensure compliance with consumer protection regulations.

2. Detailed Analysis
The Consumer Rights Act 2015 establishes a framework for fair trading and consumer protection in the UK. Key points include:

1. Prohibition of Unfair Terms: The Act explicitly prohibits unfair terms in contracts between businesses and consumers. Any term that creates a significant imbalance between the parties to the detriment of the consumer may be considered unfair and, therefore, unenforceable.

2. Liability for Death or Personal Injury: Any exclusion or limitation of liability for death or personal injury resulting from negligence is automatically invalid. This means that businesses cannot contract out of their responsibility for harm caused by their negligence.

3. Assessment of Fairness: The fairness of contract terms is evaluated based on the circumstances at the time the contract was made. Factors such as the clarity of the terms, the bargaining power of the parties, and the nature of the goods or services provided are considered.

Given these provisions, businesses must be cautious when drafting contracts to ensure that they do not include unfair terms that could expose them to liability.

3. Identified Risks
- Legal Challenges: Businesses may face litigation if they attempt to exclude liability for breach of contract, as such clauses may be deemed unfair under the Consumer Rights Act 2015.
- Invalidation of Clauses: Any attempt to limit liability for death or personal injury due to negligence could lead to the invalidation of those clauses, resulting in significant financial exposure.
- Unpredictable Outcomes: The assessment of fairness in contract terms can lead to unpredictable outcomes in disputes, increasing the risk of litigation and potential reputational damage.

4. Recommendations
- Review Contract Terms: Conduct a thorough review of all contract terms to ensure compliance with the Consumer Rights Act 2015 and eliminate any potentially unfair clauses.
- Training for Staff: Implement training programs for staff involved in contract negotiations to enhance their understanding of consumer law and the implications of liability exclusions.
- Legal Consultation: Engage legal counsel to assist in drafting and reviewing contracts to ensure that all terms are fair and compliant with current legislation.

5. Additional Notes
It is essential for businesses to stay updated on changes in consumer law and to regularly review their contractual practices. Engaging with consumer advocacy groups may also provide insights into best practices for compliance and consumer relations.

FINAL CONCLUSION:
Businesses in the UK must recognize that they cannot freely exclude liability for breach of contract under consumer law, especially in cases involving negligence. To mitigate risks, it is crucial to review and revise contract terms, provide staff training on consumer law, and seek legal advice to ensure compliance with the Consumer Rights Act 2015.
============================================================

📂 Customizing the RAG System:

The system is fully customizable and allows you to upload your own legal documents:
Add your own documents – place your legal documents (.txt or .pdf) in the data/documents/ directory.
Supported formats: TXT and PDF files.
Automatic indexing: When you run the system, all documents in this directory will be automatically loaded, split into chunks, and indexed in the ChromaDB vector database.
Multiple documents: You can add as many documents as you need – the RAG system will search across all of them.
Important: After adding new documents, you can either delete the chroma_db/ folder to re-index everything, or the system will add new documents to the existing database.

🧠 How RAG Works:

Document Ingestion: Documents are loaded from data/documents/, split into chunks (1000 characters with 200 overlap), and embedded using text-embedding-3-small.
Vector Storage: Embeddings are stored in ChromaDB (chroma_db/ folder).
Query Processing: When a user asks a question, the Query Agent extracts keywords, which are then used to retrieve the most relevant document chunks from ChromaDB.
Analysis & Reporting: Retrieved chunks are passed through the Analysis, Risk, and Report agents to generate a comprehensive legal report.

PROJECT STRUCTURE:

AI-LEGAL-ADVISOR/
├── README.md
├── .env
├── .gitignore
├── config/
│   └── openai_config.py         
├── src/
│   ├── __init__.py               
│   ├── graph.py                  
│   ├── prompts.py               
│   ├── tools.py                  
│   ├── memory.py                 
│   ├── rag.py                   
│   └── main.py                   
├── data/
│   └── documents/                <- 📂 YOUR LEGAL DOCUMENTS GO HERE
│       ├── consumer_rights_act_2015.pdf
│       ├── your_contract.pdf
│       └── your_terms.txt
└── examples/
    └── example_questions.txt 

About

AI-powered legal assistant that analyzes legal documents using RAG, LangGraph, and ChromaDB to identify risks, answer legal questions, and generate explainable reports.

Topics

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages