Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

settings.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. STRONG_TEST_COUNT = int(os.environ.get("STRONG_TEST_COUNT", "8"))
  64. BUILTIN_EMBEDDING_MODELS = ["BAAI/bge-large-zh-v1.5@BAAI", "maidalun1020/bce-embedding-base_v1@Youdao"]
  65. def get_or_create_secret_key():
  66. secret_key = os.environ.get("RAGFLOW_SECRET_KEY")
  67. if secret_key and len(secret_key) >= 32:
  68. return secret_key
  69. # Check if there's a configured secret key
  70. configured_key = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("secret_key")
  71. if configured_key and configured_key != str(date.today()) and len(configured_key) >= 32:
  72. return configured_key
  73. # Generate a new secure key and warn about it
  74. import logging
  75. new_key = secrets.token_hex(32)
  76. logging.warning(
  77. "SECURITY WARNING: Using auto-generated SECRET_KEY. "
  78. f"Generated key: {new_key}"
  79. )
  80. return new_key
  81. def init_settings():
  82. global LLM, LLM_FACTORY, LLM_BASE_URL, LIGHTEN, DATABASE_TYPE, DATABASE, FACTORY_LLM_INFOS, REGISTER_ENABLED
  83. LIGHTEN = int(os.environ.get("LIGHTEN", "0"))
  84. DATABASE_TYPE = os.getenv("DB_TYPE", "mysql")
  85. DATABASE = decrypt_database_config(name=DATABASE_TYPE)
  86. LLM = get_base_config("user_default_llm", {})
  87. LLM_DEFAULT_MODELS = LLM.get("default_models", {})
  88. LLM_FACTORY = LLM.get("factory")
  89. LLM_BASE_URL = LLM.get("base_url")
  90. try:
  91. REGISTER_ENABLED = int(os.environ.get("REGISTER_ENABLED", "1"))
  92. except Exception:
  93. pass
  94. try:
  95. with open(os.path.join(get_project_base_directory(), "conf", "llm_factories.json"), "r") as f:
  96. FACTORY_LLM_INFOS = json.load(f)["factory_llm_infos"]
  97. except Exception:
  98. FACTORY_LLM_INFOS = []
  99. global CHAT_MDL, EMBEDDING_MDL, RERANK_MDL, ASR_MDL, IMAGE2TEXT_MDL
  100. if not LIGHTEN:
  101. EMBEDDING_MDL = BUILTIN_EMBEDDING_MODELS[0]
  102. if LLM_DEFAULT_MODELS:
  103. CHAT_MDL = LLM_DEFAULT_MODELS.get("chat_model", CHAT_MDL)
  104. EMBEDDING_MDL = LLM_DEFAULT_MODELS.get("embedding_model", EMBEDDING_MDL)
  105. RERANK_MDL = LLM_DEFAULT_MODELS.get("rerank_model", RERANK_MDL)
  106. ASR_MDL = LLM_DEFAULT_MODELS.get("asr_model", ASR_MDL)
  107. IMAGE2TEXT_MDL = LLM_DEFAULT_MODELS.get("image2text_model", IMAGE2TEXT_MDL)
  108. # factory can be specified in the config name with "@". LLM_FACTORY will be used if not specified
  109. CHAT_MDL = CHAT_MDL + (f"@{LLM_FACTORY}" if "@" not in CHAT_MDL and CHAT_MDL != "" else "")
  110. EMBEDDING_MDL = EMBEDDING_MDL + (f"@{LLM_FACTORY}" if "@" not in EMBEDDING_MDL and EMBEDDING_MDL != "" else "")
  111. RERANK_MDL = RERANK_MDL + (f"@{LLM_FACTORY}" if "@" not in RERANK_MDL and RERANK_MDL != "" else "")
  112. ASR_MDL = ASR_MDL + (f"@{LLM_FACTORY}" if "@" not in ASR_MDL and ASR_MDL != "" else "")
  113. IMAGE2TEXT_MDL = IMAGE2TEXT_MDL + (f"@{LLM_FACTORY}" if "@" not in IMAGE2TEXT_MDL and IMAGE2TEXT_MDL != "" else "")
  114. global API_KEY, PARSERS, HOST_IP, HOST_PORT, SECRET_KEY
  115. API_KEY = LLM.get("api_key")
  116. PARSERS = LLM.get(
  117. "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"
  118. )
  119. HOST_IP = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("host", "127.0.0.1")
  120. HOST_PORT = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("http_port")
  121. SECRET_KEY = get_or_create_secret_key()
  122. global AUTHENTICATION_CONF, CLIENT_AUTHENTICATION, HTTP_APP_KEY, GITHUB_OAUTH, FEISHU_OAUTH, OAUTH_CONFIG
  123. # authentication
  124. AUTHENTICATION_CONF = get_base_config("authentication", {})
  125. # client
  126. CLIENT_AUTHENTICATION = AUTHENTICATION_CONF.get("client", {}).get("switch", False)
  127. HTTP_APP_KEY = AUTHENTICATION_CONF.get("client", {}).get("http_app_key")
  128. GITHUB_OAUTH = get_base_config("oauth", {}).get("github")
  129. FEISHU_OAUTH = get_base_config("oauth", {}).get("feishu")
  130. OAUTH_CONFIG = get_base_config("oauth", {})
  131. global DOC_ENGINE, docStoreConn, retrievaler, kg_retrievaler
  132. DOC_ENGINE = os.environ.get("DOC_ENGINE", "elasticsearch")
  133. # DOC_ENGINE = os.environ.get('DOC_ENGINE', "opensearch")
  134. lower_case_doc_engine = DOC_ENGINE.lower()
  135. if lower_case_doc_engine == "elasticsearch":
  136. docStoreConn = rag.utils.es_conn.ESConnection()
  137. elif lower_case_doc_engine == "infinity":
  138. docStoreConn = rag.utils.infinity_conn.InfinityConnection()
  139. elif lower_case_doc_engine == "opensearch":
  140. docStoreConn = rag.utils.opensearch_conn.OSConnection()
  141. else:
  142. raise Exception(f"Not supported doc engine: {DOC_ENGINE}")
  143. retrievaler = search.Dealer(docStoreConn)
  144. from graphrag import search as kg_search
  145. kg_retrievaler = kg_search.KGSearch(docStoreConn)
  146. if int(os.environ.get("SANDBOX_ENABLED", "0")):
  147. global SANDBOX_HOST
  148. SANDBOX_HOST = os.environ.get("SANDBOX_HOST", "sandbox-executor-manager")
  149. class CustomEnum(Enum):
  150. @classmethod
  151. def valid(cls, value):
  152. try:
  153. cls(value)
  154. return True
  155. except BaseException:
  156. return False
  157. @classmethod
  158. def values(cls):
  159. return [member.value for member in cls.__members__.values()]
  160. @classmethod
  161. def names(cls):
  162. return [member.name for member in cls.__members__.values()]
  163. class RetCode(IntEnum, CustomEnum):
  164. SUCCESS = 0
  165. NOT_EFFECTIVE = 10
  166. EXCEPTION_ERROR = 100
  167. ARGUMENT_ERROR = 101
  168. DATA_ERROR = 102
  169. OPERATING_ERROR = 103
  170. CONNECTION_ERROR = 105
  171. RUNNING = 106
  172. PERMISSION_ERROR = 108
  173. AUTHENTICATION_ERROR = 109
  174. UNAUTHORIZED = 401
  175. SERVER_ERROR = 500
  176. FORBIDDEN = 403
  177. NOT_FOUND = 404