An AI-powered conversational platform built with TypeScript and Express, featuring Google Gemini, LangChain, PostgreSQL, Redis, authentication, chat history, rate limiting, and automatic conversation summarization.
- π Authentication & Authorization β JWT authentication with bcrypt password hashing, HTTP-only cookies, and Redis-based JWT blocklisting.
- π€ AI Integration β Google Gemini API through LangChain.
- π¬ Chat System β Create chats, send messages, and retrieve chat history.
- ποΈ PostgreSQL β Stores users, chats, messages, summaries, and lifetime token usage.
- β‘ Redis β Token-window usage tracking, rate limiting, and JWT blocklisting.
- π Token Tracking β Tracks token usage for users and individual chats.
- β±οΈ Token Limits β Configurable token limits with automatic time-based reset using Redis.
- π Chat Summarization β Automatically summarizes conversations to maintain useful context.
- π¦ Rate Limiting β Separate rate limiting for authenticated and unauthenticated requests.
- β Validation & Security β Zod validation, Helmet, CORS, and structured logging.
- Bun
- Express.js
- TypeScript
- LangChain
- Google Gemini
- PostgreSQL
- Redis
- Docker & Docker Compose
- JWT
- bcrypt
- Zod
- Helmet
- CORS
- Pino
- dotenv
ChatForge uses Redis for temporary token-window usage and PostgreSQL for lifetime usage.
Redis
βββ Current token window
βββ TOKEN_LIMIT
βββ TOKEN_WINDOW_SECONDS
PostgreSQL
βββ total_token_used
βββ Lifetime usage
Example configuration:
TOKEN_LIMIT=10000
TOKEN_WINDOW_SECONDS=18000This allows 10,000 tokens per 5-hour window.
USERS
+-------------------------------+
| id |
| name |
| age |
| email |
| password |
| total_token_used |
| created_at |
| updated_at |
+---------------+---------------+
|
| 1
|
| N
βΌ
CHATS
+-------------------------------+
| id |
| user_id |
| topic |
| model |
| summary |
| summarized_till_message_number|
| message_count |
| prompt_tokens |
| completion_tokens |
| total_tokens |
| created_at |
| updated_at |
+---------------+---------------+
|
| 1
|
| N
βΌ
MESSAGES
+-------------------------------+
| id |
| user_id |
| chat_id |
| role |
| content |
| tokens |
| prompt_tokens |
| completion_tokens |
| total_tokens |
| created_at |
| updated_at |
+-------------------------------+
Each user can create multiple chat sessions.
User
βββ Chat 1 (Recursion)
βββ Chat 2 (Linked List)
βββ Chat 3 (Operating System)
Relationship:
User (1)
β
βββββββββββ< Chats (N)
Every chat contains multiple messages exchanged between the user and the AI.
Chat
βββ User : Explain recursion
βββ AI : Recursion is...
βββ User : Give me an example
βββ AI : Sure...
Relationship:
Chat (1)
β
βββββββββββ< Messages (N)
Although every message belongs to a chat, storing the user_id in the messages table allows efficient retrieval of messages belonging to a specific user without requiring an additional join with the chats table.
Relationship:
User (1)
β
βββββββββββ< Messages (N)
Optimizes retrieval of a user's most recently updated chats.
CREATE INDEX idx_chats_user_updated
ON chats(user_id, updated_at DESC);Optimizes loading messages from a specific chat in chronological order.
CREATE INDEX idx_messages_chat_created
ON messages(chat_id, created_at);Optimizes retrieval of messages belonging to a specific user.
CREATE INDEX idx_messages_user_created
ON messages(user_id, created_at DESC);git clone https://github.com/soumadip-dev/ChatForge.git
cd ChatForgebun installdocker compose up -d --buildCreate a .env file:
PORT=8080
NODE_ENV=development
DATABASE_URL=<your-postgresql-url>
LOG_LEVEL=info
CORS_ORIGINS=<your-frontend-url>
JWT_SECRET=<your-jwt-secret>
JWT_ACCESS_EXPIRES_IN=1h
GEMINI_API_KEY=<your-gemini-api-key>
REDIS_URL=<your-redis-url>
TOKEN_LIMIT=10000
TOKEN_WINDOW_SECONDS=18000bun run migratebun run devbun run dev # Start development server
bun run migrate # Run database migrations
bun run build # Build production bundle
bun run start # Start production serverSoumadip Majila
