Production-ready FastAPI service for qualifying inbound leads using structured business signals, unstructured notes, optional LLM refinement, and CRM-ready outputs.
This system receives raw inbound leads from forms, chatbots, CRMs, or automation tools and returns:
- a lead score from 0 to 100
- intent classification (
high,medium,low) - business priority label (
urgent,normal,low) and priority class (p0,p1,p2) - enrichment tags
- recommended next action
- an outreach-ready event payload for downstream automation
It is designed to look like a serious internal product, not a toy demo.
Inbound lead (form / chatbot / CRM)
|
v
FastAPI API Layer
|
+--> Enrichment Service
|
+--> Rules-based Scoring Engine
|
+--> Optional LLM Refinement
|
v
Qualification Service
|
+--> Persist to DB (SQLite by default, Postgres-ready)
|
+--> Outreach Event Builder
v
CRM / automation / nurture workflows
lead_qualification_engine/api/routes: HTTP endpointslead_qualification_engine/services/scoring.py: deterministic lead qualification logiclead_qualification_engine/services/llm.py: optional OpenAI-based refinement layerlead_qualification_engine/services/enrichment.py: metadata enrichment helperslead_qualification_engine/services/qualification.py: orchestration and persistencelead_qualification_engine/models: database tableslead_qualification_engine/schemas: request / response modelstests: API and scoring tests
The scoring engine uses:
- company size
- budget
- revenue
- timeline urgency
- source quality
- free-text buying intent signals
Maps leads into:
high: route to sales immediatelymedium: nurture + discovery outreachlow: low-touch nurture flow
If ENABLE_LLM=true and an API key is configured, the baseline result can be refined with an LLM for more nuanced reasoning.
Adds CRM-friendly metadata such as:
- email domain
- company tier
- source bucket
Generates an event payload that can be sent to:
- n8n
- Zapier
- HubSpot workflows
- custom CRM automations
- API: FastAPI
- Validation: Pydantic v2
- DB: SQLAlchemy 2.0
- Storage default: SQLite
- Production DB ready: Postgres via
DATABASE_URL - LLM integration: OpenAI-compatible HTTP API
- Tests: Pytest
- Container/dev setup: Docker Compose
ai-lead-qualification-engine/
├── lead_qualification_engine/
│ ├── api/routes/
│ ├── core/
│ ├── models/
│ ├── schemas/
│ ├── services/
│ └── main.py
├── tests/
├── .env.example
├── docker-compose.yml
├── requirements.txt
└── README.md
git clone <your-repo-url>
cd ai-lead-qualification-enginepython -m venv .venv
source .venv/bin/activate # macOS / Linuxpip install -r requirements.txtcp .env.example .envuvicorn lead_qualification_engine.main:app --reloadOpen:
- Swagger UI:
http://127.0.0.1:8000/docs - Health:
http://127.0.0.1:8000/health
{
"full_name": "Jane Founder",
"email": "jane@company.com",
"company": "Acme Corp",
"job_title": "CEO",
"source": "website",
"industry": "SaaS",
"country": "United States",
"company_size": 250,
"annual_revenue": 2000000,
"budget": 20000,
"timeline_days": 10,
"website": "https://company.com",
"notes": "Budget approved. Need integration and a demo next week."
}{
"id": 1,
"full_name": "Jane Founder",
"email": "jane@company.com",
"company": "Acme Corp",
"job_title": "CEO",
"source": "website",
"industry": "SaaS",
"country": "United States",
"company_size": 250,
"annual_revenue": 2000000.0,
"budget": 20000.0,
"timeline_days": 10,
"website": "https://company.com/",
"notes": "Budget approved. Need integration and a demo next week.",
"score": 82,
"intent": "high",
"confidence": 0.96,
"priority": "urgent",
"priority_class": "p0",
"recommended_action": "Route to sales immediately and trigger 15-minute follow-up.",
"tags": ["high_budget", "priority_source", "saas", "united_states", "mid_market", "organic"],
"needs_follow_up": true,
"llm_reasoning": null,
"created_at": "2026-04-16T15:30:00.000000"
}curl -X POST http://localhost:8000/leads/qualify \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"message": "Interested in pricing and demo",
"source": "website"
}'{
"score": 85,
"intent": "high",
"priority": "urgent",
"recommended_action": "Follow up within 5 minutes"
}pytest -q