Skip to content

yelaco/ai-document-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

37 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Document Backend

A powerful NestJS-based backend application that enables users to upload PDF documents and interact with them using AI-powered question-answering capabilities. The system leverages Retrieval-Augmented Generation (RAG) to provide accurate, context-aware responses based on document content.

demo_ai_document.mp4

πŸš€ Features

  • PDF Document Processing: Upload and parse PDF documents with automatic text extraction
  • AI-Powered Q&A: Ask questions about uploaded documents and receive intelligent answers
  • Chats and Messages: Threaded conversations with full CRUD endpoints, chat message persistence, search, and context retention
  • Asynchronous Processing: Fast, scalable document and chat message embedding using BullMQ with Redis queue backend
  • Vector Search: Advanced semantic search using ChromaDB for document and message retrieval
  • Multiple AI Providers: Support for both Google Gemini and Anthropic Claude models
  • User Authentication: Secure JWT-based authentication system
  • GCP Secret Manager Integration: Secure management of secrets and credentials for cloud and production deployments
  • Real-time Streaming: Server-sent events for streaming AI responses
  • Docker Support: Complete containerized deployment with Docker Compose
  • Observability: Built-in OpenTelemetry integration with Zipkin tracing

πŸ—οΈ Architecture

πŸ›‘οΈ API Documentation & New Endpoints

The API now includes comprehensive support for threaded conversations (Chats) and chat messages (Messages) with full CRUD endpoints. All endpoints are documented with Swagger/OpenAPI.

  • Chats
    • Create, list, retrieve, update, and delete chat threads
    • Ask questions about documents in a chat context (with streamed responses)
  • Messages
    • Create and manage chat messages, with semantic search and embedding
    • Full message pagination and context retention
  • Swagger/OpenAPI: Full API documentation is available at /api when the server is running.

The application implements a modern RAG (Retrieval-Augmented Generation) architecture:

Indexing Pipeline

  1. Document Upload - PDF files are uploaded via REST API
  2. Text Extraction - Content is extracted using pdf-parse
  3. Text Chunking - Documents are split into semantic chunks using LangChain
  4. Embedding Generation - Text chunks are converted to vectors using AI models
  5. Vector Storage - Embeddings are stored in ChromaDB for fast retrieval

Query Pipeline

  1. Question Processing - User questions are embedded using the same AI model
  2. Semantic Search - ChromaDB finds the most relevant document chunks
  3. Context Construction - Retrieved chunks are combined with the question
  4. AI Generation - AI model generates answers based on the provided context
  5. Streaming Response - Answers are streamed back to the client in real-time

πŸ› οΈ Tech Stack

  • Framework: NestJS (Node.js)
  • Runtime: Bun
  • Database: PostgreSQL with TypeORM
  • Vector Database: ChromaDB
  • Queue & Worker: Redis + BullMQ (asynchronous tasks, message embedding)
  • AI Models: Google Gemini, Anthropic Claude
  • Authentication: JWT
  • Secret Management: GCP Secret Manager
  • Text Processing: LangChain, pdf-parse
  • Containerization: Docker & Docker Compose
  • Observability: OpenTelemetry, Zipkin
  • Language: TypeScript

πŸ“‹ Prerequisites

  • Bun (latest version)
  • Docker and Docker Compose
  • AI API Key (Google Gemini or Anthropic Claude)

πŸš€ Quick Start

1. Clone the Repository

git clone <repository-url>
cd ai-document-backend

2. Environment Setup

Copy the example environment file and configure your settings:

cp .env.example .env.development

Edit .env.development with your configuration:

# App
APP_ENV=development

# PostgreSQL
DB_HOST=db
DB_USER=aidocument
DB_PASS=password
DB_NAME=aidocument

# Redis
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASS=password

# AI_SERVICE_TYPE=anthropic
AI_SERVICE_TYPE=gemini
ANTHROPIC_API_KEY=your_anthropic_api_key_here
GEMINI_API_KEY=your_gemini_api_key_here

3. Install Dependencies

bun install

4. Start the Application

Development Mode (with hot reload):

bun run compose:dev

Production Mode:

bun run compose:prod

The application will be available at:

5. Stop the Application

# Development
bun run compose:down:dev

# Production
bun run compose:down:prod

πŸ§ͺ Development

Running Tests

# Unit tests
bun test

# End-to-end tests
bun run test:e2e

# Test coverage
bun run test:cov

Code Quality

# Lint code
bun run lint

# Format code
bun run format

Local Development (without Docker)

  1. Start PostgreSQL and ChromaDB separately
  2. Update environment variables to point to local services
  3. Run the application:
bun run start:dev

πŸ—οΈ Project Structure

src/
β”œβ”€β”€ ai/                 # AI service implementations (Gemini, Anthropic, etc)
β”œβ”€β”€ auth/               # Authentication and user management
β”œβ”€β”€ chats/              # Chat session and question handling (CRUD, SSE)
β”œβ”€β”€ documents/          # Document processing and Q&A
β”œβ”€β”€ embedding/          # Document/message embedding and vector search
β”œβ”€β”€ messages/           # Chat message CRUD, processing, BullMQ queue
β”œβ”€β”€ secret-manager/     # GCP Secret Manager integration
β”œβ”€β”€ users/              # User management
β”œβ”€β”€ shared/             # Shared utilities and interceptors
β”œβ”€β”€ app.module.ts       # Main application module
└── main.ts            # Application entry point

config/                 # Configuration files
doc/                   # Documentation
test/                  # End-to-end tests

πŸ”§ Configuration

Environment Variables

Variable Description Default
APP_ENV Application environment development
APP_PORT Server port 3000
DB_HOST PostgreSQL host db
DB_PORT PostgreSQL port 5432
DB_USER Database username -
DB_PASS Database password -
DB_NAME Database name -
REDIS_HOST Redis host redis
REDIS_PORT Redis port 6379
REDIS_PASS Redis password password
AI_SERVICE_TYPE AI provider (gemini or anthropic) gemini
GEMINI_API_KEY Google Gemini API key -
ANTHROPIC_API_KEY Anthropic Claude API key -

AI Service Configuration

The application supports multiple AI providers. Configure your preferred provider in the environment:

  • Google Gemini: Set AI_SERVICE_TYPE=gemini and provide GEMINI_API_KEY
  • Anthropic Claude: Set AI_SERVICE_TYPE=anthropic and provide ANTHROPIC_API_KEY

πŸ“Š Monitoring and Observability

The application includes comprehensive observability features:

  • Logging: Structured logging with request interceptors
  • Tracing: OpenTelemetry integration with Zipkin
  • Health Checks: Database and ChromaDB connectivity monitoring

Access Zipkin UI at http://localhost:9411 to view distributed traces.

πŸ“ Migration Notes

Important: The latest updates add new modules (Chats, Messages), BullMQ for queue processing, and Redis as a required backend for async tasks. Existing deployments should:

  • Run database migrations to add Chats and Messages tables (see /src/chats and /src/messages)
  • Ensure Redis is deployed and configured for BullMQ queues
  • Review .env for new variables (see above)
  • Populate GCP Secret Manager if using for secret management

πŸš€ Deployment

Production Deployment

  1. Configure production environment variables in .env.production
  2. Deploy using Docker Compose:
bun run compose:prod

Container Configuration

The application runs in a multi-container setup:

  • app: NestJS application
  • db: PostgreSQL database
  • vector_db: ChromaDB vector database
  • otel-collector: OpenTelemetry collector
  • zipkin: Distributed tracing UI

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘₯ Author

Created by yelaco

πŸ”— Related Documentation

About

RAG-powered Q&A backend for documents

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published