Skip to content

dev-1603/multi-model-ai-gateway

Repository files navigation

AI Backend - Scalable NestJS API

A comprehensive NestJS backend that provides unified access to multiple AI models and third-party services including OpenAI, Anthropic Claude, Perplexity, Grok, Deepthink, and Notion integration.

πŸš€ Features

  • Multi-Provider AI Integration: Support for OpenAI, Anthropic Claude, Perplexity, Grok, and Deepthink
  • Notion Integration: Read, write, and AI-powered processing of Notion pages
  • Streaming Support: Real-time streaming responses for long AI outputs
  • Authentication: JWT-based authentication with per-user API key management
  • Rate Limiting: Built-in rate limiting and throttling
  • Comprehensive Logging: Structured logging with Winston
  • API Documentation: Auto-generated Swagger/OpenAPI documentation
  • Error Handling: Global exception handling and validation
  • Security: Helmet, CORS, and input validation

πŸ—οΈ Architecture

src/
β”œβ”€β”€ common/                 # Shared utilities and DTOs
β”‚   β”œβ”€β”€ dto/               # Data Transfer Objects
β”‚   β”œβ”€β”€ filters/           # Global exception filters
β”‚   β”œβ”€β”€ interceptors/      # Logging and other interceptors
β”‚   └── logger/            # Winston logger service
β”œβ”€β”€ modules/
β”‚   β”œβ”€β”€ auth/              # Authentication module
β”‚   β”œβ”€β”€ ai/                # AI providers integration
β”‚   β”‚   └── providers/     # Individual AI service providers
β”‚   β”œβ”€β”€ notion/            # Notion API integration
β”‚   └── health/            # Health check endpoints
└── main.ts                # Application bootstrap

πŸ› οΈ Installation

  1. Clone and install dependencies:
cd ai-backend
npm install
  1. Environment Setup:
cp env.example .env
  1. Configure environment variables:
# Application Configuration
NODE_ENV=development
PORT=3501
API_PREFIX=api/v1

# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-here
JWT_EXPIRES_IN=24h

# AI Provider API Keys
OPENAI_API_KEY=your-openai-api-key
ANTHROPIC_API_KEY=your-anthropic-api-key
PERPLEXITY_API_KEY=your-perplexity-api-key
NOTION_API_KEY=your-notion-api-key

# Optional: Grok API (if available)
GROK_API_KEY=your-grok-api-key
GROK_API_URL=https://api.grok.com/v1

# Optional: Deepthink API (if available)
DEEPTHINK_API_KEY=your-deepthink-api-key
DEEPTHINK_API_URL=https://api.deepthink.ai/v1

# Rate Limiting
THROTTLE_TTL=60
THROTTLE_LIMIT=100

# CORS Configuration
CORS_ORIGIN=http://localhost:3501,http://localhost:5173

# Logging
LOG_LEVEL=info
  1. Start the application:
# Development (runs on port 3501)
npm run start:dev

# Production (runs on port 6100)
npm run build
npm run start:prod

Docker Setup (Alternative)

# Development with Docker
docker-compose -f docker-compose.dev.yml up --build

# Production with Docker
docker-compose -f docker-compose.prod.yml up --build

# Or use the main docker-compose.yml
docker-compose up --build

πŸ“š API Documentation

Once the server is running, visit:

πŸ”‘ Authentication

Register a new user:

curl -X POST http://localhost:3501/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123",
    "name": "John Doe"
  }'

Login:

curl -X POST http://localhost:3501/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'

Add API keys for providers:

curl -X POST http://localhost:3501/api/v1/auth/api-keys \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "name": "OpenAI Key",
    "key": "sk-your-openai-key",
    "provider": "openai"
  }'

πŸ€– AI API Usage

Chat Completion:

curl -X POST http://localhost:3501/api/v1/ai/chat \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "provider": "openai",
    "model": "gpt-4",
    "messages": [
      {
        "role": "user",
        "content": "Hello, how are you?"
      }
    ],
    "maxTokens": 1000,
    "temperature": 0.7
  }'

