Skip to content

sambitsargam/CropRAG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CropRAG

CropRAG is a Verifiable Trait Registry for crops built on the BNB Smart Chain Testnet. It combines Retrieval-Augmented Generation (RAG) with blockchain technology to create an immutable, queryable database of agricultural trait data.

Features

✨ Key Capabilities:

  • 🌾 Trait Registration - Register crop traits on the blockchain with full immutability
  • πŸ” RAG Query System - Query crop traits using natural language with document retrieval
  • πŸ“Š Vector Database - In-memory vector DB with cosine similarity for semantic search
  • ⛓️ Blockchain Integration - Traits stored on BNB Testnet via smart contracts
  • πŸ” Wallet Management - Automatic wallet integration using backend private key
  • πŸ“± Modern UI - Premium dark theme with smooth animations and responsive design

Tech Stack

Frontend:

  • React 18 + Tailwind CSS
  • ethers.js v5 for blockchain interaction
  • Axios for HTTP requests
  • Context API for state management

Backend:

  • Node.js + Express
  • ethers.js for blockchain transactions
  • In-memory vector database with cosine similarity

Blockchain:

Getting Started

Prerequisites

  • Node.js (v19+)
  • npm or yarn
  • BNB Testnet tokens (optional, for gas)

Installation

# Clone repository
git clone https://github.com/sambitsargam/CropRAG.git
cd CropRAG

# Backend setup
cd backend
npm install
cp .env.example .env
# Edit .env with your PRIVATE_KEY and CONTRACT_ADDRESS
node index.js

# Frontend setup (new terminal)
cd frontend
npm install
npm start

Environment Variables

Backend (.env):

PRIVATE_KEY=your_private_key_here
CONTRACT_ADDRESS=0x635BaC4ed3321ba4E8E5f5B1921C73B8ff6f7278
BSC_TESTNET_RPC=https://data-seed-prebsc-1-s1.bnbchain.org:8545
PORT=5001

Usage

1. Access the Application

2. Query Traits

Navigate to Query Traits tab and search for crop information:

Example queries:
- "Tell me about maize drought resistance"
- "What traits improve wheat yield?"
- "Properties of rice early maturity"

3. Register New Traits

Go to Create Trait Asset tab and fill in:

  • Crop Name: e.g., "Maize", "Wheat", "Rice"
  • Traits: e.g., "drought_resistance, early_maturity, high_yield"
  • Data: Trait measurements in JSON or CSV format

Example:

{
  "crop": "Maize",
  "traits": ["drought_resistance", "early_maturity"],
  "yield_kg_per_hectare": 8500,
  "water_requirement_mm": 450,
  "days_to_maturity": 120,
  "protein_content_percent": 10.5
}

API Endpoints

Backend REST API

Endpoint Method Description
/health GET Health check
/api/wallet-info GET Get wallet address from private key
/api/ingest POST Ingest documents into vector DB
/api/query POST Query using RAG
/api/register-trait POST Register trait on blockchain
/api/trait/:hash GET Retrieve trait record
/api/records/count GET Get total registered traits

Example Requests

Query Traits:

curl -X POST http://localhost:5001/api/query \
  -H "Content-Type: application/json" \
  -d '{"query": "drought resistant crops", "topK": 3}'

Register Trait:

curl -X POST http://localhost:5001/api/register-trait \
  -H "Content-Type: application/json" \
  -d '{
    "cropName": "Maize",
    "traits": ["drought_resistance", "yield"],
    "traitData": "{...}"
  }'

Project Structure

CropRAG/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ index.js              # Express server & API endpoints
β”‚   β”œβ”€β”€ utils.js              # Utility functions
β”‚   β”œβ”€β”€ .env                  # Environment variables
β”‚   └── package.json
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ LandingPage.js
β”‚   β”‚   β”‚   β”œβ”€β”€ TraitQuery.js
β”‚   β”‚   β”‚   β”œβ”€β”€ CreateAsset.js
β”‚   β”‚   β”‚   └── WalletConnect.js
β”‚   β”‚   β”œβ”€β”€ context/
β”‚   β”‚   β”‚   └── MetaMaskContext.js
β”‚   β”‚   β”œβ”€β”€ App.js
β”‚   β”‚   └── index.js
β”‚   └── package.json
β”œβ”€β”€ contracts/
β”‚   └── abi.json              # TraitRegistry ABI
β”œβ”€β”€ data.json                 # Sample trait data
└── README.md

Smart Contract

TraitRegistry.sol:

  • Located at: 0x635BaC4ed3321ba4E8E5f5B1921C73B8ff6f7278
  • Network: BNB Smart Chain Testnet
  • Function: registerTrait(string memory traitData, string memory cropName, string[] memory traits)

View on BscScan: https://testnet.bscscan.com/address/0x635BaC4ed3321ba4E8E5f5B1921C73B8ff6f7278

Sample Data

Sample crop trait data is available in data.json. To ingest this data:

curl -X POST http://localhost:5001/api/ingest \
  -H "Content-Type: application/json" \
  -d @data.json

Features in Development

  • πŸ”œ Advanced trait filtering
  • πŸ”œ Multi-chain support
  • πŸ”œ IPFS integration for data storage
  • πŸ”œ Mobile app support
  • πŸ”œ Real ML embeddings

Troubleshooting

Backend won't start

# Check if port 5001 is in use
lsof -i :5001

# Kill process on port 5001
kill -9 <PID>

Frontend connection issues

  • Ensure backend is running on http://localhost:5001
  • Check browser console (F12) for CORS errors
  • Verify .env variables in backend

Wallet not connecting

Wallet & Faucet

Get test BNB tokens for the testnet:

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and questions:

  • Open an issue on GitHub
  • Check existing documentation

Built with ❀️ for agricultural data integrity

About

CropRAG is a retrieval-augmented agriculture assistant that lets users load real trait and crop datasets into the browser, search them with local embeddings, and get ChatGPT-powered, citation-grounded answers.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages