A production-ready Retrieval-Augmented Generation (RAG) backend application designed specifically for Urban Rooftop & Terrace Organic Agriculture.
The system provides verified, grounded, and structurally safe agronomic adviceβcombining FastAPI, LangChain (LCEL), Pinecone Serverless Vector Database, and Google Gemini API (gemini-2.0-flash & models/text-embedding-004).
ββββββββββββββββββββββββββ
β Curated Knowledge β
β Base (.md Files) β
βββββββββββββ¬βββββββββββββ
β Ingestion Pipeline
βΌ
βββββββββββββββββββ ββββββββββββββββββββββββββ
β User / Client β β Recursive Character β
β (cURL/Web/App) β β Text Splitter β
ββββββββββ¬βββββββββ βββββββββββββ¬βββββββββββββ
β β
β POST /ask βΌ
βΌ ββββββββββββββββββββββββββ
βββββββββββββββββββ β Gemini text-embedding β
β FastAPI Router β β -004 (768-dim) β
ββββββββββ¬βββββββββ βββββββββββββ¬βββββββββββββ
β β
β LangChain LCEL βΌ
βΌ ββββββββββββββββββββββββββ
βββββββββββββββββββ Embed Query β Pinecone Serverless β
β RAG Pipeline ββββββββββββββββΊβ Vector Database β
β Orchestration βββββββββββββββββ€ (k=3 Similarity) β
ββββββββββ¬βββββββββ Top-3 Chunks ββββββββββββββββββββββββββ
β
β Agronomy Safety Prompt + Context
βΌ
ββββββββββββββββββββββββββ
β Google Gemini 2.0 β
β Flash LLM Generation β
ββββββββββ¬ββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β Verified JSON Response β
β with Source Citations β
ββββββββββββββββββββββββββ
The system comes pre-loaded with curated, expert agronomic documentation in the /data directory:
- Structural Load & Lightweight Mixes (
01_structural_load_and_potting_mix.md):- Residential slab live load capacity (150β200 kg/mΒ² / 30β40 lbs/sq ft).
- Warnings on wet garden soil (1,800β2,200 kg/mΒ³).
- 3:1:1 lightweight organic potting formula (Cocopeat + Vermicompost + Perlite/Pumice, density 450β600 kg/mΒ³).
- Load distribution over columns and load-bearing beams.
- Waterproofing, Drainage & Root Protection (
02_waterproofing_and_drainage.md):- Polyurethane/EPDM waterproofing membranes.
- HDPE anti-root penetration barriers (1.0β1.5mm).
- Polypropylene drainage cells (20β30mm) + Non-woven Geotextile filter fabric (120β150 GSM).
- Raised pot stands (2β4 inches) for air-pruning and terrace floor inspection.
- Organic Pest & Fungal Control (
03_organic_pest_and_fungal_control.md):- Cold-pressed Neem oil spray formulation (5ml neem + 2ml potassium castile soap per liter).
- Fermented sour buttermilk (1:10 dilution) against powdery mildew, downy mildew, and blights.
- Chromatic yellow/blue sticky traps and pheromone traps.
- Absolute ban on synthetic chemical pesticides (chlorpyrifos, imidacloprid).
- Soil Nutrition & Bio-Fertilizers (
04_soil_nutrition_and_biofertilizers.md):- Liquid Jeevamrut biostimulant formulation & fermentation protocol.
- Vermiwash foliar tonics.
- Targeted bio-fertilizers (Azotobacter, Rhizobium, PSB Bacillus megaterium, KMB, Mycorrhiza/VAM).
- Enriched vermicompost, neem cake powder, rock phosphate, and wood ash top-dressing.
- Microclimates, Containers & Companion Planting (
05_microclimate_containers_and_companion_planting.md):- UV-stabilized HDPE grow bags (200β240 GSM) and Sub-Irrigated Planters (SIP).
- 50% UV-stabilized agro-shade nets (temperatures reduced by 4β6Β°C).
- Living and porous windbreaks for high rooftop wind speeds.
- Symbiotic guilds (Tomato + Basil + Marigold; Urban Three Sisters; Borage + Strawberries).
- Framework: FastAPI with Uvicorn
- Orchestration: LangChain Expression Language (LCEL)
- Vector Database: Pinecone Serverless (AWS
us-east-1, 768 dimensions, cosine similarity) - LLM: Google Gemini (
gemini-2.0-flash) - Embeddings: Google Gemini (
models/text-embedding-004) - Validation: Pydantic v2 & Pydantic Settings
- Configuration: python-dotenv
orliv/
βββ app/
β βββ __init__.py
β βββ config.py # Pydantic Settings for environment variables
β βββ schemas.py # Pydantic Request & Response models
β βββ main.py # FastAPI app, lifespan, endpoints, error handlers
β βββ services/
β β βββ __init__.py
β β βββ embeddings.py # Google Gemini Embeddings (text-embedding-004)
β β βββ vector_store.py # Pinecone Serverless connection & auto-provisioning
β β βββ rag_chain.py # LCEL RAG pipeline with custom Agronomy prompt
β β βββ ingestion.py # Document loader, Recursive splitter & Pinecone upsert
β βββ utils/
β βββ __init__.py
β βββ logger.py # Structured logger
βββ data/ # Markdown Knowledge Base documents
β βββ 01_structural_load_and_potting_mix.md
β βββ 02_waterproofing_and_drainage.md
β βββ 03_organic_pest_and_fungal_control.md
β βββ 04_soil_nutrition_and_biofertilizers.md
β βββ 05_microclimate_containers_and_companion_planting.md
βββ scripts/
β βββ ingest.py # Standalone CLI ingestion script
β βββ query_cli.py # Terminal interactive query tool
βββ tests/
β βββ __init__.py
β βββ test_api.py # Unit & integration test suite
βββ .env.example # Environment variable template
βββ .env # Local environment configuration
βββ requirements.txt # Production Python dependencies
βββ README.md # Comprehensive documentation
- Python 3.10+
- Google Gemini API Key (Get one here)
- Pinecone API Key (Get one here)
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
# On Windows:
.\venv\Scripts\activate
# On Linux/macOS:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txtEdit the .env file (copied from .env.example):
# Google Gemini API
GOOGLE_API_KEY=AIzaSy...your_gemini_api_key_here
GEMINI_MODEL=gemini-2.0-flash
EMBEDDING_MODEL=models/text-embedding-004
# Pinecone Vector Database
PINECONE_API_KEY=pcsk_...your_pinecone_api_key_here
PINECONE_INDEX_NAME=urban-rooftop-farming
PINECONE_CLOUD=aws
PINECONE_REGION=us-east-1
PINECONE_DIMENSION=768
PINECONE_METRIC=cosine
# Application Settings
APP_NAME="Urban Rooftop Organic Farming RAG Assistant"
APP_ENV=development
DEBUG=true
PORT=8000
HOST=0.0.0.0
# Retrieval & Ingestion Settings
TOP_K_RETRIEVAL=3
CHUNK_SIZE=600
CHUNK_OVERLAP=100You can ingest the curated knowledge base into Pinecone via either the CLI script or the API endpoint. The system will automatically provision the Serverless Pinecone index if it doesn't already exist.
python scripts/ingest.pycurl -X POST http://localhost:8000/ingestStart the FastAPI application with Uvicorn:
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reloadInteractive Swagger API Documentation is available at:
π http://localhost:8000/docs
Alternative ReDoc Documentation:
π http://localhost:8000/redoc
Check service status, Pinecone connectivity, and Gemini model availability:
curl -X GET "http://localhost:8000/health" \
-H "Accept: application/json"Example Response:
{
"status": "healthy",
"app_name": "Urban Rooftop Organic Farming RAG Assistant",
"version": "1.0.0",
"timestamp": "2026-08-19T11:15:30.123456Z",
"services": {
"gemini_llm": {
"status": "configured",
"model": "gemini-2.0-flash",
"embedding_model": "models/text-embedding-004"
},
"pinecone": {
"status": "connected",
"index_name": "urban-rooftop-farming",
"dimension": 768,
"vector_stats": {
"total_vector_count": 28,
"dimension": 768,
"index_fullness": 0.0,
"namespaces": {}
}
}
}
}curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{
"question": "What is the recommended lightweight potting mix formula to prevent roof overload?",
"top_k": 3,
"include_sources": true
}'curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{
"question": "How do I prepare and apply fermented buttermilk and neem oil for powdery mildew on terrace tomatoes?",
"top_k": 3,
"include_sources": true
}'curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{
"question": "What is the recipe for preparing liquid Jeevamrut, and how should it be applied to container plants?",
"top_k": 3,
"include_sources": true
}'curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{
"question": "Why shouldn'\''t grow bags rest directly on terrace tiles, and what drainage system should be used?",
"top_k": 3,
"include_sources": true
}'curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{
"question": "What agro-shade net specification is best for rooftop summer heat, and what companion plants protect tomatoes?",
"top_k": 3,
"include_sources": true
}'You can also ask questions interactively inside your terminal:
python scripts/query_cli.pyRun unit and integration tests using pytest:
pytest tests/ -v| Scenario | HTTP Status | Description |
|---|---|---|
| Missing / Placeholder API Keys | 401 Unauthorized |
Clear prompt specifying missing GOOGLE_API_KEY or PINECONE_API_KEY. |
| Invalid Question Payload | 422 Unprocessable |
Pydantic validation rejects empty, whitespace, or excessively long prompts. |
| Gemini Quota / Rate Limit | 429 Too Many Requests |
Handled gracefully with retry advisory. |
| Pinecone Network / Timeout | 504 Gateway Timeout |
Vector search timeout protection with structured error JSON. |
| Index Provisioning | Auto-managed | Serverless index is provisioned automatically with cosine distance. |
MIT License. Built for Sustainable Urban Agriculture.