Skip to content

Arjun-Reddy-I/UFAC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

90 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

UFAC Engine

🌾 UFAC Engine

AI-Powered PM-KISAN Eligibility Assessment

Live Demo Β  API Docs Β  Version


UFAC (Uncertainity Β· First Β· Agent Β· Council) is a multi-agent LLM reasoning framework that determines a farmer's eligibility for India's PM-KISAN β‚Ή6,000/year direct benefit scheme using a council of 5 specialized AI agents working in parallel.



πŸ“‹ Table of Contents


✨ Features

🧠 AI & Intelligence

  • 5-Agent parallel reasoning β€” Fact, Assumption, Unknown, Confidence, Decision
  • RAG pipeline β€” ChromaDB vector search over PM-KISAN rulebook
  • LLaMA 3.3 70B via Groq for sub-10s responses
  • Confidence scoring β€” 0–100 with consensus across agents
  • Graceful degradation β€” falls back to hardcoded rules if RAG fails

⚑ Performance

  • 8–10s first request (4.5x faster than v1)
  • 0.1s cached request (450x faster)
  • 80%+ cache hit rate in production
  • Parallel agent execution via asyncio
  • Singleton RAG β€” loaded once, reused always

πŸ”’ Security

  • CORS lockdown with environment-based origins
  • Rate limiting β€” 10 req/min on /check
  • Input sanitization & injection detection
  • Security headers (XSS, clickjacking, MIME)
  • Circuit breaker β€” 5 failures β†’ 60s auto-recovery

🎨 Frontend

  • Next.js 15 with App Router
  • Dark/light mode with system preference
  • Animated agent flow visualization
  • Real-time backend status indicator
  • Fully responsive β€” mobile to 4K

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        USER BROWSER                             β”‚
β”‚              Next.js 15  Β·  ufac.vercel.app                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚  POST /check
                             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     FastAPI Backend                             β”‚
β”‚              Rate Limiting  Β·  CORS  Β·  Cache                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                                        β”‚
         β–Ό                                        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    UFAC Engine     β”‚                  β”‚    RAG Pipeline     β”‚
β”‚   Orchestrator     │◄────────────────►│  ChromaDB + Rules   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό  asyncio parallel execution
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    AGENT COUNCIL                               β”‚
β”‚                                                                β”‚
β”‚  BATCH 1 (parallel)              BATCH 2 (parallel)           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Fact    β”‚ β”‚ Assumption β”‚     β”‚ Confidence β”‚ β”‚Decision  β”‚ β”‚
β”‚  β”‚  Agent   β”‚ β”‚   Agent    β”‚     β”‚   Agent    β”‚ β”‚  Agent   β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                                  β”‚
β”‚  β”‚ Unknown  β”‚                                                  β”‚
β”‚  β”‚  Agent   β”‚                                                  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
   UFAC Response: answer Β· confidence Β· facts Β· assumptions Β·
                  unknowns Β· risk_level Β· next_steps Β· consensus

πŸ€– The 5 Agents

Agent Role Output
πŸ” Fact Agent Extracts confirmed, objective facts from the user's input known_facts[]
πŸ’­ Assumption Agent Identifies implicit assumptions being made assumptions[]
❓ Unknown Agent Detects missing critical information that could change the verdict unknowns[]
πŸ“Š Confidence Agent Calculates a 0–100 confidence score based on available data confidence score
βœ… Decision Agent Generates the final verdict and actionable next steps answer, next_steps[]

Each agent runs 3Γ— independently and the results are consensus-aggregated β€” meaning the final answer reflects agreement across all runs, reducing hallucination.


πŸ“Š Performance

                  v1.0        v2.0       Improvement
                ─────────   ─────────   ──────────────
First request    45s          8–10s      🟒  4.5Γ—  faster
Cached request   45s          0.1s       🟒  450Γ—  faster
RAG load time    10s          2s         🟒  5Γ—    faster
Avg (80% cache)  45s          0.5s       🟒  90Γ—   faster
Cache hit rate   0%           80%+       🟒  ∞     improvement

πŸš€ Quick Start

Prerequisites

1. Clone & Configure

git clone https://github.com/Arjun-57561/UFAC.git
cd UFAC

# Copy and fill in your API key
cp .env.example .env
# Edit .env β†’ add GROQ_API_KEY=gsk_...

2. Backend

# Install dependencies
pip install -r requirements.txt

# (Optional) Setup RAG vector database
python setup_rag.py

# Start API server
python -m uvicorn api.app:app --reload
# β†’ http://localhost:8000
# β†’ http://localhost:8000/docs  (Swagger UI)

3. Frontend

cd UI
npm install
npm run dev
# β†’ http://localhost:3000

πŸ’‘ That's it. Open http://localhost:3000, fill in the form, and watch the 5 agents assess eligibility in real-time.


πŸ”Œ API Reference

POST /check β€” Run Assessment

Request:

{
  "occupation": "farmer",
  "land_ownership": "yes",
  "aadhaar_linked": true,
  "aadhaar_ekyc": true,
  "bank_account": true,
  "annual_income": 180000,
  "income_tax_payer": false,
  "government_employee": false,
  "monthly_pension_above_10k": false,
  "practicing_professional": false,
  "constitutional_post": false
}

Response:

{
  "answer": "ELIGIBLE for PM-KISAN",
  "confidence": 92,
  "known_facts": ["User is a farmer", "User owns agricultural land"],
  "assumptions": ["Land is self-cultivated, not leased out"],
  "unknowns": ["Land size not specified"],
  "risk_level": "LOW",
  "next_steps": ["Register at pmkisan.gov.in", "Submit land records (Khasra/Khatauni)"],
  "fact_consensus": 0.91,
  "confidence_consensus": 0.88
}

Other Endpoints

Method Endpoint Description
GET /health Health check + RAG status
GET /metrics Request counts, latency, errors
GET /cache-stats Hit rate, memory usage
GET /circuit-status Circuit breaker state
GET /history Past assessments (paginated)
GET /history/{id} Full assessment by ID
POST /cache-clear Flush all caches
GET /docs Swagger UI

πŸ“ Project Structure

UFAC/
β”œβ”€β”€ πŸ“‚ api/
β”‚   └── app.py                  # FastAPI app β€” routing, middleware, rate limiting
β”‚
β”œβ”€β”€ πŸ“‚ core/
β”‚   β”œβ”€β”€ fact_agent.py           # Agent: extracts known facts
β”‚   β”œβ”€β”€ assumption_agent.py     # Agent: identifies assumptions
β”‚   β”œβ”€β”€ unknown_agent.py        # Agent: detects missing info
β”‚   β”œβ”€β”€ confidence_agent.py     # Agent: scores confidence 0–100
β”‚   β”œβ”€β”€ decision_agent.py       # Agent: final verdict + next steps
β”‚   β”œβ”€β”€ ufac_engine.py          # Orchestrator β€” runs agents in parallel
β”‚   β”œβ”€β”€ llm_utils.py            # Groq API wrapper with retry + circuit breaker
β”‚   β”œβ”€β”€ cache.py                # In-memory TTL cache
β”‚   β”œβ”€β”€ metrics.py              # Request/latency/error tracking
β”‚   β”œβ”€β”€ circuit_breaker.py      # Circuit breaker pattern
β”‚   β”œβ”€β”€ database.py             # SQLite/PostgreSQL persistence
β”‚   β”œβ”€β”€ constants.py            # Centralized config (no magic numbers)
β”‚   └── schema.py               # Pydantic response schemas
β”‚
β”œβ”€β”€ πŸ“‚ data/
β”‚   β”œβ”€β”€ rag_pipeline.py         # RAG with singleton + fallback
β”‚   β”œβ”€β”€ pm_kisan_rules.py       # Hard-coded eligibility rules (fallback)
β”‚   └── chroma_db/              # ChromaDB vector store
β”‚
β”œβ”€β”€ πŸ“‚ UI/                      # Next.js 15 frontend
β”‚   β”œβ”€β”€ app/                    # App Router pages
β”‚   β”‚   β”œβ”€β”€ page.tsx            # Home / landing
β”‚   β”‚   β”œβ”€β”€ check/page.tsx      # Eligibility form + results
β”‚   β”‚   β”œβ”€β”€ flow/page.tsx       # Agent flow visualization
β”‚   β”‚   └── about/page.tsx      # About page
β”‚   β”œβ”€β”€ components/             # Reusable React components
β”‚   β”œβ”€β”€ hooks/                  # useUFACAssessment, useBackendStatus
β”‚   └── lib/                    # API client, constants, utils
β”‚
β”œβ”€β”€ πŸ“‚ tests/                   # 30+ unit tests
β”œβ”€β”€ πŸ“„ requirements.txt
β”œβ”€β”€ πŸ“„ .env.example
└── πŸ“„ README.md