Streaming Chat:

curl -X POST http://localhost:3501/api/v1/ai/chat/stream \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "provider": "anthropic",
    "model": "claude-3-sonnet-20240229",
    "messages": [
      {
        "role": "user",
        "content": "Write a short story about a robot"
      }
    ],
    "stream": true
  }'

Text Embedding:

curl -X POST http://localhost:3501/api/v1/ai/embedding \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "provider": "openai",
    "text": "This is a sample text for embedding",
    "model": "text-embedding-ada-002"
  }'

πŸ“ Notion Integration

Read a Notion page:

curl -X POST http://localhost:3501/api/v1/notion/query \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "id": "your-notion-page-id",
    "action": "read"
  }'

Summarize a Notion page with AI:

curl -X POST http://localhost:3501/api/v1/notion/summarize \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "pageId": "your-notion-page-id",
    "provider": "openai",
    "model": "gpt-4",
    "length": "medium"
  }'

Perform AI task on Notion content:

curl -X POST http://localhost:3501/api/v1/notion/ai-task \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "pageId": "your-notion-page-id",
    "task": "extract key insights and create action items",
    "provider": "anthropic",
    "model": "claude-3-sonnet-20240229"
  }'

πŸ”§ Available AI Providers

OpenAI

  • Models: gpt-4, gpt-4-turbo, gpt-3.5-turbo, gpt-4o, gpt-4o-mini
  • Features: Chat completion, embeddings, streaming
  • API Key: Required

Anthropic Claude

  • Models: claude-3-opus, claude-3-sonnet, claude-3-haiku, claude-3-5-sonnet
  • Features: Chat completion, streaming
  • API Key: Required

Perplexity

  • Models: llama-3.1-sonar variants
  • Features: Chat completion, streaming, web search
  • API Key: Required

Grok (Placeholder)

  • Models: grok-beta, grok-1
  • Features: Chat completion, streaming
  • API Key: Required (when available)

Deepthink (Placeholder)

  • Models: deepthink-1, deepthink-2
  • Features: Chat completion, streaming
  • API Key: Required (when available)

πŸ§ͺ Testing

# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov

πŸ“¦ Deployment

Docker (Recommended)

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
EXPOSE 6100
CMD ["node", "dist/main"]

Port Configuration

  • Development: Port 3501
  • Production: Port 6100
  • Docker: Exposes both ports for flexibility

Environment Variables for Production

  • Set NODE_ENV=production
  • Use strong JWT secrets
  • Configure proper CORS origins
  • Set up proper logging levels
  • Use environment-specific API keys

πŸ”’ Security Considerations

  1. API Keys: Store securely, never commit to version control
  2. JWT Secrets: Use strong, random secrets in production
  3. Rate Limiting: Configure appropriate limits for your use case
  4. CORS: Restrict origins to your frontend domains
  5. Input Validation: All inputs are validated using class-validator
  6. Error Handling: Sensitive information is not exposed in error messages

πŸš€ Frontend Integration

See the frontend-guides/ directory for specific integration examples:

  • React integration guide
  • Vue.js integration guide
  • Cursor IDE usage examples

πŸ“ˆ Monitoring and Logging

  • Structured Logging: Winston with JSON format
  • Request Logging: All requests logged with timing and status
  • Error Tracking: Comprehensive error logging with stack traces
  • Health Checks: Built-in health and readiness endpoints

🀝 Contributing

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

πŸ“„ License

MIT License - see LICENSE file for details.

πŸ†˜ Support

For issues and questions:

  1. Check the API documentation at /api/docs
  2. Review the logs for error details
  3. Ensure all required environment variables are set
  4. Verify API keys are valid and have proper permissions

About

A high-performance NestJS backend providing unified access to OpenAI, Claude, Perplexity, Grok, and DeepSeek. Features real-time streaming, Notion integration, JWT security, and automated Swagger documentation. πŸš€

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors