LexAI is a RAG-powered legal document analyzer for contracts, leases, terms of service, NDAs, and other PDF legal documents. Upload a document, ask questions in plain English, and get grounded answers with exact clause-level citations from the source text.
- Next.js 14 App Router
- TypeScript
- Tailwind CSS
- Next.js API routes
- LangChain.js
- OpenAI
text-embedding-3-smallandgpt-4o - Prisma ORM
- PostgreSQL with
pgvector pdf-parsefor PDF text extractionformidablefor uploads- Docker Compose for local PostgreSQL
- Simple password gate with HTTP-only session cookie
- In-memory API rate limiting with
lru-cache
- Node.js 18 or newer, with Node 20 LTS recommended
- Docker and Docker Compose
- OpenAI API key
-
Install dependencies:
npm install
-
Create your local environment files:
cp .env.local.example .env.local cp .env.local.example .env
Next.js reads
.env.localwhen the app runs. Prisma CLI reads.envwhen running commands such asnpx prisma migrate dev. -
Add your OpenAI key and app password to both
.env.localand.env:DATABASE_URL=postgresql://postgres:postgres@localhost:5433/lexai OPENAI_API_KEY=your_openai_api_key_here APP_PASSWORD=your_password_here
-
Start PostgreSQL with pgvector:
docker-compose up -d
-
Generate Prisma Client and run migrations:
npx prisma generate npx prisma migrate dev
-
Start the app:
npm run dev
-
Open
http://localhost:3000.
- Open
http://localhost:3000. - Enter the password configured as
APP_PASSWORD. - Click Upload Document or drop a PDF into the upload zone.
- Wait for LexAI to extract text, chunk it, generate embeddings, and index the document.
- Select the uploaded document from the sidebar.
- Ask a question in the chat input.
- Read the plain-language answer and review the cited source clauses.
- Click Sign out in the sidebar to clear the session cookie.
When a PDF is uploaded, LexAI extracts its text with pdf-parse, splits the text into overlapping chunks of about 500 tokens, and generates an OpenAI embedding for every chunk. Each chunk and its vector are stored in PostgreSQL using pgvector.
When a user asks a question, LexAI embeds the question, runs a cosine similarity search against the stored chunk vectors, and retrieves the five most relevant chunks. Those chunks are passed to gpt-4o through a LangChain chat model with a strict system prompt that requires grounded answers and exact citations.
LexAI is protected by a single shared password. Successful login sets an HTTP-only lexai_session cookie for the browser session, and middleware redirects unauthenticated requests to /login.
| Variable | Required | Description |
|---|---|---|
DATABASE_URL |
Yes | PostgreSQL connection string. Defaults locally to postgresql://postgres:postgres@localhost:5433/lexai with the included Docker Compose file. |
OPENAI_API_KEY |
Yes | API key used for embeddings and chat completions. |
APP_PASSWORD |
Yes | Shared password required to access the app. |