Key components
Perplexica’s architecture is organized into several key layers:User interface
A web-based UI that lets users chat, search, and view citations. Built with Next.js app router.- Components (
src/components): Reusable UI components - Pages and routes (
src/app): Next.js app directory structure- Home (
/) - Chat (
/c) - Discover (
/discover) - Library (
/library)
- Home (
API routes
Server endpoints implemented with Next.js route handlers insrc/app/api:
Chat API
POST /api/chat - Powers the interactive chat UI with streaming responsesSearch API
POST /api/search - Provides a programmatic search endpoint for integrationsProviders API
GET /api/providers - Lists available AI providers and model configurationsMedia APIs
POST /api/images and POST /api/videos - Dedicated endpoints for visual searchAgents and orchestration
The core search intelligence lives insrc/lib/agents/search:
Classification
The system first classifies the question using
classifier.ts:- Determines if research is needed
- Decides which widgets should run
- Rewrites the query into a standalone form
src/lib/agents/search/classifier.ts:37Parallel execution
Research and widgets run in parallel for optimal performance:
- Research: Gathers information from enabled sources
- Widgets: Provides structured data (weather, stocks, calculations)
src/lib/agents/search/index.ts:61-95Research system
The researcher (src/lib/agents/search/researcher/) gathers information using multiple tools:
Available research tools
Available research tools
Research tools are located in
src/lib/agents/search/researcher/actions/:- Web search (
webSearch.ts): General web results via SearXNG - Academic search (
academicSearch.ts): Scholarly papers and research - Social search (
socialSearch.ts): Discussion forums and social platforms - Upload search (
uploadsSearch.ts): Semantic search over user-uploaded files - URL scraping (
scrapeURL.ts): Direct content extraction from specific URLs
src/lib/agents/search/researcher/actions/index.ts.Widgets
Widgets provide structured, real-time data and run in parallel with research:- Weather widget (
src/lib/agents/search/widgets/weatherWidget.ts): Current conditions and forecasts - Stock widget (
src/lib/agents/search/widgets/stockWidget.ts): Market data and prices - Calculation widget (
src/lib/agents/search/widgets/calculationWidget.ts): Mathematical expressions
src/lib/agents/search/widgets/executor.ts.
Widget results are displayed in the UI but are not cited as sources. They provide helpful context that the model can reference when generating answers.
Search backend
A meta-search backend fetches relevant web results when research is enabled:- SearXNG integration (
src/lib/searxng.ts): Privacy-focused meta-search - Requires JSON format enabled
- Wolfram Alpha engine should be enabled for calculations
LLMs (Large language models)
Used throughout the system for:- Classification: Analyzing queries and planning responses
- Answer generation: Writing coherent, cited responses
- Query rewriting: Creating standalone search queries
- Providers are defined in
src/lib/models/providers/ - Model registry:
src/lib/models/registry.ts - Supports local (Ollama) and cloud providers (OpenAI, Anthropic, Groq, etc.)
Embedding models
Used for semantic search over user-uploaded files:- Enable searching PDFs, text files, and images by meaning
- Integrated with the uploads search action
- Provider-agnostic architecture
Storage
Chats and messages are persisted so conversations can be reloaded:- Database:
src/lib/db/ - Schema:
src/lib/db/schema.ts - Migrations run automatically on startup
- Stores chat history, messages, and response blocks
Data flow
Here’s how data flows through the system when you ask a question:- User sends message →
POST /api/chat - Classification → Determines research needs and widget relevance
- Parallel execution:
- Researcher gathers information from enabled sources
- Widgets fetch structured data
- Context assembly → Search results and widget data combined
- Answer generation → LLM streams response with citations
- Storage → Message and response saved to database
For detailed workflow and step-by-step execution, see the How it works guide.
Prompt templates
Prompt engineering is centralized insrc/lib/prompts/:
- Classifier prompt: Guides the classification step
- Writer prompt: Controls answer generation and citation format
- Research prompts: Direct tool usage during research
- Optimization mode (speed, balanced, quality)
- System instructions (user-defined)
- Available context (search results, widgets)
Configuration
Perplexica uses a flexible configuration system:- Setup screen: First-run configuration at http://localhost:3000
- API keys: Provider credentials stored securely
- Model selection: Choose models per provider
- Search backend: SearXNG URL configuration
- Sources: Enable/disable research sources
Next steps
How it works
Understand the high-level flow of answering questions
Contributing
Learn how to contribute to Perplexica’s codebase