RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Fix: Reduce excessive IO operations by loading LLM factory configurations (#6047)
utopia20772d4a60cae6db
diff --git a/api/db/init_data.py b/api/db/init_data.pyindex 506377c479..523805e702 100644--- a/api/db/init_data.py+++ b/api/db/init_data.py@@ -103,13 +103,8 @@ def init_llm_factory():except Exception:pass- factory_llm_infos = json.load(- open(- os.path.join(get_project_base_directory(), "conf", "llm_factories.json"),- "r",- )- )- for factory_llm_info in factory_llm_infos["factory_llm_infos"]:+ factory_llm_infos = settings.FACTORY_LLM_INFOS+ for factory_llm_info in factory_llm_infos:llm_infos = factory_llm_info.pop("llm")try:LLMFactoriesService.save(**factory_llm_info)diff --git a/api/db/services/llm_service.py b/api/db/services/llm_service.pyindex 039d459365..910a6b2a23 100644--- a/api/db/services/llm_service.py+++ b/api/db/services/llm_service.py@@ -13,13 +13,11 @@# See the License for the specific language governing permissions and# limitations under the License.#-import jsonimport logging-import osfrom api.db.services.user_service import TenantService-from api.utils.file_utils import get_project_base_directoryfrom rag.llm import EmbeddingModel, CvModel, ChatModel, RerankModel, Seq2txtModel, TTSModel+from api import settingsfrom api.db import LLMTypefrom api.db.db_models import DBfrom api.db.db_models import LLMFactories, LLM, TenantLLM@@ -75,7 +73,7 @@ class TenantLLMService(CommonService):# model name must be xxx@yyytry:- model_factories = json.load(open(os.path.join(get_project_base_directory(), "conf/llm_factories.json"), "r"))["factory_llm_infos"]+ model_factories = settings.FACTORY_LLM_INFOSmodel_providers = set([f["name"] for f in model_factories])if arr[-1] not in model_providers:return model_name, Nonediff --git a/api/settings.py b/api/settings.pyindex 3aa7bf8f34..d4b829cf90 100644--- a/api/settings.py+++ b/api/settings.py@@ -16,6 +16,7 @@import osfrom datetime import datefrom enum import IntEnum, Enum+import jsonimport rag.utils.es_connimport rag.utils.infinity_conn@@ -24,6 +25,7 @@ from rag.nlp import searchfrom graphrag import search as kg_searchfrom api.utils import get_base_config, decrypt_database_configfrom api.constants import RAG_FLOW_SERVICE_NAME+from api.utils.file_utils import get_project_base_directoryLIGHTEN = int(os.environ.get('LIGHTEN', "0"))@@ -40,6 +42,7 @@ PARSERS = NoneHOST_IP = NoneHOST_PORT = NoneSECRET_KEY = None+FACTORY_LLM_INFOS = NoneDATABASE_TYPE = os.getenv("DB_TYPE", 'mysql')DATABASE = decrypt_database_config(name=DATABASE_TYPE)@@ -61,7 +64,7 @@ kg_retrievaler = Nonedef init_settings():- global LLM, LLM_FACTORY, LLM_BASE_URL, LIGHTEN, DATABASE_TYPE, DATABASE+ global LLM, LLM_FACTORY, LLM_BASE_URL, LIGHTEN, DATABASE_TYPE, DATABASE, FACTORY_LLM_INFOSLIGHTEN = int(os.environ.get('LIGHTEN', "0"))DATABASE_TYPE = os.getenv("DB_TYPE", 'mysql')DATABASE = decrypt_database_config(name=DATABASE_TYPE)@@ -69,6 +72,12 @@ def init_settings():LLM_DEFAULT_MODELS = LLM.get("default_models", {})LLM_FACTORY = LLM.get("factory", "Tongyi-Qianwen")LLM_BASE_URL = LLM.get("base_url")++ try:+ with open(os.path.join(get_project_base_directory(), "conf", "llm_factories.json"), "r") as f:+ FACTORY_LLM_INFOS = json.load(f)["factory_llm_infos"]+ except Exception:+ FACTORY_LLM_INFOS = []global CHAT_MDL, EMBEDDING_MDL, RERANK_MDL, ASR_MDL, IMAGE2TEXT_MDLif not LIGHTEN:diff --git a/rag/prompts.py b/rag/prompts.pyindex af6df16239..839a55fc16 100644--- a/rag/prompts.py+++ b/rag/prompts.py@@ -16,14 +16,13 @@import datetimeimport jsonimport logging-import osimport refrom collections import defaultdictimport json_repair+from api import settingsfrom api.db import LLMTypefrom api.db.services.document_service import DocumentServicefrom api.db.services.llm_service import TenantLLMService, LLMBundle-from api.utils.file_utils import get_project_base_directoryfrom rag.settings import TAG_FLDfrom rag.utils import num_tokens_from_string, encoder@@ -46,9 +45,9 @@ def chunks_format(reference):def llm_id2llm_type(llm_id):llm_id, _ = TenantLLMService.split_model_name_and_factory(llm_id)- fnm = os.path.join(get_project_base_directory(), "conf")- llm_factories = json.load(open(os.path.join(fnm, "llm_factories.json"), "r"))- for llm_factory in llm_factories["factory_llm_infos"]:++ llm_factories = settings.FACTORY_LLM_INFOS+ for llm_factory in llm_factories:for llm in llm_factory["llm"]:if llm_id == llm["llm_name"]:return llm["model_type"].strip(",")[-1]