A production-grade multi-tenant AI analytics platform for marketing analytics featuring Retrieval-Augmented Generation (RAG) capabilities.
This project is an AI-powered analytics platform designed for marketing analytics, specifically focused on Key Opinion Leader (KOL) and content performance analysis. It features a three-stage AI pipeline (Planner → Executor → Analyst) that enables natural language querying of marketing data using RAG (Retrieval-Augmented Generation) technology.
- Natural Language Querying: Ask questions about your marketing data in plain language
- KOL Analytics: Track and analyze Key Opinion Leader performance across multiple social media platforms
- Content Analysis: Analyze posts, captions, hashtags, and engagement metrics
- Semantic Search: Vector-based semantic search using pgvector for finding relevant content
- AI-Powered Insights: Get intelligent recommendations and performance metrics through LLM integration
- Multi-Platform Support: Works with Instagram, Threads, Reels, and other social media platforms
- NestJS - Progressive Node.js framework for building efficient server-side applications
- TypeScript - Type-safe JavaScript
- PostgreSQL - Relational database
- Prisma - Next-generation ORM with type safety
- pgvector - PostgreSQL extension for vector similarity search
- LangChain - Framework for building LLM applications
- OpenRouter - Unified API for accessing multiple LLM providers
- Ollama - Local LLM support for embeddings
- Swagger/OpenAPI - Interactive API documentation
- Zod - TypeScript-first schema validation
- class-validator - Decorator-based validation
- class-transformer - Object transformation
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher)
- pnpm (package manager)
- PostgreSQL (v14 or higher) with pgvector extension
- Git
git clone https://github.com/juniardys/rag-experiments
cd rag-experimentspnpm installCopy the .env.example file to .env:
cp .env.example .envThen edit .env and fill in your configuration values. See Environment Variables section below for details.
# Connect to PostgreSQL
psql -U postgres
# Create database
CREATE DATABASE rag_experiments;
# Connect to the database
\c rag_experimentsThe application uses pgvector for vector similarity search. Enable it in your database:
CREATE EXTENSION IF NOT EXISTS vector;You can verify the extension is installed:
\dxpnpm run prisma:generateThis will create all the necessary tables in your database:
pnpm run prisma:migratePopulate the database with sample data:
pnpm run prisma:seedpnpm run start:dev# Build the application
pnpm run build
# Run in production mode
pnpm run start:prodThe application will be available at:
- API: http://localhost:3000
- Swagger Documentation: http://localhost:3000/api
Create a .env file in the root directory with the following variables:
# Database
DATABASE_URL="postgresql://user:password@localhost:5432/rag_experiments?schema=public"
# OpenRouter API (for LLM access)
OPENROUTER_API_KEY="your-openrouter-api-key"# Server Configuration
PORT=3000
CORS_ORIGIN="*"
# LLM Model Configuration
OPENROUTER_MODEL="openai/gpt-4o-mini"
EMBEDDING_MODEL="openai/text-embedding-3-small"
TOOL_CALLING_MODEL="nex-agi/deepseek-v3.1-nex-n1:free"
# LangSmith Tracing (Optional - for observability)
LANGSMITH_TRACING="false"
LANGSMITH_ENDPOINT=""
LANGSMITH_API_KEY=""
LANGSMITH_PROJECT=""- Visit OpenRouter
- Sign up for an account
- Navigate to your API keys section
- Create a new API key
- Add it to your
.envfile
rag-experiments/
├── prisma/
│ ├── schema.prisma # Database schema
│ ├── seed.ts # Database seeding script
│ └── generated/ # Generated Prisma client
├── src/
│ ├── ai-marketing/ # AI marketing service
│ ├── chat/ # Chat/query endpoints
│ ├── config/ # Configuration service
│ ├── database/ # Database module (Prisma)
│ ├── executor/ # Executor service
│ ├── tools/ # AI tools (KOL recommendation, post analysis, etc.)
│ └── main.ts # Application entry point
├── test/ # E2E tests
└── .env.example # Environment variables template
# Development
pnpm run start:dev # Start in watch mode
pnpm run start:debug # Start in debug mode
# Production
pnpm run build # Build the application
pnpm run start:prod # Start in production mode
# Database
pnpm run prisma:generate # Generate Prisma client
pnpm run prisma:migrate # Run database migrations
pnpm run prisma:seed # Seed the database
# Code Quality
pnpm run lint # Run ESLint
pnpm run format # Format code with Prettier
# Testing
pnpm run test # Run unit tests
pnpm run test:watch # Run tests in watch mode
pnpm run test:cov # Run tests with coverage
pnpm run test:e2e # Run end-to-end testsOnce the application is running, visit http://localhost:3000/api to access the interactive Swagger documentation. Here you can:
- Explore all available endpoints
- Test API calls directly from the browser
- View request/response schemas
The application uses the following main models:
- User: Represents users of the platform
- Kol: Key Opinion Leaders with their social media profiles
- Post: Social media posts with embeddings for semantic search
Send a POST request to /chat:
curl -X POST http://localhost:3000/chat \
-H "Content-Type: application/json" \
-d '{
"query": "Which KOLs performed best this week?",
"userId": "user-id-here"
}'The AI will automatically:
- Understand your query
- Select appropriate tools (KOL recommendation, post analysis, performance metrics)
- Execute queries against the database
- Return intelligent insights
- Ensure PostgreSQL is running
- Verify
DATABASE_URLin.envis correct - Check that the database exists and pgvector extension is enabled
- Run
pnpm run prisma:generateafter schema changes - Run
pnpm run prisma:migrateto apply migrations - Check Prisma logs for detailed error messages
- Verify your OpenRouter API key is valid
- Check that you have sufficient credits/quota
- Ensure the API key is correctly set in
.env
Contributions are welcome! Please feel free to submit a Pull Request.
This project is MIT licensed.