Skip to content

AmitxParmar/ai-agent

Repository files navigation

AI Agent - Multi-Agent RAG System

A sophisticated AI-powered chat application built with Next.js, Mastra framework, and Supabase that implements a multi-agent workflow for intelligent document retrieval and response generation.

πŸš€ Features

  • Multi-Agent Workflow: Orchestrated AI agents for query classification, document retrieval, and response generation
  • RAG (Retrieval Augmented Generation): Semantic search through knowledge base using vector embeddings
  • Real-time Chat Interface: Modern, responsive chat UI with streaming responses
  • Authentication: Secure user authentication with Supabase Auth
  • Vector Search: Advanced document similarity matching with configurable thresholds
  • Multiple AI Providers: Support for Google Gemini, OpenAI, and Mistral models

πŸ“ Project Structure

β”œβ”€β”€ app/                          # Next.js app directory
β”‚   β”œβ”€β”€ (auth)/                   # Authentication routes (login, callback)
β”‚   β”œβ”€β”€ api/                      # API routes and endpoints
β”‚   β”œβ”€β”€ globals.css               # Global styles
β”‚   β”œβ”€β”€ layout.tsx                # Root layout component
β”‚   └── page.tsx                  # Home page component
β”œβ”€β”€ components/                   # Reusable React components
β”‚   β”œβ”€β”€ chat/                     # Chat-related components (interface, message bar)
β”‚   β”œβ”€β”€ common/                   # Shared UI components (sidebar)
β”‚   └── ProtectedRoute.tsx        # Route protection wrapper
β”œβ”€β”€ hooks/                        # Custom React hooks
β”‚   └── useAuth.ts                # Authentication hook
β”œβ”€β”€ lib/                          # Core library code
β”‚   β”œβ”€β”€ mastra/                   # Mastra framework configuration
β”‚   β”‚   β”œβ”€β”€ agents/               # AI agents (classifier, RAG, responder)
β”‚   β”‚   β”œβ”€β”€ tools/                # Custom tools (RAG tool for document retrieval)
β”‚   β”‚   β”œβ”€β”€ workflow/             # Multi-agent workflow orchestration
β”‚   β”‚   └── index.ts              # Mastra instance configuration
β”‚   β”œβ”€β”€ supabase.ts               # Supabase client configuration
β”‚   └── utils.ts                  # Utility functions
β”œβ”€β”€ scripts/                      # Database and testing scripts
β”‚   β”œβ”€β”€ check-database.ts         # Database connectivity verification
β”‚   β”œβ”€β”€ debug-embeddings.ts       # Embedding debugging utilities
β”‚   β”œβ”€β”€ populate-embeddings.ts    # Knowledge base seeding script
β”‚   └── test-rag-tool.ts          # RAG functionality testing
β”œβ”€β”€ services/                     # Business logic services
β”‚   └── conversation.services.ts  # Conversation management
β”œβ”€β”€ supabase/                     # Database schema and migrations
β”‚   β”œβ”€β”€ complete-setup.sql        # Complete database setup
β”‚   β”œβ”€β”€ seed.sql                  # Sample data seeding
β”‚   └── *.sql                     # Various SQL scripts
β”œβ”€β”€ .env.local                    # Environment variables
β”œβ”€β”€ package.json                  # Dependencies and scripts
└── README.md                     # Project documentation

πŸ› οΈ Tech Stack

  • Frontend: Next.js 15, React 19, TypeScript, Tailwind CSS
  • Backend: Hono API framework, Vercel Edge Runtime
  • AI Framework: Mastra Core with multi-agent workflows
  • Database: Supabase (PostgreSQL with vector extensions)
  • AI Models: Google Gemini, OpenAI, Mistral
  • Authentication: Supabase Auth
  • Styling: Tailwind CSS with custom components

πŸ“‹ Prerequisites

  • Node.js 18+ and npm/pnpm
  • Supabase account and project
  • Google AI API key (for Gemini models)
  • Optional: OpenAI or Mistral API keys

πŸš€ Getting Started

1. Clone and Install

git clone <repository-url>
cd ai-agent
npm install

2. Environment Setup

Create .env.local file:

# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key

# AI Provider Keys
GOOGLE_GENERATIVE_AI_API_KEY=your_google_ai_key
OPENAI_API_KEY=your_openai_key (optional)
MISTRAL_API_KEY=your_mistral_key (optional)

# Database
DATABASE_URL=your_supabase_database_url

3. Database Setup

Run the database setup scripts:

# Execute the SQL files in your Supabase SQL editor
# 1. supabase/complete-setup.sql
# 2. supabase/seed.sql (optional sample data)

4. Populate Knowledge Base

npm run seed:embeddings

5. Development

# Start Next.js development server
npm run dev

# Start Mastra development server (in another terminal)
npm run dev:mastra

Visit http://localhost:3000 for the application and http://localhost:4111 for Mastra playground.

πŸ”§ Available Scripts

  • npm run dev - Start Next.js development server
  • npm run build - Build production application
  • npm run start - Start production server
  • npm run dev:mastra - Start Mastra development server
  • npm run build:mastra - Build Mastra workflows
  • npm run seed:embeddings - Populate knowledge base with embeddings
  • npm run verify:setup - Verify database and API setup
  • npm run test:rag - Test RAG functionality
  • npm run check:database - Check database connectivity

πŸ€– How It Works

Multi-Agent Workflow

  1. Query Classification Agent: Analyzes user queries to determine intent and category
  2. RAG Agent: Retrieves relevant documents from the knowledge base using semantic search
  3. Responder Agent: Generates contextual responses based on retrieved information

RAG Implementation

  • Documents are chunked and embedded using Google's text-embedding-004 model
  • Vector similarity search with configurable thresholds (0.6-0.8)
  • Supports category filtering and result count limits
  • Real-time document retrieval with similarity scoring

Authentication Flow

  • Supabase Auth integration with email/password and social providers
  • Protected routes with automatic redirection
  • Session management with React hooks

πŸ”’ Security Features

  • Row Level Security (RLS) policies in Supabase
  • API route protection with authentication middleware
  • Environment variable validation
  • Secure token handling

πŸ“š Key Components

  • Chat Interface: Real-time messaging with streaming responses
  • RAG Tool: Semantic document retrieval with vector search
  • Multi-Agent System: Orchestrated AI workflow for complex queries
  • Knowledge Base: Vector-enabled document storage and retrieval
  • Authentication: Secure user management and session handling

πŸš€ Deployment

The application is optimized for Vercel deployment with Edge Runtime support:

npm run build
# Deploy to Vercel or your preferred platform

🀝 Contributing

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

πŸ“„ License

This project is licensed under the MIT License.

About

A knowledge-driven AI agent powered by RAG, Hono, and Supabase, with Mistral AI as the reasoning engine. Designed for multi-step question answering, it dynamically retrieves and synthesizes information from structured and unstructured data sources. Built for accuracy, scalability, and real-time knowledge interaction.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors