您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. import os
  18. import secrets
  19. from datetime import date
  20. from enum import Enum, IntEnum
  21. import rag.utils
  22. import rag.utils.es_conn
  23. import rag.utils.infinity_conn
  24. import rag.utils.opensearch_conn
  25. from api.constants import RAG_FLOW_SERVICE_NAME
  26. from api.utils import decrypt_database_config, get_base_config
  27. from api.utils.file_utils import get_project_base_directory
  28. from rag.nlp import search
  29. LIGHTEN = int(os.environ.get("LIGHTEN", "0"))
  30. LLM = None
  31. LLM_FACTORY = None
  32. LLM_BASE_URL = None
  33. CHAT_MDL = ""
  34. EMBEDDING_MDL = ""
  35. RERANK_MDL = ""
  36. ASR_MDL = ""
  37. IMAGE2TEXT_MDL = ""
  38. API_KEY = None
  39. PARSERS = None
  40. HOST_IP = None
  41. HOST_PORT = None
  42. SECRET_KEY = None
  43. FACTORY_LLM_INFOS = None
  44. DATABASE_TYPE = os.getenv("DB_TYPE", "mysql")
  45. DATABASE = decrypt_database_config(name=DATABASE_TYPE)
  46. # authentication
  47. AUTHENTICATION_CONF = None
  48. # client
  49. CLIENT_AUTHENTICATION = None
  50. HTTP_APP_KEY = None
  51. GITHUB_OAUTH = None
  52. FEISHU_OAUTH = None
  53. OAUTH_CONFIG = None
  54. DOC_ENGINE = None
  55. docStoreConn = None
  56. retrievaler = None
  57. kg_retrievaler = None
  58. # user registration switch
  59. REGISTER_ENABLED = 1
  60. # sandbox-executor-manager
  61. SANDBOX_ENABLED = 0
  62. SANDBOX_HOST = None
  63. BUILTIN_EMBEDDING_MODELS = ["BAAI/bge-large-zh-v1.5@BAAI", "maidalun1020/bce-embedding-base_v1@Youdao"]
  64. def get_or_create_secret_key():
  65. secret_key = os.environ.get("RAGFLOW_SECRET_KEY")
  66. if secret_key and len(secret_key) >= 32:
  67. return secret_key
  68. # Check if there's a configured secret key
  69. configured_key = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("secret_key")
  70. if configured_key and configured_key != str(date.today()) and len(configured_key) >= 32:
  71. return configured_key
  72. # Generate a new secure key and warn about it
  73. import logging
  74. new_key = secrets.token_hex(32)
  75. logging.warning(
  76. "SECURITY WARNING: Using auto-generated SECRET_KEY. "
  77. f"Generated key: {new_key}"
  78. )
  79. return new_key
  80. def init_settings():
  81. global LLM, LLM_FACTORY, LLM_BASE_URL, LIGHTEN, DATABASE_TYPE, DATABASE, FACTORY_LLM_INFOS, REGISTER_ENABLED
  82. LIGHTEN = int(os.environ.get("LIGHTEN", "0"))
  83. DATABASE_TYPE = os.getenv("DB_TYPE", "mysql")
  84. DATABASE = decrypt_database_config(name=DATABASE_TYPE)
  85. LLM = get_base_config("user_default_llm", {})
  86. LLM_DEFAULT_MODELS = LLM.get("default_models", {})
  87. LLM_FACTORY = LLM.get("factory")
  88. LLM_BASE_URL = LLM.get("base_url")
  89. try:
  90. REGISTER_ENABLED = int(os.environ.get("REGISTER_ENABLED", "1"))
  91. except Exception:
  92. pass
  93. try:
  94. with open(os.path.join(get_project_base_directory(), "conf", "llm_factories.json"), "r") as f:
  95. FACTORY_LLM_INFOS = json.load(f)["factory_llm_infos"]
  96. except Exception:
  97. FACTORY_LLM_INFOS = []
  98. global CHAT_MDL, EMBEDDING_MDL, RERANK_MDL, ASR_MDL, IMAGE2TEXT_MDL
  99. if not LIGHTEN:
  100. EMBEDDING_MDL = BUILTIN_EMBEDDING_MODELS[0]
  101. if LLM_DEFAULT_MODELS:
  102. CHAT_MDL = LLM_DEFAULT_MODELS.get("chat_model", CHAT_MDL)
  103. EMBEDDING_MDL = LLM_DEFAULT_MODELS.get("embedding_model", EMBEDDING_MDL)
  104. RERANK_MDL = LLM_DEFAULT_MODELS.get("rerank_model", RERANK_MDL)
  105. ASR_MDL = LLM_DEFAULT_MODELS.get("asr_model", ASR_MDL)
  106. IMAGE2TEXT_MDL = LLM_DEFAULT_MODELS.get("image2text_model", IMAGE2TEXT_MDL)
  107. # factory can be specified in the config name with "@". LLM_FACTORY will be used if not specified
  108. CHAT_MDL = CHAT_MDL + (f"@{LLM_FACTORY}" if "@" not in CHAT_MDL and CHAT_MDL != "" else "")
  109. EMBEDDING_MDL = EMBEDDING_MDL + (f"@{LLM_FACTORY}" if "@" not in EMBEDDING_MDL and EMBEDDING_MDL != "" else "")
  110. RERANK_MDL = RERANK_MDL + (f"@{LLM_FACTORY}" if "@" not in RERANK_MDL and RERANK_MDL != "" else "")
  111. ASR_MDL = ASR_MDL + (f"@{LLM_FACTORY}" if "@" not in ASR_MDL and ASR_MDL != "" else "")
  112. IMAGE2TEXT_MDL = IMAGE2TEXT_MDL + (f"@{LLM_FACTORY}" if "@" not in IMAGE2TEXT_MDL and IMAGE2TEXT_MDL != "" else "")
  113. global API_KEY, PARSERS, HOST_IP, HOST_PORT, SECRET_KEY
  114. API_KEY = LLM.get("api_key")
  115. PARSERS = LLM.get(
  116. "parsers", "naive:General,qa:Q&A,resume:Resume,manual:Manual,table:Table,paper:Paper,book:Book,laws:Laws,presentation:Presentation,picture:Picture,one:One,audio:Audio,email:Email,tag:Tag"
  117. )
  118. HOST_IP = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("host", "127.0.0.1")
  119. HOST_PORT = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("http_port")
  120. SECRET_KEY = get_or_create_secret_key()
  121. global AUTHENTICATION_CONF, CLIENT_AUTHENTICATION, HTTP_APP_KEY, GITHUB_OAUTH, FEISHU_OAUTH, OAUTH_CONFIG
  122. # authentication
  123. AUTHENTICATION_CONF = get_base_config("authentication", {})
  124. # client
  125. CLIENT_AUTHENTICATION = AUTHENTICATION_CONF.get("client", {}).get("switch", False)
  126. HTTP_APP_KEY = AUTHENTICATION_CONF.get("client", {}).get("http_app_key")
  127. GITHUB_OAUTH = get_base_config("oauth", {}).get("github")
  128. FEISHU_OAUTH = get_base_config("oauth", {}).get("feishu")
  129. OAUTH_CONFIG = get_base_config("oauth", {})
  130. global DOC_ENGINE, docStoreConn, retrievaler, kg_retrievaler
  131. DOC_ENGINE = os.environ.get("DOC_ENGINE", "elasticsearch")
  132. # DOC_ENGINE = os.environ.get('DOC_ENGINE', "opensearch")
  133. lower_case_doc_engine = DOC_ENGINE.lower()
  134. if lower_case_doc_engine == "elasticsearch":
  135. docStoreConn = rag.utils.es_conn.ESConnection()
  136. elif lower_case_doc_engine == "infinity":
  137. docStoreConn = rag.utils.infinity_conn.InfinityConnection()
  138. elif lower_case_doc_engine == "opensearch":
  139. docStoreConn = rag.utils.opensearch_conn.OSConnection()
  140. else:
  141. raise Exception(f"Not supported doc engine: {DOC_ENGINE}")
  142. retrievaler = search.Dealer(docStoreConn)
  143. from graphrag import search as kg_search
  144. kg_retrievaler = kg_search.KGSearch(docStoreConn)
  145. if int(os.environ.get("SANDBOX_ENABLED", "0")):
  146. global SANDBOX_HOST
  147. SANDBOX_HOST = os.environ.get("SANDBOX_HOST", "sandbox-executor-manager")
  148. class CustomEnum(Enum):
  149. @classmethod
  150. def valid(cls, value):
  151. try:
  152. cls(value)
  153. return True
  154. except BaseException:
  155. return False
  156. @classmethod
  157. def values(cls):
  158. return [member.value for member in cls.__members__.values()]
  159. @classmethod
  160. def names(cls):
  161. return [member.name for member in cls.__members__.values()]
  162. class RetCode(IntEnum, CustomEnum):
  163. SUCCESS = 0
  164. NOT_EFFECTIVE = 10
  165. EXCEPTION_ERROR = 100
  166. ARGUMENT_ERROR = 101
  167. DATA_ERROR = 102
  168. OPERATING_ERROR = 103
  169. CONNECTION_ERROR = 105
  170. RUNNING = 106
  171. PERMISSION_ERROR = 108
  172. AUTHENTICATION_ERROR = 109
  173. UNAUTHORIZED = 401
  174. SERVER_ERROR = 500
  175. FORBIDDEN = 403
  176. NOT_FOUND = 404