A whatsapp chat board for cameroon law explaination
# Cameroonian Law RAG System
A production-ready NestJS backend that implements a legal RAG (Retrieval Augmented Generation) system for Cameroonian law.
## Features
- **PDF Ingestion**: Upload PDF law books (Penal Code, Criminal Procedure Code, Constitution)
- **Text Extraction**: Extracts text from PDF using pdf-parse
- **Structured Parsing**: Splits text into Articles and Sections
- **Smart Chunking**: Chunks long articles into 800-word pieces for optimal embedding
- **Vector Storage**: Stores OpenAI embeddings in PostgreSQL using pgvector
- **Similarity Search**: Cosine similarity search using pgvector
- **AI Answers**: RAG-powered question answering that prevents hallucination
## Tech Stack
- **NestJS** - Node.js framework
- **PostgreSQL** - Database
- **pgvector** - Vector similarity search
- **Prisma ORM** - Database access
- **OpenAI API** - Embeddings and chat completion
- **pdf-parse** - PDF text extraction
- **class-validator** - DTO validation
## Prerequisites
1. Node.js 18+
2. PostgreSQL with pgvector extension
3. OpenAI API key
## Setup
### 1. Install Dependencies
```bash
npm install
```
### 2. Configure Environment
Copy `.env.example` to `.env` and fill in your values:
```env
DATABASE_URL="postgresql://postgres:password@localhost:5432/cm_law?schema=public"
OPENAI_API_KEY="your-openai-api-key"
PORT=3000
NODE_ENV="development"
```
### 3. Set Up Database
Run the pgvector migration:
```bash
psql -U postgres -d cm_law -f prisma/migrations/001_enable_pgvector.sql
```
### 4. Generate Prisma Client
```bash
npm run prisma:generate
```
### 5. Run the Application
```bash
npm run start:dev
```
The API will be available at `
localhost`
## API Endpoints
### 1. Ingest PDF
Upload a PDF law document.
```bash
curl -X POST
localhost \
-F "file=@./penal-code.pdf" \
-F "lawName=Penal Code"
```
### 2. Ingest Text
Ingest raw text content directly.
```bash
curl -X POST
localhost \
-H "C …