Skip to content

3idey/ai-support-bot

Repository files navigation

AI Support Bot

A scalable AI Support Assistant built with Laravel 12 with 29 production-ready API endpoints.

πŸš€ Features

  • Multi-Format Document Processing: PDF, DOCX, TXT with parallel batch processing
  • Intelligent Vector Search: OpenAI embeddings with automatic storage optimization (JSON/pgvector/Qdrant)
  • Conversation Memory: Context-aware follow-up questions with streaming responses (SSE)
  • Quota Management: Track usage, messages, and token consumption
  • Multi-Tenant Workspaces: Isolated environments with filtering
  • Production Ready: Laravel Sanctum auth, Redis queues, rate limiting

πŸ—οΈ Tech Stack

  • Framework: Laravel 12 (PHP 8.4)
  • AI Model: GPT-4o-mini (chat completions)
  • Embeddings: text-embedding-3-small (1536 dimensions)
  • Database: MariaDB/MySQL (JSON vector storage) or PostgreSQL (pgvector support)
  • Cache & Queue: Redis (Required for batch processing)
  • Authentication: Laravel Sanctum with API tokens
  • Vector Storage: Auto-detecting (JSON/pgvector/Qdrant)

Installation

  1. Clone & Install

    git clone https://github.com/3idey/ai-support-bot.git
    cd ai-support-bot
    composer install
  2. Environment Setup

    cp .env.example .env
    php artisan key:generate

    Configure your .env file:

    OPENAI_API_KEY=sk-...
    DB_CONNECTION=mariadb
    QUEUE_CONNECTION=redis
    CACHE_DRIVER=redis
  3. Database & Migration

    touch database/database.sqlite # If using SQLite
    php artisan migrate
  4. Start Services Ensure Redis is running:

    redis-server

    Start the queue worker (Critical for batch processing):

    php artisan queue:work

    Start the dev server:

    php artisan serve

API Documentation

Authentication

All API endpoints require a Bearer token:

Authorization: Bearer <your-token>

Generate a token: php artisan tinker β†’ User::first()->createToken('dev')->plainTextToken


🎯 Quota Management (8 Endpoints)

Monitor and track user usage limits.

Check Full Quota Status

curl http://localhost:8000/api/quota \
  -H "Authorization: Bearer <token>"

Response:

{
  "message_quota": { "total": 50, "used": 15, "remaining": 35, "percentage": 30 },
  "token_quota": { "total": 100000, "used": 12450, "remaining": 87550, "percentage": 12.45 },
  "document_quota": { "total": 100, "used": 5, "remaining": 95, "percentage": 5 }
}

Other Quota Endpoints

  • GET /api/quota/check - Simple boolean check
  • GET /api/quota/remaining - Get remaining messages
  • GET /api/quota/tokens - Get remaining tokens
  • GET /api/quota/usage - Detailed usage breakdown
  • POST /api/quota/track - Manually track token usage
  • GET /api/quota/documents - Check document quota
  • POST /api/quota/documents/validate - Validate document size

πŸ—‚οΈ Workspace Management (7 Endpoints)

Multi-tenant workspace isolation and filtering.

List Workspaces

curl http://localhost:8000/api/workspaces \
  -H "Authorization: Bearer <token>"

Create Workspace

curl -X POST http://localhost:8000/api/workspaces \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"name": "Customer Support", "description": "Support documentation"}'

Other Workspace Endpoints

  • GET /api/workspaces/{id} - Show workspace details
  • PUT /api/workspaces/{id} - Update workspace
  • DELETE /api/workspaces/{id} - Delete workspace
  • GET /api/workspaces/{id}/documents - List workspace documents
  • GET /api/workspaces/{id}/conversations - List workspace conversations

πŸ’¬ Conversation Management (4 Endpoints)

Manage chat history and conversations.

List Conversations

curl http://localhost:8000/api/conversations?workspace_id=1 \
  -H "Authorization: Bearer <token>"

Get Conversation Messages

curl http://localhost:8000/api/conversations/1/messages \
  -H "Authorization: Bearer <token>"

Other Conversation Endpoints

  • GET /api/conversations/{id} - Show conversation details
  • DELETE /api/conversations/{id} - Delete conversation

πŸ“„ Document Management (4 Endpoints)

Upload and manage knowledge base documents with filtering.

List Documents (with filtering)

curl http://localhost:8000/api/documents?workspace_id=1 \
  -H "Authorization: Bearer <token>"

Upload Document

curl -X POST http://localhost:8000/api/documents \
  -H "Authorization: Bearer <token>" \
  -F "[email protected]" \
  -F "workspace_id=1"

Other Document Endpoints

  • GET /api/documents/{id} - Show document details
  • DELETE /api/documents/{id} - Delete document

πŸ€– Chat Endpoints (1 Endpoint)

Ask Question (JSON Response)

curl -X POST http://localhost:8000/api/ask \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "question": "How do I reset my password?",
    "workspace_id": 1
  }'

Ask with Streaming + Memory

curl -N -X POST http://localhost:8000/api/ask \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "question": "Tell me more about that.",
    "conversation_id": 1,
    "stream": true
  }'

Stream Events:

  • event: sources - Related document chunks
  • event: conversation_id - ID for follow-ups
  • data: {"content": "..."} - Answer tokens
  • data: [DONE] - Stream complete

πŸ“Š Vector Database Management (4 Endpoints)

Intelligent vector storage with automatic optimization recommendations.

Check Vector Database Status

curl http://localhost:8000/api/vectors/status \
  -H "Authorization: Bearer <token>"

Response:

{
  "system": "mariadb_json",
  "status": "active",
  "total_embeddings": 150,
  "avg_dimensions": 1536,
  "database": "MariaDB 11.6.2",
  "storage_method": "JSON columns with PHP cosine similarity"
}

Get Optimization Recommendations

curl http://localhost:8000/api/vectors/recommendations \
  -H "Authorization: Bearer <token>"

Performance Benchmark

curl -X POST http://localhost:8000/api/vectors/benchmark \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"sample_size": 10}'

Migrate to pgvector (PostgreSQL only)

curl -X POST http://localhost:8000/api/vectors/migrate \
  -H "Authorization: Bearer <token>"

πŸ₯ Health Check

curl http://localhost:8000/api/health

πŸš€ Performance & Scalability

Benchmarks

  • Vector Search: ~30ms (JSON), ~5ms (pgvector), ~1ms (Qdrant)
  • API Response: <100ms average
  • Scaling: Stateless design with horizontal scaling support

πŸ“„ License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages