# mudasir345/perplexityai-clone architecture

> Architecture diagram of the GitHub repository mudasir345/perplexityai-clone, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mudasir345/perplexityai-clone
- Repository: https://github.com/mudasir345/perplexityai-clone

## Overview

##### Project Type and Purpose
- This is a full-stack, AI-powered search engine inspired by Perplexity AI.
- It incorporates multiple search modes (normal search, academic, image, video, Reddit, Wolfram Alpha, etc.) along with advanced features like similarity search and support for local LLMs (for example, via Ollama).
- Its goal is to enable both traditional web search and intelligent querying with AI-driven query enhancements.

##### File Structure to Define Main Components
- Backend (located in the "src" directory):
- Contains separate subdirectories for:
∙ Agents: Individual search strategy implementations (e.g., academicSearchAgent.ts, imageSearchAgent.ts, etc.) that abstract specific search functionalities.
∙ Routes: API endpoints (e.g., routes for chats, configs, models, search, suggestions, etc.) that define how external clients can interact with the backend.
∙ Database: Components under src/db that include schema definitions and database connection logic.
∙ Websocket: Modules managing real-time communication (connectionManager.ts, websocketServer.ts, messageHandler.ts) to support interactive features.
∙ Libraries/Utils: General-purpose libraries (e.g., providers for external APIs like OpenAI or Anthropic, output parsers, similarity computations, etc.) that encapsulate business logic.
- Frontend (located in the "ui" directory):
- Built as a Next.js application (notice the "page.tsx", Next config, tailwind.config.ts, and other UI components).
- Contains components and libraries dealing with authentication, chat interface, search capabilities, and theme management.
- Infrastructure and Deployment:
- Multiple Dockerfiles (app.dockerfile and backend.dockerfile), and a docker-compose.yaml file imply container-based deployment.
- Documentation in the “docs” directory provides additional architectural, API, and installation insights.
- External Tools/Services:
- SearxNG (configuration in the “searxng” directory) as the metasearch engine is a core component.
- Various API integrations with services like OpenAI, Ollama, Anthropic, and Groq manage the AI and search elements.

##### Defining Relationships and Interactions Between Components
- Frontend (Next.js UI):
- Acts as the client interface.
- Interacts with backend API endpoints (via REST or WebSocket) to send search queries and receive responses.
- Backend API:
- Orchestrates search functionalities by routing queries through various “agents” based on the selected mode (normal, academic, image, etc.).
- Communicates with the SearxNG metasearch engine to obtain aggregated and re-ranked results.
- Implements real-time updates and notifications through the WebSocket module.
- Handles user configuration and state management.
- Database:
- Stores historical data, user settings, and search indices or schema required by the app.
- External Integrations:
- Providers in src/lib are in charge of interfacing with third-party services (for example, local LLM connections via Ollama, or querying OpenAI’s APIs).
- Deployment Infrastructure:
- Docker, Docker Compose, and associated configuration files ensure the system is containerized and smoothly deployed to various environments.

##### Important Architectural Patterns and Design Principles
- Modular Design: Separation of concerns between functionalities (agents, routes, DB, websocket, UI).
- Microservices-ish separation: Even if all components are in one repository, having a clear distinction between frontend and backend (and even further, separation of search agents) resembles a microservices architecture.
- Containerization: Use of Docker for isolation and deployment.
- Real-Time Communication: The inclusion of WebSocket modules demonstrates a need for immediate, event-driven data flow (e.g., live chat or dynamic search suggestions).
- Extensible Integration: Interface modules for external providers hint at extensibility and straightforward integration of multiple AI and search services.

- UI (Frontend/Next.js)
- Backend API (with subcomponents such as Agents, Routes, Websocket Manager, DB interface)
- External Services (SearxNG, and individual 3rd party provider integrations like OpenAI, Ollama, Anthropic, etc.)
- Database module
- Infrastructure Layer (Docker, Docker Compose)
- UI to API endpoints (REST/WebSocket calls)
- API to external search services and LLM providers
- API to DB interactions for logging/search history
- Notes on key design patterns (e.g., "modular agents", "real-time WebSocket communication") close to each relevant component.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        UI["Frontend UI (Next.js)"]:::ui
    end

    %% Application Layer
    subgraph "Application Layer"
        Backend["Backend API Server"]:::backend
        Agents["Search Agents"]:::module
        Routes["API Routes"]:::module
        Websocket["WebSocket Module"]:::module
        Providers["External Providers Integration"]:::module
        SearxNG["SearxNG Integration"]:::module
    end

    %% Data Layer
    subgraph "Data Layer"
        DB["Database Module"]:::db
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        Deployment["Containerization & Deployment"]:::infra
        Docs["Documentation & Architectural Insights"]:::infra
    end

    %% Connections between Layers
    UI -->|"REST/WebSocket"| Backend
    Backend -->|"uses"| Agents
    Backend -->|"uses"| Routes
    Backend -->|"manages"| Websocket
    Backend -->|"calls"| Providers
    Backend -->|"integrates"| SearxNG
    Backend -->|"stores/fetches"| DB

    UI -->|"deployed in"| Deployment
    Backend -->|"deployed in"| Deployment
    DB -->|"managed by"| Deployment
    Deployment -->|"see details in"| Docs

    %% Class definitions for styling
    classDef ui fill:#adebad,stroke:#0c8033,stroke-width:2px;
    classDef backend fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef module fill:#d0e7ff,stroke:#1e90ff,stroke-width:2px;
    classDef db fill:#ffe4b5,stroke:#ff8c00,stroke-width:2px;
    classDef infra fill:#fdfd96,stroke:#cccc00,stroke-width:2px;

    %% Click Events
    click UI "https://github.com/mudasir345/perplexityai-clone/tree/main/ui/"
    click Backend "https://github.com/mudasir345/perplexityai-clone/blob/main/src/app.ts"
    click Agents "https://github.com/mudasir345/perplexityai-clone/tree/main/src/agents/"
    click Routes "https://github.com/mudasir345/perplexityai-clone/tree/main/src/routes/"
    click DB "https://github.com/mudasir345/perplexityai-clone/tree/main/src/db/"
    click Websocket "https://github.com/mudasir345/perplexityai-clone/tree/main/src/websocket/"
    click Providers "https://github.com/mudasir345/perplexityai-clone/tree/main/src/lib/providers/"
    click SearxNG "https://github.com/mudasir345/perplexityai-clone/blob/main/src/lib/searxng.ts"
    click Deployment "https://github.com/mudasir345/perplexityai-clone/blob/main/docker-compose.yaml"
    click Docs "https://github.com/mudasir345/perplexityai-clone/tree/main/docs/"
```
