Skip to content

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.

Notifications You must be signed in to change notification settings

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

No packages published