Skip to content

tantasui/rag-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌊 Decentralized RAG System

A complete decentralized Retrieval-Augmented Generation (RAG) system combining Walrus storage, Sui blockchain, and AI to create a trustless, permanent document storage and intelligent querying platform.

🎯 Overview

This project demonstrates a fully functional decentralized RAG system where:

  • πŸ“€ Users upload documents to Walrus (decentralized storage)
  • ⛓️ Document ownership is recorded as NFTs on Sui blockchain
  • πŸ€– AI-powered querying using OpenAI + LangChain + ChromaDB
  • πŸ” Wallet-based authentication and access control

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Frontend  β”‚      β”‚   Backend    β”‚      β”‚   Walrus    β”‚
β”‚   (React)   │─────▢│  (FastAPI)   │─────▢│  Storage    β”‚
β”‚             β”‚      β”‚              β”‚      β”‚             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                     β”‚
       β”‚                     β”‚
       β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Sui Wallet  β”‚      β”‚ Sui Blockchainβ”‚
β”‚             β”‚      β”‚   (pysui)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚  Document    β”‚
                     β”‚  Registry    β”‚
                     β”‚  (Move)      β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚    RAG       β”‚
                     β”‚  (LangChain) β”‚
                     β”‚  (ChromaDB)  β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Features

Core Functionality

  • βœ… Document Upload: Upload PDFs, text files, and more to Walrus
  • βœ… NFT Minting: Automatic NFT creation on Sui for each document
  • βœ… AI Querying: Ask questions about your documents with AI-powered answers
  • βœ… Source Citations: Get exact references from your documents
  • βœ… Wallet Integration: Connect with Sui wallets for authentication
  • βœ… Access Control: Private and public document settings

Technical Features

  • πŸ”„ Decentralized Storage: Documents stored on Walrus (no central server)
  • ⛓️ Blockchain Verification: Ownership tracked on Sui blockchain
  • 🎯 Vector Search: Efficient similarity search with ChromaDB
  • 🧠 RAG Pipeline: LangChain-powered retrieval and generation
  • πŸ”’ Secure: Wallet-based authentication

πŸ“¦ Tech Stack

Backend

  • FastAPI: High-performance Python web framework
  • pysui: Official Python SDK for Sui blockchain
  • LangChain: RAG orchestration framework
  • ChromaDB: Vector database for embeddings
  • OpenAI: GPT models for text generation
  • httpx: Async HTTP client for Walrus

Frontend

  • React: UI framework
  • Vite: Build tool and dev server
  • @mysten/dapp-kit: Sui wallet integration
  • Axios: HTTP client

Blockchain & Storage

  • Sui Move: Smart contract language
  • Walrus: Decentralized blob storage
  • Sui Network: Layer 1 blockchain

πŸ› οΈ Installation

Prerequisites

  • Python 3.8+
  • Node.js 16+
  • Sui CLI (for contract deployment)
  • OpenAI API key

Quick Setup

  1. Clone the repository
git clone <repository-url>
cd rag-python
  1. Run setup script
chmod +x setup.sh
./setup.sh
  1. Configure environment
# Edit .env file
cp .env.example .env
nano .env

Required configuration:

SUI_PACKAGE_ID=0x...          # From contract deployment
SUI_NETWORK=testnet
OPENAI_API_KEY=sk-...         # Your OpenAI API key
WALRUS_PUBLISHER_URL=https://publisher-devnet.walrus.space
WALRUS_AGGREGATOR_URL=https://aggregator-devnet.walrus.space

Manual Setup

Backend Setup

cd backend
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt

Frontend Setup

cd frontend
npm install

Deploy Smart Contract

cd contracts
sui move build
sui client publish --gas-budget 100000000
# Save the Package ID to your .env file

πŸƒ Running the Application

1. Start Backend

cd backend
source venv/bin/activate  # On Windows: venv\Scripts\activate
python run.py

Backend will run on http://localhost:8000

2. Start Frontend (in new terminal)

cd frontend
npm run dev

Frontend will run on http://localhost:3000

3. Access the Application

Open your browser and navigate to http://localhost:3000

πŸ“– Usage Guide

1. Connect Wallet

  • Click "Connect Wallet" button
  • Select your Sui wallet (Sui Wallet, Suiet, etc.)
  • Approve the connection

2. Upload Documents

  • Click "Choose File" and select a document (PDF, TXT, etc.)
  • Optionally check "Make document public"
  • Click "Upload to Walrus & Sui"
  • Wait for:
    • βœ… Upload to Walrus
    • βœ… NFT minting on Sui
    • βœ… RAG processing

3. Query Documents

  • Type your question in the query box
  • Click "Ask AI"
  • View AI-generated answer with source citations

4. Manage Documents

  • View all your documents in the "My Documents" section
  • Download documents
  • See metadata (upload date, visibility, etc.)

πŸ”§ API Endpoints

Document Operations

  • POST /upload-document - Upload document to Walrus & Sui
  • GET /documents/{wallet_address} - Get user's documents
  • GET /download/{blob_id} - Download document from Walrus

Query Operations

  • POST /query - Query documents with AI

System

  • GET /health - Health check
  • GET / - API information

πŸ“ Smart Contract

The Sui Move smart contract (contracts/document_registry/sources/registry.move) provides:

Functions:

  • mint_document() - Create document NFT
  • transfer_document() - Transfer ownership
  • set_visibility() - Change public/private status

Events:

  • DocumentMinted - Emitted when document is created
  • DocumentTransferred - Emitted on ownership transfer
  • VisibilityChanged - Emitted when visibility changes

πŸ” Security Considerations

  1. Private Keys: Never commit private keys or mnemonics
  2. API Keys: Keep OpenAI API key secure
  3. Access Control: Document visibility is enforced on-chain
  4. Wallet Verification: All operations require wallet signature

πŸ§ͺ Testing

Backend Tests

cd backend
pytest tests/

Frontend Tests

cd frontend
npm run test

πŸš€ Deployment

Backend Deployment

  • Deploy to any Python-compatible hosting (AWS, GCP, Heroku)
  • Set environment variables
  • Use gunicorn or uvicorn in production

Frontend Deployment

cd frontend
npm run build
# Deploy 'dist' folder to Vercel, Netlify, or any static host

Contract Deployment

cd contracts/document_registry
sui move build
sui client publish --gas-budget 100000000

πŸ“Š Project Structure

rag-python/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py              # FastAPI application
β”‚   β”‚   β”œβ”€β”€ config.py            # Configuration
β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   └── schemas.py       # Pydantic models
β”‚   β”‚   └── services/
β”‚   β”‚       β”œβ”€β”€ walrus_service.py    # Walrus integration
β”‚   β”‚       β”œβ”€β”€ sui_service.py       # Sui blockchain integration
β”‚   β”‚       └── rag_service.py       # RAG system
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── run.py
β”œβ”€β”€ contracts/
β”‚   └── document_registry/
β”‚       β”œβ”€β”€ Move.toml
β”‚       └── sources/
β”‚           └── registry.move    # Smart contract
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx              # Main app component
β”‚   β”‚   β”œβ”€β”€ main.jsx             # Entry point
β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”‚   └── sui.js           # Sui configuration
β”‚   β”‚   └── components/
β”‚   β”‚       β”œβ”€β”€ DocumentUpload.jsx
β”‚   β”‚       β”œβ”€β”€ DocumentList.jsx
β”‚   β”‚       └── QueryInterface.jsx
β”‚   β”œβ”€β”€ package.json
β”‚   └── vite.config.js
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
β”œβ”€β”€ setup.sh
└── README.md

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

πŸ› Troubleshooting

Backend won't start

  • Check Python version: python --version (need 3.8+)
  • Verify all dependencies: pip install -r requirements.txt
  • Check .env configuration

Frontend won't start

  • Check Node version: node --version (need 16+)
  • Clear node_modules: rm -rf node_modules && npm install

Contract deployment fails

  • Check Sui CLI: sui --version
  • Verify wallet has gas: sui client gas
  • Check network configuration

Wallet won't connect

  • Install Sui Wallet browser extension
  • Check network (devnet/testnet/mainnet)
  • Try refreshing the page

πŸ“š Resources

πŸ“„ License

MIT License - see LICENSE file for details

πŸŽ‰ Acknowledgments

  • Built for the Walrus/Sui Hackathon
  • Uses OpenAI GPT models
  • Powered by Walrus decentralized storage
  • Built on Sui blockchain

πŸ“§ Contact

For questions or support, please open an issue on GitHub.


Built with ❀️ using Walrus, Sui, and AI

About

halout hackathon

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors