2024-08-15 09:17:36 +08:00
|
|
|
#
|
|
|
|
|
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
|
2026-03-25 18:17:52 +08:00
|
|
|
print("Start RAGFlow server...")
|
2024-11-18 17:38:17 +08:00
|
|
|
|
2026-01-12 12:48:23 +08:00
|
|
|
import time
|
|
|
|
|
start_ts = time.time()
|
2024-11-14 17:13:48 +08:00
|
|
|
|
2024-12-10 09:36:59 +08:00
|
|
|
import os
|
2026-05-13 18:01:33 +08:00
|
|
|
|
|
|
|
|
# LiteLLM fetches a model cost map from GitHub during import unless this is set.
|
|
|
|
|
# The API server should not block startup on external network access.
|
|
|
|
|
os.environ.setdefault("LITELLM_LOCAL_MODEL_COST_MAP", "True")
|
|
|
|
|
|
|
|
|
|
import logging
|
2024-08-15 09:17:36 +08:00
|
|
|
import signal
|
|
|
|
|
import sys
|
2025-02-28 14:52:40 +08:00
|
|
|
import threading
|
2025-03-20 17:01:28 +08:00
|
|
|
import uuid
|
2025-11-26 11:06:37 +08:00
|
|
|
import faulthandler
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-12-12 10:23:40 +08:00
|
|
|
from api.apps import app
|
2024-08-15 09:17:36 +08:00
|
|
|
from api.db.runtime_config import RuntimeConfig
|
|
|
|
|
from api.db.services.document_service import DocumentService
|
2025-11-02 21:05:28 +08:00
|
|
|
from common.file_utils import get_project_base_directory
|
2025-11-06 09:36:38 +08:00
|
|
|
from common import settings
|
2024-08-15 09:17:36 +08:00
|
|
|
from api.db.db_models import init_database_tables as init_web_db
|
2025-11-24 19:02:08 +08:00
|
|
|
from api.db.init_data import init_web_data, init_superuser
|
2025-11-06 19:24:46 +08:00
|
|
|
from common.versions import get_ragflow_version
|
2025-11-03 17:25:06 +08:00
|
|
|
from common.config_utils import show_configs
|
2025-11-17 15:34:17 +08:00
|
|
|
from common.mcp_tool_call_conn import shutdown_all_mcp_sessions
|
2026-01-12 12:48:23 +08:00
|
|
|
from common.log_utils import init_root_logger
|
2026-01-29 14:23:26 +08:00
|
|
|
from agent.plugin import GlobalPluginManager
|
2025-03-10 15:15:06 +08:00
|
|
|
from rag.utils.redis_conn import RedisDistributedLock
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-02-28 14:52:40 +08:00
|
|
|
stop_event = threading.Event()
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-03-26 15:34:42 +08:00
|
|
|
RAGFLOW_DEBUGPY_LISTEN = int(os.environ.get('RAGFLOW_DEBUGPY_LISTEN', "0"))
|
|
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
def update_progress():
|
2025-03-20 17:01:28 +08:00
|
|
|
lock_value = str(uuid.uuid4())
|
|
|
|
|
redis_lock = RedisDistributedLock("update_progress", lock_value=lock_value, timeout=60)
|
|
|
|
|
logging.info(f"update_progress lock_value: {lock_value}")
|
2025-02-28 14:52:40 +08:00
|
|
|
while not stop_event.is_set():
|
2024-08-15 09:17:36 +08:00
|
|
|
try:
|
2025-03-20 17:01:28 +08:00
|
|
|
if redis_lock.acquire():
|
|
|
|
|
DocumentService.update_progress()
|
|
|
|
|
redis_lock.release()
|
2024-11-12 17:35:13 +08:00
|
|
|
except Exception:
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.exception("update_progress exception")
|
2025-04-14 10:23:13 +08:00
|
|
|
finally:
|
2025-08-19 09:42:15 +08:00
|
|
|
try:
|
|
|
|
|
redis_lock.release()
|
|
|
|
|
except Exception:
|
|
|
|
|
logging.exception("update_progress exception")
|
|
|
|
|
stop_event.wait(6)
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-02-28 14:52:40 +08:00
|
|
|
def signal_handler(sig, frame):
|
|
|
|
|
logging.info("Received interrupt signal, shutting down...")
|
2025-06-26 13:52:01 +08:00
|
|
|
shutdown_all_mcp_sessions()
|
2025-02-28 14:52:40 +08:00
|
|
|
stop_event.set()
|
2025-12-01 14:24:06 +08:00
|
|
|
stop_event.wait(1)
|
2025-02-28 14:52:40 +08:00
|
|
|
sys.exit(0)
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2024-11-12 17:35:13 +08:00
|
|
|
if __name__ == '__main__':
|
2025-11-26 11:06:37 +08:00
|
|
|
faulthandler.enable()
|
|
|
|
|
init_root_logger("ragflow_server")
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info(r"""
|
2025-08-15 18:12:20 +08:00
|
|
|
____ ___ ______ ______ __
|
2024-09-29 16:28:07 +08:00
|
|
|
/ __ \ / | / ____// ____// /____ _ __
|
|
|
|
|
/ /_/ // /| | / / __ / /_ / // __ \| | /| / /
|
2025-08-15 18:12:20 +08:00
|
|
|
/ _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
|
|
|
|
|
/_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2024-11-12 17:35:13 +08:00
|
|
|
""")
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info(
|
2024-11-14 17:51:21 +08:00
|
|
|
f'RAGFlow version: {get_ragflow_version()}'
|
2024-11-14 15:51:30 +08:00
|
|
|
)
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info(
|
2025-11-02 21:05:28 +08:00
|
|
|
f'project base: {get_project_base_directory()}'
|
2024-08-15 09:17:36 +08:00
|
|
|
)
|
Print configs when startup RAGFlow server (#3414)
### What problem does this PR solve?
Print configs at the RAGFlow startup phase.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
```
2024-11-14 21:27:53,090 INFO 962231 Current configs, from /home/weilongma/Documents/development/ragflow/conf/service_conf.yaml:
2024-11-14 21:27:53,090 INFO 962231 ragflow: {'host': '0.0.0.0', 'http_port': 9380}
2024-11-14 21:27:53,090 INFO 962231 mysql: {'name': 'rag_flow', 'user': 'root', 'password': 'infini_rag_flow', 'host': 'mysql', 'port': 5455, 'max_connections': 100, 'stale_timeout': 30}
2024-11-14 21:27:53,090 INFO 962231 minio: {'user': 'rag_flow', 'password': 'infini_rag_flow', 'host': 'minio:9000'}
2024-11-14 21:27:53,090 INFO 962231 es: {'hosts': 'http://es01:1200', 'username': 'elastic', 'password': 'infini_rag_flow'}
2024-11-14 21:27:53,090 INFO 962231 redis: {'db': 1, 'password': 'infini_rag_flow', 'host': 'redis:6379'}
```
Signed-off-by: jinhai <haijin.chn@gmail.com>
2024-11-15 09:29:40 +08:00
|
|
|
show_configs()
|
2024-11-15 17:30:56 +08:00
|
|
|
settings.init_settings()
|
2025-11-06 09:36:38 +08:00
|
|
|
settings.print_rag_settings()
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-03-26 15:34:42 +08:00
|
|
|
if RAGFLOW_DEBUGPY_LISTEN > 0:
|
|
|
|
|
logging.info(f"debugpy listen on {RAGFLOW_DEBUGPY_LISTEN}")
|
|
|
|
|
import debugpy
|
|
|
|
|
debugpy.listen(("0.0.0.0", RAGFLOW_DEBUGPY_LISTEN))
|
|
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
# init db
|
|
|
|
|
init_web_db()
|
|
|
|
|
init_web_data()
|
|
|
|
|
# init runtime config
|
|
|
|
|
import argparse
|
2024-11-04 08:35:36 +01:00
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
2024-11-04 08:35:36 +01:00
|
|
|
parser.add_argument(
|
Print configs when startup RAGFlow server (#3414)
### What problem does this PR solve?
Print configs at the RAGFlow startup phase.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
```
2024-11-14 21:27:53,090 INFO 962231 Current configs, from /home/weilongma/Documents/development/ragflow/conf/service_conf.yaml:
2024-11-14 21:27:53,090 INFO 962231 ragflow: {'host': '0.0.0.0', 'http_port': 9380}
2024-11-14 21:27:53,090 INFO 962231 mysql: {'name': 'rag_flow', 'user': 'root', 'password': 'infini_rag_flow', 'host': 'mysql', 'port': 5455, 'max_connections': 100, 'stale_timeout': 30}
2024-11-14 21:27:53,090 INFO 962231 minio: {'user': 'rag_flow', 'password': 'infini_rag_flow', 'host': 'minio:9000'}
2024-11-14 21:27:53,090 INFO 962231 es: {'hosts': 'http://es01:1200', 'username': 'elastic', 'password': 'infini_rag_flow'}
2024-11-14 21:27:53,090 INFO 962231 redis: {'db': 1, 'password': 'infini_rag_flow', 'host': 'redis:6379'}
```
Signed-off-by: jinhai <haijin.chn@gmail.com>
2024-11-15 09:29:40 +08:00
|
|
|
"--version", default=False, help="RAGFlow version", action="store_true"
|
2024-11-04 08:35:36 +01:00
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--debug", default=False, help="debug mode", action="store_true"
|
|
|
|
|
)
|
2025-11-24 19:02:08 +08:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--init-superuser", default=False, help="init superuser", action="store_true"
|
|
|
|
|
)
|
2024-08-15 09:17:36 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
if args.version:
|
2024-11-14 17:51:21 +08:00
|
|
|
print(get_ragflow_version())
|
2024-08-15 09:17:36 +08:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
2025-11-24 19:02:08 +08:00
|
|
|
if args.init_superuser:
|
|
|
|
|
init_superuser()
|
2024-08-15 09:17:36 +08:00
|
|
|
RuntimeConfig.DEBUG = args.debug
|
|
|
|
|
if RuntimeConfig.DEBUG:
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info("run on debug mode")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
RuntimeConfig.init_env()
|
2024-11-15 17:30:56 +08:00
|
|
|
RuntimeConfig.init_config(JOB_SERVER_HOST=settings.HOST_IP, HTTP_PORT=settings.HOST_PORT)
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-05-16 16:32:19 +08:00
|
|
|
GlobalPluginManager.load_plugins()
|
|
|
|
|
|
2025-02-28 14:52:40 +08:00
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
|
|
2025-05-30 13:38:30 +08:00
|
|
|
def delayed_start_update_progress():
|
|
|
|
|
logging.info("Starting update_progress thread (delayed)")
|
|
|
|
|
t = threading.Thread(target=update_progress, daemon=True)
|
|
|
|
|
t.start()
|
|
|
|
|
|
Feat: chat channels — connect assistants to external messaging bots (#15850)
### What problem does this PR solve?
#15844
Adds a **Chat channels** capability so a RAGFlow assistant (Dialog) can
be exposed as a bot on external messaging platforms (Feishu/Lark,
Discord, Telegram, Slack, WeCom, LINE, etc.). An admin configures a bot
in the UI, connects it to an assistant, and inbound messages are
answered from that assistant's knowledge base — replies are delivered
back on the channel.
**Feishu/Lark is implemented and tested end-to-end.** Discord, Telegram,
LINE, and WeCom are scaffolded against the same interface; the remaining
listed channels are tracked as follow-ups.
### Design
**Backend**
- New `chat_channel` table (`tenant_id`, `name`, `channel`, `config`
JSON holding `{credential: {...}}`, `dialog_id`, `status`) +
`ChatChannelService` and RESTful CRUD under `/api/v1/chat_channels`.
- Channel framework under `api/channels/`: a `core` registry +
per-channel packages that self-register a builder and implement a common
`Channel` interface (`start`/`stop`/`send` + inbound normalization) over
`IncomingMessage`/`OutgoingMessage`.
- Embedded **reconcile loop** in `ragflow_server`
(`api/channels/bootstrap.py`): loads enabled bots, and
starts/stops/restarts them as rows change (no server restart needed).
Inbound messages run the connected dialog via the non-streaming
completion path, keeping per-end-user conversation history.
- Missing optional channel SDKs degrade gracefully (channel skipped with
a warning; others unaffected). Channel-level errors are logged, not
crashed.
- Feishu's WebSocket client runs in a dedicated thread with its own
event loop to avoid cross-loop/contextvars conflicts with the channel
runtime.
**Frontend**
- **Settings → Chat channels** panel: available-channels grid +
configured-bots list with add/edit/delete and a **Connect assistant**
popup that binds a bot to a dialog.
- Brand icons via simple-icons / reused shared data-source assets, with
colored fallbacks for brands not available.
- Route, sidebar entry, i18n (en/zh), and a top-nav segment-boundary fix
so the settings page no longer highlights the Chat tab.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### Notes
- DB: new `chat_channel` table is auto-created; `chat_channel.dialog_id`
is also covered by a `migrate_db` `alter_db_add_column` for existing
installs.
- Channel SDKs (`lark-oapi`, `discord.py`, `python-telegram-bot`,
`line-bot-sdk`, `wechatpy`, `aiohttp`) added to dependencies.
- Screenshots / per-channel credential docs to follow.
<img width="1338" height="1290" alt="Image"
src="https://github.com/user-attachments/assets/042cb2f9-0dad-4e6a-bcf7-43ced4bbd704"
/>
<img width="1344" height="738" alt="Image"
src="https://github.com/user-attachments/assets/373cd08e-ec40-4c67-9c51-4d948b1ba617"
/>
<img width="672" height="887" alt="Image"
src="https://github.com/user-attachments/assets/5a34953f-a9a3-4c1e-869e-5eff0dc64c84"
/>
---------
2026-06-12 18:21:30 +08:00
|
|
|
def start_chat_channels():
|
|
|
|
|
try:
|
|
|
|
|
from api.channels.bootstrap import start_channel_server
|
|
|
|
|
logging.info("Starting chat channel server thread")
|
|
|
|
|
t = threading.Thread(
|
|
|
|
|
target=start_channel_server,
|
|
|
|
|
args=(stop_event,),
|
|
|
|
|
daemon=True,
|
|
|
|
|
name="chat-channels",
|
|
|
|
|
)
|
|
|
|
|
t.start()
|
|
|
|
|
except Exception:
|
|
|
|
|
logging.exception("Failed to start chat channel server")
|
|
|
|
|
|
2025-05-30 13:38:30 +08:00
|
|
|
if RuntimeConfig.DEBUG:
|
|
|
|
|
if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
|
|
|
|
|
threading.Timer(1.0, delayed_start_update_progress).start()
|
Feat: chat channels — connect assistants to external messaging bots (#15850)
### What problem does this PR solve?
#15844
Adds a **Chat channels** capability so a RAGFlow assistant (Dialog) can
be exposed as a bot on external messaging platforms (Feishu/Lark,
Discord, Telegram, Slack, WeCom, LINE, etc.). An admin configures a bot
in the UI, connects it to an assistant, and inbound messages are
answered from that assistant's knowledge base — replies are delivered
back on the channel.
**Feishu/Lark is implemented and tested end-to-end.** Discord, Telegram,
LINE, and WeCom are scaffolded against the same interface; the remaining
listed channels are tracked as follow-ups.
### Design
**Backend**
- New `chat_channel` table (`tenant_id`, `name`, `channel`, `config`
JSON holding `{credential: {...}}`, `dialog_id`, `status`) +
`ChatChannelService` and RESTful CRUD under `/api/v1/chat_channels`.
- Channel framework under `api/channels/`: a `core` registry +
per-channel packages that self-register a builder and implement a common
`Channel` interface (`start`/`stop`/`send` + inbound normalization) over
`IncomingMessage`/`OutgoingMessage`.
- Embedded **reconcile loop** in `ragflow_server`
(`api/channels/bootstrap.py`): loads enabled bots, and
starts/stops/restarts them as rows change (no server restart needed).
Inbound messages run the connected dialog via the non-streaming
completion path, keeping per-end-user conversation history.
- Missing optional channel SDKs degrade gracefully (channel skipped with
a warning; others unaffected). Channel-level errors are logged, not
crashed.
- Feishu's WebSocket client runs in a dedicated thread with its own
event loop to avoid cross-loop/contextvars conflicts with the channel
runtime.
**Frontend**
- **Settings → Chat channels** panel: available-channels grid +
configured-bots list with add/edit/delete and a **Connect assistant**
popup that binds a bot to a dialog.
- Brand icons via simple-icons / reused shared data-source assets, with
colored fallbacks for brands not available.
- Route, sidebar entry, i18n (en/zh), and a top-nav segment-boundary fix
so the settings page no longer highlights the Chat tab.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### Notes
- DB: new `chat_channel` table is auto-created; `chat_channel.dialog_id`
is also covered by a `migrate_db` `alter_db_add_column` for existing
installs.
- Channel SDKs (`lark-oapi`, `discord.py`, `python-telegram-bot`,
`line-bot-sdk`, `wechatpy`, `aiohttp`) added to dependencies.
- Screenshots / per-channel credential docs to follow.
<img width="1338" height="1290" alt="Image"
src="https://github.com/user-attachments/assets/042cb2f9-0dad-4e6a-bcf7-43ced4bbd704"
/>
<img width="1344" height="738" alt="Image"
src="https://github.com/user-attachments/assets/373cd08e-ec40-4c67-9c51-4d948b1ba617"
/>
<img width="672" height="887" alt="Image"
src="https://github.com/user-attachments/assets/5a34953f-a9a3-4c1e-869e-5eff0dc64c84"
/>
---------
2026-06-12 18:21:30 +08:00
|
|
|
start_chat_channels()
|
2025-05-30 13:38:30 +08:00
|
|
|
else:
|
|
|
|
|
threading.Timer(1.0, delayed_start_update_progress).start()
|
Feat: chat channels — connect assistants to external messaging bots (#15850)
### What problem does this PR solve?
#15844
Adds a **Chat channels** capability so a RAGFlow assistant (Dialog) can
be exposed as a bot on external messaging platforms (Feishu/Lark,
Discord, Telegram, Slack, WeCom, LINE, etc.). An admin configures a bot
in the UI, connects it to an assistant, and inbound messages are
answered from that assistant's knowledge base — replies are delivered
back on the channel.
**Feishu/Lark is implemented and tested end-to-end.** Discord, Telegram,
LINE, and WeCom are scaffolded against the same interface; the remaining
listed channels are tracked as follow-ups.
### Design
**Backend**
- New `chat_channel` table (`tenant_id`, `name`, `channel`, `config`
JSON holding `{credential: {...}}`, `dialog_id`, `status`) +
`ChatChannelService` and RESTful CRUD under `/api/v1/chat_channels`.
- Channel framework under `api/channels/`: a `core` registry +
per-channel packages that self-register a builder and implement a common
`Channel` interface (`start`/`stop`/`send` + inbound normalization) over
`IncomingMessage`/`OutgoingMessage`.
- Embedded **reconcile loop** in `ragflow_server`
(`api/channels/bootstrap.py`): loads enabled bots, and
starts/stops/restarts them as rows change (no server restart needed).
Inbound messages run the connected dialog via the non-streaming
completion path, keeping per-end-user conversation history.
- Missing optional channel SDKs degrade gracefully (channel skipped with
a warning; others unaffected). Channel-level errors are logged, not
crashed.
- Feishu's WebSocket client runs in a dedicated thread with its own
event loop to avoid cross-loop/contextvars conflicts with the channel
runtime.
**Frontend**
- **Settings → Chat channels** panel: available-channels grid +
configured-bots list with add/edit/delete and a **Connect assistant**
popup that binds a bot to a dialog.
- Brand icons via simple-icons / reused shared data-source assets, with
colored fallbacks for brands not available.
- Route, sidebar entry, i18n (en/zh), and a top-nav segment-boundary fix
so the settings page no longer highlights the Chat tab.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### Notes
- DB: new `chat_channel` table is auto-created; `chat_channel.dialog_id`
is also covered by a `migrate_db` `alter_db_add_column` for existing
installs.
- Channel SDKs (`lark-oapi`, `discord.py`, `python-telegram-bot`,
`line-bot-sdk`, `wechatpy`, `aiohttp`) added to dependencies.
- Screenshots / per-channel credential docs to follow.
<img width="1338" height="1290" alt="Image"
src="https://github.com/user-attachments/assets/042cb2f9-0dad-4e6a-bcf7-43ced4bbd704"
/>
<img width="1344" height="738" alt="Image"
src="https://github.com/user-attachments/assets/373cd08e-ec40-4c67-9c51-4d948b1ba617"
/>
<img width="672" height="887" alt="Image"
src="https://github.com/user-attachments/assets/5a34953f-a9a3-4c1e-869e-5eff0dc64c84"
/>
---------
2026-06-12 18:21:30 +08:00
|
|
|
start_chat_channels()
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
# start http server
|
|
|
|
|
try:
|
2026-01-12 12:48:23 +08:00
|
|
|
logging.info(f"RAGFlow server is ready after {time.time() - start_ts}s initialization.")
|
2026-04-10 18:01:49 +08:00
|
|
|
app.run(host=settings.HOST_IP, port=settings.HOST_PORT, use_reloader=RuntimeConfig.DEBUG, debug=False)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.exception(f"Unhandled exception: {e}")
|
2025-02-28 14:52:40 +08:00
|
|
|
stop_event.set()
|
2025-12-01 14:24:06 +08:00
|
|
|
stop_event.wait(1)
|
2024-11-04 08:35:36 +01:00
|
|
|
os.kill(os.getpid(), signal.SIGKILL)
|