| title | Agentic Loan Officer |
|---|---|
| emoji | 💰 |
| colorFrom | blue |
| colorTo | purple |
| sdk | gradio |
| sdk_version | 4.19.2 |
| app_file | app.py |
| pinned | false |
| license | mit |
A multi-agent AI system for processing loan applications in Nigeria, featuring natural language processing, risk assessment, recommendation generation, and email communication. Built with Python, Gradio, and various AI and messaging APIs, it provides an interactive interface for users to apply for loans.
- Overview
- Agent Roles & Workflow
- Features
- Technical Architecture
- Setup Guide
- Running the Application
- Usage Guide
- Testing
- Troubleshooting
- Contributing
- License
The Agentic Loan Officer automates loan application processing for Nigerian users through a multi-agent system. Agents handle tasks from data collection to sending loan decisions via email. The system uses OpenAI, Anthropic, and Google Gemini for AI tasks, a fine-tuned OpenAI model for risk scoring, and SendGrid for email notifications. Users interact via a Gradio-based web interface, providing loan details in natural language.
The system comprises four agents:
| Agent | Responsibility | Handoff |
|---|---|---|
| Coordinator (Agent 1) | Engages users via chat, detects loan application intent, collects and validates fields (age, gender, marital_status, location, amount, tenure). |
Passes validated data to Agent 2. |
| Risk Scorer (Agent 2) | Uses a fine-tuned OpenAI model to compute a repayment probability score (0-99) and risk level (high, medium, acceptable). | Returns results to Agent 1. |
| Analyst (Agent 3) | Analyzes Agent 2’s output, providing a detailed recommendation (approve, adjust, decline). | Sends recommendation to Agent 1 for confirmation. |
| Notifier (Agent 4) | Upon confirmation, sends a professional email with the loan decision using SendGrid. | Completes the workflow. |
sequenceDiagram
participant C as Customer
participant A1 as Coordinator
participant A2 as Risk Scorer
participant A3 as Analyst
participant A4 as Notifier
C->>A1: Chat input (e.g., "I'm 30, male, single, in Lagos, need 50000 for 60 days")
A1-->>C: Request missing fields or confirm data
A1->>A2: Validated loan payload
A2-->>A1: Repayment probability score + risk level
A1->>A3: Score + application data
A3-->>A1: Detailed recommendation
A1-->>C: Present summary for confirmation
C-->>A1: Confirm or modify
A1->>A4: Send final decision
A4-->>C: Email confirmation
{
"age": 30,
"gender": "male",
"marital_status": "single",
"location": "Lagos",
"amount": 50000,
"tenure": 60
}- Natural Language Processing: Extracts loan details using spaCy, regex, and word2number.
- Data Validation: Ensures fields meet requirements (e.g., age 19-70, amount ≤ ₦1,000,000).
- Risk Assessment: Generates repayment probability scores via a fine-tuned OpenAI model.
- Loan Recommendation: Provides detailed approval/rejection recommendations.
- Email Notifications: Sends HTML emails via SendGrid.
- Interactive UI: Gradio-based web interface.
- Modular Design: Extensible agent-based architecture.
- Language: Python 3.12+
- Framework: Gradio for the web interface
- AI Models:
- OpenAI
gpt-4o-minifor coordination and NLP - Fine-tuned OpenAI model for risk scoring
- Anthropic, Llama-3.3-70b-versatile and Google Gemini for additional AI tasks
- OpenAI
- Libraries:
openai,anthropic,google-genai: AI model interactionssendgrid: Email deliveryspacy,word2number: NLPgradio: UIplaywright,requests,httpx: Web interactions
- Tools:
uv: Dependency and environment managementpytest: Unit testing
- APIs:
- OpenAI, Anthropic, Google Gemini, SendGrid
-
Clone the Repository:
git clone https://github.com/seunope/agentic-loan-officer.git cd agentic-loan-officer -
Install
uv:pip install uv
-
Create a Virtual Environment:
uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install Dependencies: Install project and development dependencies:
uv pip install . --group dev -
Configure Environment Variables: Create a
.envfile in the project root:OPENAI_API_KEY=your_openai_key FINE_TUNED_MODEL=your_fine_tuned_model_id GOOGLE_API_KEY=your_google_key GROQ_API_KEY=your_groq_key SENDGRID_API_KEY=your_sendgrid_key ANTHROPIC_API_KEY=your_anthropic_keyObtain keys from:
-
Run with
uv:uv run python -m src.main
-
Access the Interface:
- Open the Gradio URL (e.g.,
http://127.0.0.1:7860) in a browser.
- Open the Gradio URL (e.g.,
-
Alternative Run Command: If running directly:
uv run python src/main.py
Note: May require
sys.path.appendinsrc/main.pyfor relative imports (see troubleshooting).
-
Start a Loan Application:
- Enter details in the Gradio interface: "I'm 30 years old, male, single, living in Lagos, need a loan of 50,000 for 60 days."
- The Coordinator Agent prompts for missing or invalid data.
-
Review and Confirm:
- Review the summary of collected fields.
- Confirm with "yes" or "proceed," or modify with "change" or "edit."
-
Receive Decision:
- After confirmation, the system processes the application and sends a decision email.
- Provide a valid email address when prompted.
-
Start a New Application:
- Click "Start New Application" to reset.
Run unit tests from the project root:
uv run pytest tests/ -vThe -v flag provides verbose output. Tests cover:
- Coordinator data collection
- Risk scorer instructions
- Recommendation logic
- Emailer initialization
- Data validation
- NLP extraction
Note: Ensure the project is installed (uv pip install .) to make the src module available for test imports.
- ModuleNotFoundError: No module named 'src':
- Run tests from the project root:
uv run pytest tests/. - Ensure the project is installed:
uv pip install .. - Add
pytest.iniwithpython_paths = .(see Setup Guide). - Set
PYTHONPATH:export PYTHONPATH=$PYTHONPATH:/path/to/agentic-loan-officer.
- Run tests from the project root:
- Dependency Group Errors:
- Use
uv pip install . --group devinstead ofuv pip install ".[dev]".
- Use
- Zsh Globbing Errors:
- For commands like
uv pip install ".[dev]", use quotes or escape:uv pip install .\[dev\].
- For commands like
- Relative Import Errors:
- Use
uv run python -m src.main. - Or add
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))tosrc/main.py.
- Use
- API Errors:
- Verify API keys in
.env. - Check rate limits or permissions.
- Verify API keys in
- Port Conflicts:
- Change port:
uv run python -m src.main --server_port 5000.
- Change port:
- Dependency Issues:
- Re-sync:
uv pip install . --group dev. - Ensure Python ≥3.12:
uv python 3.12.
- Re-sync:
- NLP Extraction Failures:
- Confirm spaCy model:
uv run python -m spacy download en_core_web_sm.
- Confirm spaCy model:
- Fork the repository.
- Create a feature branch:
git checkout -b feature/your-feature. - Commit changes:
git commit -m "Add your feature". - Push:
git push origin feature/your-feature. - Open a pull request.
Include tests and follow PEP 8 guidelines.
MIT License. See LICENSE file.