This document details the core application services within RAGFlow responsible for implementing business logic, managing API requests, and initializing the main application server. It covers the API server initialization via api/ragflow_server.py, the setup and configuration of the Quart application, and the architectural patterns used in the service layer.
For complementary information on the system's persistence layer, task execution, or LLM integration, see the corresponding wiki sections: Data Storage Architecture (3.2), Task Execution and Queue System (3.3), and LLM Integration System (5).
RAGFlow's backend server starts in the api/ragflow_server.py script, which orchestrates the environment setup, configuration loading, database initialization, and then launches the Quart web server serving the API.
Early Bootstrapping:
LITELLM_LOCAL_MODEL_COST_MAP to "True" to avoid blocking startup on external network model cost map fetches api/ragflow_server.py25-27ragflow_server api/ragflow_server.py83-84Configuration & Logging:
settings.init_settings() and prints the RAG-specific configurations api/ragflow_server.py96-97Database Initialization:
init_web_db() to create necessary Peewee tables if absent api/ragflow_server.py41-106init_web_data() to seed initial system data api/ragflow_server.py42-107Runtime Configuration:
RuntimeConfig with environment and host settings api/ragflow_server.py126-127Plugin Loading:
GlobalPluginManager.load_plugins() to dynamically register extensible components and agent plugins api/ragflow_server.py47-129Signal Handling:
signal_handler for graceful shutdown on SIGINT and SIGTERM, ensuring all MCP sessions are closed api/ragflow_server.py74-132Background Threads:
update_progress() utilizing a RedisDistributedLock to periodically refresh document parsing progress api/ragflow_server.py55-159start_channel_server api/ragflow_server.py139-160Server Run:
api/apps/__init__.py:app) with configured host and port api/ragflow_server.py36-165Sources: api/ragflow_server.py17-171
The backend's business logic resides in dedicated "service" classes that act as facades over the ORM and data stores. These services manage the lifecycle of users, documents, and system metadata.
Sources: api/ragflow_server.py37-38 api/db/services/document_service.py42 api/db/services/dialog_service.py140 rag/nlp/search.py39 rag/utils/redis_conn.py34
| Service Class | Module Path | Responsibility |
|---|---|---|
UserService | api/db/services/user_service.py | Core user and tenant management including password updates and status activation api/db/services/user_service.py27 |
DocumentService | api/db/services/document_service.py | Document CRUD, metadata management, and health checks api/db/services/document_service.py42-115 |
DialogService | api/db/services/dialog_service.py | Manages chat assistant (Dialog) configurations and retrieval hydration api/db/services/dialog_service.py140-184 |
TaskService | api/db/services/task_service.py | Orchestrates document parsing tasks, progress tracking, and chunking credits api/db/services/task_service.py146-205 |
KnowledgebaseService | api/db/services/knowledgebase_service.py | Manages datasets, embedding models, and parser configurations api/db/services/knowledgebase_service.py32 |
LLMBundle | api/db/services/llm_service.py | Aggregates model configurations for chat, embedding, and reranking api/db/services/llm_service.py20 |
RAGFlow uses a robust task service to handle asynchronous document processing and a search dealer to manage complex retrieval logic.
TaskService manages the lifecycle of document processing, utilizing Redis to track pending and aborted tasks api/db/services/task_service.py56-104Dealer class in rag/nlp/search.py interacts with the DocStoreConnection to perform vector, full-text, and hybrid searches rag/nlp/search.py39-134FulltextQueryer handles query normalization, tokenization, and synonym expansion to build complex search expressions rag/nlp/query.py28-92Sources: rag/nlp/search.py134-186 rag/nlp/query.py42-92 api/db/services/dialog_service.py64-109
This diagram maps conceptual system components to their specific implementation classes and files.
Sources: api/ragflow_server.py17 rag/nlp/search.py39 api/db/services/task_service.py146 api/db/services/dialog_service.py140 api/db/db_models.py151
Refresh this wiki
This wiki was recently refreshed. Please wait 4 days to refresh again.