πŸ”’ Security

Feature Implementation Status
CORS lockdown ALLOWED_ORIGINS env var βœ…
Rate limiting 10 req/min on /check βœ…
Input sanitization Injection pattern regex βœ…
Security headers X-Frame, XSS, MIME βœ…
Circuit breaker 5 failures β†’ 60s pause βœ…
Exponential backoff 3 retries with tenacity βœ…
No secrets in logs Env-based config only βœ…

πŸ§ͺ Testing

# Run full test suite
pytest tests/ -v

# With coverage report
pytest tests/ --cov=core --cov=api --cov-report=html

# Individual test files
pytest tests/test_cache.py          # Cache layer
pytest tests/test_error_handling.py # Error handling
pytest tests/test_api.py            # API endpoints

30+ unit tests across cache, error handling, and API endpoints.


βš™οΈ Configuration

Create a .env file (copy from .env.example):

# ── Required ──────────────────────────────────────
GROQ_API_KEY=gsk_your_key_here

# ── Security ──────────────────────────────────────
ALLOWED_ORIGINS=http://localhost:3000,https://ufac.vercel.app

# ── Performance ───────────────────────────────────
LLM_TIMEOUT_SECONDS=15.0
CACHE_TTL_ASSESSMENT=3600   # 1 hour
CACHE_TTL_RAG=7200          # 2 hours
CACHE_TTL_LLM=3600          # 1 hour

# ── Database ──────────────────────────────────────
DATABASE_URL=sqlite+aiosqlite:///./ufac_engine.db

# ── Development ───────────────────────────────────
DEV_MODE=true               # Uses 1 LLM call instead of 3
LOG_LEVEL=INFO

πŸ› οΈ Troubleshooting

❌ Backend won't start
  • Check GROQ_API_KEY is set correctly in .env
  • Verify port 8000 is free: lsof -i :8000
  • Check ALLOWED_ORIGINS includes your frontend URL
❌ Rate limit errors (HTTP 429)
  • Wait 60 seconds between bursts
  • Check current counts at GET /metrics
  • Use DEV_MODE=true during development
❌ Circuit breaker open
  • Check GET /circuit-status
  • Verify your Groq API key is valid and has quota
  • Circuit auto-recovers after 60 seconds
❌ RAG not working
  • Run python setup_rag.py to build the vector DB
  • Check GET /rag-status for details
  • System falls back to hardcoded rules automatically
❌ Frontend shows blank page
  • Check browser console for errors
  • Ensure backend is running on port 8000
  • Verify NEXT_PUBLIC_API_URL in UI/.env.local

πŸ‘₯ Team

BTech AIML Β· PBL-2 Project Β· 2023–2027 Batch

Role Contribution
πŸ§‘β€πŸ’» Backend & AI Architecture UFAC engine, agents, RAG pipeline, FastAPI
🎨 Frontend Development Next.js UI, components, animations
πŸ“Š Data & Rules PM-KISAN ruleset, ChromaDB, preprocessing
πŸ§ͺ Testing & DevOps Test suite, Vercel deployment, CI

UFAC Engine v2.0 β€” Production-ready since April 2026

🌐 Live Site Β Β·Β  πŸ“– API Docs Β Β·Β  πŸ› Report Bug


Made with ❀️ for Indian Farmers

About

AI-powered multi-agent PM-KISAN eligibility assessment system | BTech AIML PBL-2

Topics

Resources

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors