Basjoo AI Customer Support Platform

repository·main·Indexed 41 days ago

https://github.com/haoyiyin/basjoo

An AI customer-support platform featuring a FastAPI backend, a Next.js admin dashboard, and an embeddable chat widget. Basjoo allows businesses to create AI agents powered by external LLMs that interact with website content and uploaded files using vector search via Qdrant.

Tokens
14K
Snippets
46
Records
63
Agent score
49%

What's inside Basjoo

  1. Understand the Basjoo Architecture

    main

    Basjoo is composed of three main layers: a FastAPI backend, a Next.js admin dashboard, and an embeddable widget.

    Backend

    Built with FastAPI, the backend manages:

    • Auth: Admin routes under /api/admin.
    • V1 APIs: Public endpoints for chat (/api/v1/chat), agent configuration, sessions, and quotas.
    • Admin Routers: Protected endpoints for URL ingestion, file uploads, and index rebuilding.
    • Core Domains: Agent configuration, knowledge sources (URLs/Files), indexing (via Qdrant), chat sessions, and scheduling.

    Frontend

    A Next.js application located in frontend-nextjs/ providing the admin dashboard and management interfaces.

    Widget

    A self-contained component (BasjooWidget.tsx) that can be embedded in any website. It automatically detects the apiBase, fetches public configuration, and handles streaming chat replies via SSE.

  2. Embed the Basjoo Chat Widget

    main

    The BasjooWidget is a self-contained component designed to be embedded in websites.

    Key Behaviors:

    • Auto-detection: It automatically detects the apiBase from the script's source URL. In development, it assumes the backend is on port 8000 if the frontend is on 3000, otherwise it falls back to window.location.origin.
    • Configuration: On initialization, it calls /api/v1/config:public to fetch the default_agent_id, widget title, colors, and welcome message.
    • Session Management: It persists the visitor/session ID in localStorage.
    • Streaming: It uses Server-Sent Events (SSE) via /api/v1/chat/stream for real-time replies, with a 90-second timeout and automatic retry on network errors.
    • Security: The backend enforces a source whitelist configured per agent. Only administrators can bypass this whitelist.

    To use it, include the provided /sdk.js from your Basjoo backend instance.

  3. Install and deploy Basjoo automatically

    main

    For a fresh Ubuntu or Debian server, you can perform an automatic deployment using a single command. This script handles the setup of the necessary Docker containers.

    Automatic deployment on a blank server:

    curl -fsSL https://raw.githubusercontent.com/haoyiyin/basjoo/main/install-deploy.sh | sudo sh

    Deployment from a local repository checkout: If you have already cloned the repository locally, run the script from the root directory:

    sudo sh install-deploy.sh

    After completion, the script will provide the Admin Dashboard URL. The first user to register an account on the new deployment will be designated as the workspace super administrator.

    curl -fsSL https://raw.githubusercontent.com/haoyiyin/basjoo/main/install-deploy.sh | sudo sh
  4. Preserve widget embeds during redeployment or migration

    main

    To ensure existing widget embeds continue working after a redeployment or server migration, you must preserve the backend data volume (mounted at /app/data) and the specific agent ID.

    1. Back up /app/data/basjoo.db and /app/data/.agent_id.
    2. Preserve the volume mounted at /app/data.
    3. If migrating to a new server and you know the old agentId, set the DEFAULT_AGENT_ID environment variable before the first boot.

    Migration Configuration

    Use the following environment variable pattern in your .env file:

    SECRET_KEY=
    DEFAULT_AGENT_ID=agt_123456789abc

    Warning: If the data volume is lost and the agentId is unknown, existing widget embeds cannot be recovered automatically.

    # Example .env snippet for migration
    SECRET_KEY=
    DEFAULT_AGENT_ID=agt_123456789abc
  5. Run Basjoo services locally without Docker

    main

    If you prefer to run the components directly on your host machine, follow these steps for each service.

    Backend (FastAPI):

    cd backend
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    python3 main.py

    Check backend health with: curl http://localhost:8000/health

    Frontend (Next.js):

    cd frontend-nextjs
    npm install
    npm run dev

    Widget:

    cd widget
    npm install
    npm run dev

    Default Dev Ports:

    • Frontend: http://localhost:3000
    • Backend API: http://localhost:8000
    • Qdrant: http://localhost:6333
    • PostgreSQL: 127.0.0.1:5432
    • Redis: 127.0.0.1:6379
    cd backend
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    python3 main.py
  6. Run Basjoo E2E tests

    main

    To verify the system using End-to-End (E2E) tests, you can run the typechecker or the test suites using npm run. Note that some tests require API keys for providers like DeepSeek or Jina.

    Standard E2E Tests

    Run the full E2E suite by providing the necessary API keys via environment variables:

    E2E_API_KEY=$DEEPSEEK_API_KEY E2E_JINA_API_KEY=$JINA_API_KEY npm run test:e2e

    Widget E2E Tests

    To test widget functionality across different origins, use the test:e2e:widget command with HOST_ALLOWED_URL and HOST_BLOCKED_URL defined:

    HOST_ALLOWED_URL=http://localhost:8080 HOST_BLOCKED_URL=http://localhost:8081 API_BASE_URL=http://localhost:8000 npm run test:e2e:widget

    Production-like E2E Tests

    To run tests against a production-like environment, use the prod profile with Docker and set the E2E_ENV to prod:

    docker compose --profile prod up -d --build backend-prod frontend-prod nginx
    E2E_ENV=prod API_BASE_URL=http://localhost E2E_API_KEY=$DEEPSEEK_API_KEY E2E_JINA_API_KEY=$JINA_API_KEY npm run test:e2e:prod
  7. Run Basjoo services locally without Docker

    main

    You can run the backend, frontend, and widget components separately in a local development environment.

    # 1. Backend setup
    cd backend
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    python3 main.py
    
    # Check health: curl http://localhost:8000/health
    
    # 2. Frontend setup
    cd frontend-nextjs
    npm install
    npm run dev
    
    # 3. Widget setup
    cd widget
    npm install
    npm run dev
  8. Run Backend Tests

    main

    Backend tests are located in backend/tests/. The test environment uses BASJOO_TEST_MODE=1 and an isolated SQLite database in backend/.pytest_dbs/.

    Commands:

    Run all tests:

    cd backend
    pytest

    Run a specific test file:

    pytest tests/test_api.py

    Run a specific test case:

    pytest tests/test_api.py::test_name
    cd backend
    pytest
  9. Deploy Basjoo using Docker Compose

    main

    Basjoo can be deployed using Docker Compose profiles depending on your environment.

    Development stack: Use this for local development with permissive CORS and local API URLs.

    docker compose --profile dev up -d

    Production-style stack: Use this for stable, production-like environments. The production profile expects persistent backend data to be mounted under /app/data and uses SERVER_DOMAIN to enforce canonical host access via nginx.

    docker compose --profile prod up -d

    Useful Docker commands:

    • View logs: docker compose logs -f backend-dev frontend-dev nginx
    • Rebuild specific services: docker compose --profile dev up -d --build backend-dev frontend-dev
    • Run stability check: bash scripts/prod_stability_check.sh
    docker compose --profile dev up -d
  10. Deploy Basjoo via Docker or Script

    main

    Basjoo can be deployed using Docker Compose or a specialized deployment script.

    One-click Deployment (Ubuntu/Debian): Use the install-deploy.sh script to automatically install Docker/Compose, clone the repository, sync branches, and initialize .env files.

    Docker Compose: Use docker-compose.yml as the primary orchestration entry point.

    Nginx Configuration Notes:

    • client_max_body_size is set to 12m to allow large requests to reach the backend for proper JSON error handling.
    • HTTPS is enabled automatically if readable certificates/keys exist in the ./ssl directory. When enabled, HTTP requests on port 80 are redirected to 443.
    • You can set SERVER_DOMAIN to restrict Nginx to respond only to a specific domain; other hosts will receive a 444 response (except for /health).
  11. Deploy Basjoo using the one-command installer

    main

    For Ubuntu and Debian systems, you can use the install-deploy.sh script to perform a production installation. This script automates:

    • Installing Docker and Docker Compose.
    • Cloning the repository.
    • Syncing the repository to the chosen remote branch.
    • Deploying the application via Docker Compose.
    ./install-deploy.sh
  12. Run backend tests with pytest

    main

    Basjoo uses pytest for backend testing. When running tests, BASJOO_TEST_MODE=1 is set, and isolated SQLite databases are used under backend/.pytest_dbs/ to prevent interference with production data.

    # Run all tests
    cd backend
    pytest
    
    # Run a specific file
    pytest tests/test_api.py
    
    # Run a single test case
    pytest tests/test_api.py::test_name