Skip to content

RITIK-KHARYA/leafra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

151 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Leafra - AI-Powered PDF RAG System

Leafra is a Next.js application that provides a Retrieval-Augmented Generation (RAG) system for PDF documents. Users can upload PDFs, ask questions about them, and receive AI-powered responses based on the document content.

Features

  • πŸ“„ PDF Upload and Processing
  • πŸ€– AI-Powered Chat Interface
  • πŸ” Vector Search using Pinecone
  • πŸ” Authentication (Email/Password + OAuth)
  • πŸ“Š Real-time Chat Streaming
  • πŸ—„οΈ PostgreSQL Database
  • ⚑ Background Job Processing with BullMQ

Architecture

Tech Stack

  • Framework: Next.js 15 (App Router)
  • Database: PostgreSQL with Drizzle ORM
  • Vector Database: Pinecone
  • AI/ML: TogetherAI (Embeddings & Chat)
  • Queue: BullMQ with Redis (Upstash)
  • File Upload: UploadThing
  • Authentication: Better-auth
  • UI: React, Tailwind CSS, shadcn/ui

Application Flow

  1. Authentication Flow: User signs up/in β†’ Better-auth β†’ Session management
  2. Chat Creation: User creates chat β†’ Stored in DB β†’ Chat page loads
  3. PDF Upload: UploadThing β†’ BullMQ queue β†’ Worker processes β†’ Pinecone embeddings
  4. Chat Flow: User message β†’ API route β†’ Save to DB β†’ Query Pinecone β†’ Stream AI response β†’ Save response

Getting Started

Prerequisites

  • Node.js 18+ or Bun
  • PostgreSQL database
  • Pinecone account
  • TogetherAI account
  • Upstash Redis account (optional but recommended)
  • UploadThing account

Installation

  1. Clone the repository:
git clone <repository-url>
cd leafra
  1. Install dependencies:
npm install
# or
bun install
  1. Set up environment variables: Create a .env.local file in the root directory with the following variables:
# Database
DATABASE_URL=postgresql://username:password@host:port/database

# Pinecone
PINECONE_API_KEY=your-pinecone-api-key

# TogetherAI
TOGETHER_AI_API_KEY=your-together-ai-api-key
TOGETHER_AI_MODEL=your-model-name

# Redis (Upstash) - Optional
UPSTASH_REDIS_REST_URL=https://your-redis-instance.upstash.io
UPSTASH_REDIS_REST_TOKEN=your-redis-token

# OAuth Providers (Optional)
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
DISCORD_CLIENT_ID=your-discord-client-id
DISCORD_CLIENT_SECRET=your-discord-client-secret

# Next.js
NEXT_PUBLIC_BASE_URL=http://localhost:3000
NODE_ENV=development
  1. Set up the database:
# Run migrations
npm run db:push
# or with drizzle-kit
npx drizzle-kit push
  1. Start the development server:
npm run dev
# or
bun dev
  1. Start the worker (in a separate terminal):
# Compile worker
tsc --watch ./lib/worker.ts

# Run worker (in another terminal)
bunx nodemon ./lib/worker.js
# or
bun run ./lib/worker.js

Open http://localhost:3000 in your browser.

Project Structure

leafra/
β”œβ”€β”€ app/                    # Next.js App Router
β”‚   β”œβ”€β”€ api/               # API routes
β”‚   β”‚   β”œβ”€β”€ auth/          # Authentication endpoints
β”‚   β”‚   β”œβ”€β”€ chat/          # Chat API
β”‚   β”‚   β”œβ”€β”€ messages/      # Messages API
β”‚   β”‚   └── uploadthing/   # File upload
β”‚   β”œβ”€β”€ actions/           # Server actions
β”‚   β”œβ”€β”€ chat/              # Chat pages
β”‚   β”œβ”€β”€ dashboard/         # Dashboard page
β”‚   └── layout.tsx         # Root layout
β”œβ”€β”€ components/             # React components
β”‚   β”œβ”€β”€ custom/            # Custom components
β”‚   β”œβ”€β”€ ui/                # shadcn/ui components
β”‚   └── shared/            # Shared components
β”œβ”€β”€ lib/                   # Utility libraries
β”‚   β”œβ”€β”€ auth.ts            # Authentication config
β”‚   β”œβ”€β”€ db.ts              # Database connection
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   └── schema.ts      # Database schema
β”‚   β”œβ”€β”€ env.ts             # Environment validation
β”‚   β”œβ”€β”€ logger.ts          # Logging utility
β”‚   β”œβ”€β”€ api-response.ts    # API response helpers
β”‚   β”œβ”€β”€ integrations/     # Third-party integrations
β”‚   β”‚   β”œβ”€β”€ pinecone.ts   # Pinecone client
β”‚   β”‚   └── redis.ts      # Redis client
β”‚   └── worker.ts          # Background worker
β”œβ”€β”€ types/                 # TypeScript types
└── middleware.ts          # Next.js middleware

API Documentation

Authentication

All API routes (except /api/auth/*) require authentication via session cookie.

Endpoints

POST /api/chat

Send a message to the chat and get an AI response.

Request Body:

{
  "messages": [
    {
      "role": "user",
      "content": "What is this document about?"
    }
  ],
  "chatId": "uuid-string"
}

Response: Streaming text response

GET /api/messages?chatId=uuid

Get all messages for a chat.

Query Parameters:

  • chatId (required): UUID of the chat

Response:

[
  {
    "id": 1,
    "chatId": "uuid",
    "content": "Message content",
    "role": "user",
    "createdAt": "2024-01-01T00:00:00Z"
  }
]

Development

Available Scripts

  • npm run dev - Start development server
  • npm run build - Build for production
  • npm run start - Start production server
  • npm run lint - Run ESLint
  • npm run typecheck - Run TypeScript type checking
  • npm run dev:worker - Watch and run worker

Code Quality

  • TypeScript for type safety
  • ESLint for code linting
  • Zod for runtime validation
  • Structured logging
  • Standardized error responses

Deployment

Environment Variables

Ensure all required environment variables are set in your production environment. See the .env.example file for reference.

Database Migrations

Run database migrations before deploying:

npx drizzle-kit push

Worker Process

The worker process must be running separately in production. Consider using:

  • PM2
  • Docker containers
  • Cloud functions
  • Kubernetes jobs

Recommended Platforms

  • Vercel - For Next.js hosting
  • Railway - For PostgreSQL and worker
  • Upstash - For Redis
  • Pinecone - For vector database

Security

  • All API routes are authenticated
  • Environment variables are validated at startup
  • Input validation using Zod
  • SQL injection protection via Drizzle ORM
  • Session-based authentication

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests and linting
  5. Submit a pull request

License

[Add your license here]

Support

For issues and questions, please open an issue on GitHub.

About

RAG system for pdf reading ( if you are not able to chat then most probably im out of tokens )

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors