A specialized RAG (Retrieval-Augmented Generation) chatbot built with LangChain for analyzing LSTA credit agreements, credit applications, and compliance certificates. This system helps credit analysts quickly access and compare information across multiple financial documents.
- Automatic Document Monitoring: Hourly refresh to detect new documents
- Intelligent Chunking: Preserves document structure, definitions, and covenants
- Cross-Document Analysis: Compares covenant requirements with compliance data
- Source Citations: Every answer includes document name and section references
- Covenant Comparison Tables: Formatted tables for covenant compliance analysis
- Financial Term Highlighting: Automatic highlighting of amounts, dates, and percentages
- Confidence Levels: Indicates certainty of information retrieval
- Expert Domain Knowledge: Specialized in LSTA credit agreement terminology
- Python 3.9 or higher
- OpenAI API key (for GPT models)
- 2GB+ free disk space for vector store
- 4GB+ RAM recommended
-
Clone or download this repository
-
Install dependencies
pip install -r requirements.txt- Set up environment variables
Create a .env file in the project root:
OPENAI_API_KEY=your_openai_api_key_here- Create documents directory
mkdir credit_documents- Add your documents
Place your credit agreements, compliance certificates, and credit applications in the credit_documents folder. Supported formats:
- PDF (
.pdf) - Word Documents (
.docx)
Basic usage:
python credit_agreement_chatbot.pyWith custom configuration:
from credit_agreement_chatbot import CreditAgreementChatbot
chatbot = CreditAgreementChatbot(
documents_directory="./credit_documents",
vector_store_path="./vector_store",
model_name="gpt-4", # or "gpt-3.5-turbo" for faster/cheaper
refresh_interval=3600 # 1 hour in seconds
)
# Query the chatbot
response = chatbot.query("What is the leverage ratio requirement?")
print(response['answer'])Your Question: What is the maximum leverage ratio covenant?
ANSWER:
Based on the **Senior Credit Agreement dated March 15, 2024, Section 6.12(a)**,
the Maximum Total Leverage Ratio covenant requires:
- **Q1-Q2 2024:** ≤ **4.50:1.00**
- **Q3-Q4 2024:** ≤ **4.25:1.00**
- **Q1 2025 onwards:** ≤ **4.00:1.00**
Confidence Level: I can confirm this information directly from the credit agreement.
Your Question: Are we in compliance with the leverage ratio covenant?
ANSWER:
According to the **Compliance Certificate dated June 30, 2024**, your actual
Total Leverage Ratio was **3.85:1.00**.
| Covenant | Requirement | Actual | Status |
|----------|-------------|--------|--------|
| Total Leverage Ratio | ≤ 4.50:1.00 | 3.85:1.00 | ✓ Pass |
You are in compliance with a cushion of **0.65**.
Confidence Level: I can confirm this based on [Credit Agreement, Section 6.12(a)]
and [Compliance Certificate dated 06/30/2024].
Your Question: Compare our debt levels in the compliance certificate with what's
allowed in the credit agreement
ANSWER:
Based on cross-document analysis:
**From Credit Agreement (Section 7.02 - Limitation on Indebtedness)**:
Maximum permitted debt: **$50,000,000** plus permitted baskets
**From Compliance Certificate (dated September 30, 2024)**:
Actual Total Debt: **$42,500,000**
| Debt Type | Limit | Actual | Available Capacity |
|-----------|-------|--------|-------------------|
| Total Debt | $50M | $42.5M | $7.5M |
Status: ✓ IN COMPLIANCE
Confidence Level: I can confirm these figures from [Credit Agreement, Section 7.02,
Page 48] and [Compliance Certificate Q3 2024, Page 3].
-
LSTA Credit Agreements
- Standard article/section structure
- Definitions (Article I)
- Covenants (Articles VI-VII)
- Events of Default (Article VIII)
- Pricing grids and schedules
-
Compliance Certificates
- Covenant calculations
- Financial statement data
- Officer certifications
-
Credit Applications
- Borrower information
- Financial projections
- Use of proceeds
For best results:
- Ensure documents are text-searchable (not scanned images)
- Include complete section numbers in headers
- Keep pricing grids and tables formatted clearly
- Use consistent date formats (MM/DD/YYYY)
See document_preprocessing_guide.md for detailed preprocessing strategies.
┌─────────────────────────────────────────────────────────────┐
│ User Query │
└───────────────────────┬─────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CreditAgreementChatbot │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ 1. Check if hourly refresh needed │ │
│ │ 2. Query classification (optional) │ │
│ │ 3. Retrieve relevant chunks from vector store │ │
│ │ 4. Apply specialized prompt template │ │
│ │ 5. Generate response with LLM │ │
│ │ 6. Format with citations and confidence level │ │
│ └──────────────────────────────────────────────────────┘ │
└───────────────────────┬─────────────────────────────────────┘
│
┌───────────────┼───────────────┐
▼ ▼ ▼
┌──────────────┐ ┌─────────────┐ ┌─────────────┐
│ Vector Store │ │ Embeddings │ │ LLM │
│ (FAISS) │ │ (HF Model) │ │ (GPT-3.5/4)│
└──────────────┘ └─────────────┘ └─────────────┘
│
│ Hourly Refresh
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Document Directory │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ • Credit_Agreement_2024.pdf │ │
│ │ • Compliance_Cert_Q3_2024.pdf │ │
│ │ • Credit_Application.docx │ │
│ │ • ... │ │
│ └──────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Default: sentence-transformers/all-MiniLM-L6-v2 (384 dimensions, fast)
Alternatives:
sentence-transformers/all-mpnet-base-v2(768 dimensions, higher quality)BAAI/bge-large-en-v1.5(1024 dimensions, best quality)
chatbot = CreditAgreementChatbot(
embedding_model="sentence-transformers/all-mpnet-base-v2"
)Default: gpt-3.5-turbo (fast, cost-effective)
Recommended for production: gpt-4 or gpt-4-turbo (higher accuracy)
chatbot = CreditAgreementChatbot(
model_name="gpt-4"
)Default settings are optimized for credit agreements, but can be adjusted:
from langchain.text_splitter import RecursiveCharacterTextSplitter
custom_splitter = RecursiveCharacterTextSplitter(
chunk_size=1500, # Larger chunks for more context
chunk_overlap=250, # More overlap for better continuity
)Default: 3600 seconds (1 hour)
chatbot = CreditAgreementChatbot(
refresh_interval=1800 # 30 minutes
)The system includes pre-built prompts for extracting specific information:
- Covenant Extraction: Financial and negative covenants
- Definition Lookup: Defined terms and financial metrics
- Pricing Information: Interest rates and fee structures
- Events of Default: Default triggers and remedies
- Compliance Verification: Cross-document compliance checks
See specialized_retrieval_prompts.md for detailed prompt templates.
If you've added new documents and don't want to wait for the hourly refresh:
chatbot.manual_refresh()Or via command line:
Your Question: refresh
Every response includes source information:
response = chatbot.query("What is EBITDA?")
# Access the answer
print(response['answer'])
# Access source documents
for source in response['sources']:
print(f"File: {source['filename']}")
print(f"Page: {source['page']}")
print(f"Section: {source['section']}")
print(f"Preview: {source['content_preview']}")Modify the system prompt for specific use cases:
custom_prompt = """You are a specialized credit analyst assistant...
[Your custom instructions here]
"""
# Update the QA chain with custom prompt
# (requires modifying the _initialize_qa_chain method)- Use GPU embeddings if available:
pip install faiss-gpu- Switch to more scalable vector stores:
pip install chromadb
# or
pip install pinecone-client- Implement caching:
pip install redis- Reduce retrieval count:
# In _initialize_qa_chain, modify:
search_kwargs={"k": 3} # Instead of 6- Use smaller embedding model:
embedding_model="sentence-transformers/all-MiniLM-L6-v2"- Use GPT-3.5-turbo instead of GPT-4
Solution: Verify documents are in the correct directory and format (PDF or DOCX)
Solution:
- Reduce chunk size
- Process documents in batches
- Use CPU-based FAISS instead of loading all into memory
Solution:
- Add rate limiting to queries
- Use exponential backoff
- Upgrade OpenAI plan
Solution:
- Review preprocessing with
document_preprocessing_guide.md - Increase chunk overlap
- Use better embedding model
- Add more metadata to chunks
Solution:
- Use multithreading for document loading
- Cache unchanged documents
- Reduce refresh frequency
- API Keys: Never commit
.envfile to version control - Document Access: Ensure proper access controls on document directory
- Sensitive Data: Credit agreements contain confidential financial information
- Compliance: Ensure usage complies with data handling regulations
Current implementation runs locally - suitable for development and small teams.
Deploy as FastAPI service:
# api.py
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
chatbot = CreditAgreementChatbot(...)
class Query(BaseModel):
question: str
@app.post("/query")
async def query_endpoint(query: Query):
response = chatbot.query(query.question)
return responseRun with:
uvicorn api:app --host 0.0.0.0 --port 8000- AWS: Deploy on EC2 or Lambda with EFS for document storage
- Azure: Use Azure Functions with Blob Storage
- GCP: Deploy on Cloud Run with Cloud Storage
Embeddings (one-time per document):
- Local HuggingFace models: Free
LLM Costs (per query):
- GPT-3.5-turbo: ~$0.002 per query
- GPT-4: ~$0.03 per query
Storage:
- Vector store: Negligible (local storage)
- Pinecone (if used): ~$70/month for 100k vectors
Small team (100 queries/day):
- GPT-3.5-turbo: ~$6/month
- GPT-4: ~$90/month
Medium usage (500 queries/day):
- GPT-3.5-turbo: ~$30/month
- GPT-4: ~$450/month
To extend or improve the system:
- Add new document types: Modify
_identify_document_type() - Enhance chunking: Update
CreditAgreementChunkerclass - Add new prompts: Expand
specialized_retrieval_prompts.md - Improve metadata: Extend
extract_metadata()function
- Documentation: See
specialized_retrieval_prompts.mdanddocument_preprocessing_guide.md - LangChain Docs: https://python.langchain.com/docs/
- OpenAI API: https://platform.openai.com/docs/
- LSTA: https://www.lsta.org/
This project is licensed under the MIT License.
For issues or questions:
- Check the troubleshooting section
- Review the preprocessing guide
- Consult LangChain documentation
- Open an issue with detailed error messages and logs
Version: 1.0.0
Last Updated: 2025
Tested With: Python 3.9+, LangChain 0.1.0, OpenAI API