You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

settings.py 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 os
  17. from datetime import date
  18. from enum import IntEnum, Enum
  19. from api.utils.file_utils import get_project_base_directory
  20. import rag.utils.es_conn
  21. import rag.utils.infinity_conn
  22. import rag.utils
  23. from rag.nlp import search
  24. from graphrag import search as kg_search
  25. from api.utils import get_base_config, decrypt_database_config
  26. API_VERSION = "v1"
  27. RAG_FLOW_SERVICE_NAME = "ragflow"
  28. SERVER_MODULE = "rag_flow_server.py"
  29. TEMP_DIRECTORY = os.path.join(get_project_base_directory(), "temp")
  30. RAG_FLOW_CONF_PATH = os.path.join(get_project_base_directory(), "conf")
  31. LIGHTEN = int(os.environ.get('LIGHTEN', "0"))
  32. ERROR_REPORT = True
  33. ERROR_REPORT_WITH_PATH = False
  34. MAX_TIMESTAMP_INTERVAL = 60
  35. SESSION_VALID_PERIOD = 7 * 24 * 60 * 60
  36. REQUEST_TRY_TIMES = 3
  37. REQUEST_WAIT_SEC = 2
  38. REQUEST_MAX_WAIT_SEC = 300
  39. USE_REGISTRY = get_base_config("use_registry")
  40. LLM = get_base_config("user_default_llm", {})
  41. LLM_FACTORY = LLM.get("factory", "Tongyi-Qianwen")
  42. LLM_BASE_URL = LLM.get("base_url")
  43. CHAT_MDL = EMBEDDING_MDL = RERANK_MDL = ASR_MDL = IMAGE2TEXT_MDL = ""
  44. if not LIGHTEN:
  45. default_llm = {
  46. "Tongyi-Qianwen": {
  47. "chat_model": "qwen-plus",
  48. "embedding_model": "text-embedding-v2",
  49. "image2text_model": "qwen-vl-max",
  50. "asr_model": "paraformer-realtime-8k-v1",
  51. },
  52. "OpenAI": {
  53. "chat_model": "gpt-3.5-turbo",
  54. "embedding_model": "text-embedding-ada-002",
  55. "image2text_model": "gpt-4-vision-preview",
  56. "asr_model": "whisper-1",
  57. },
  58. "Azure-OpenAI": {
  59. "chat_model": "gpt-35-turbo",
  60. "embedding_model": "text-embedding-ada-002",
  61. "image2text_model": "gpt-4-vision-preview",
  62. "asr_model": "whisper-1",
  63. },
  64. "ZHIPU-AI": {
  65. "chat_model": "glm-3-turbo",
  66. "embedding_model": "embedding-2",
  67. "image2text_model": "glm-4v",
  68. "asr_model": "",
  69. },
  70. "Ollama": {
  71. "chat_model": "qwen-14B-chat",
  72. "embedding_model": "flag-embedding",
  73. "image2text_model": "",
  74. "asr_model": "",
  75. },
  76. "Moonshot": {
  77. "chat_model": "moonshot-v1-8k",
  78. "embedding_model": "",
  79. "image2text_model": "",
  80. "asr_model": "",
  81. },
  82. "DeepSeek": {
  83. "chat_model": "deepseek-chat",
  84. "embedding_model": "",
  85. "image2text_model": "",
  86. "asr_model": "",
  87. },
  88. "VolcEngine": {
  89. "chat_model": "",
  90. "embedding_model": "",
  91. "image2text_model": "",
  92. "asr_model": "",
  93. },
  94. "BAAI": {
  95. "chat_model": "",
  96. "embedding_model": "BAAI/bge-large-zh-v1.5",
  97. "image2text_model": "",
  98. "asr_model": "",
  99. "rerank_model": "BAAI/bge-reranker-v2-m3",
  100. }
  101. }
  102. if LLM_FACTORY:
  103. CHAT_MDL = default_llm[LLM_FACTORY]["chat_model"] + f"@{LLM_FACTORY}"
  104. ASR_MDL = default_llm[LLM_FACTORY]["asr_model"] + f"@{LLM_FACTORY}"
  105. IMAGE2TEXT_MDL = default_llm[LLM_FACTORY]["image2text_model"] + f"@{LLM_FACTORY}"
  106. EMBEDDING_MDL = default_llm["BAAI"]["embedding_model"] + "@BAAI"
  107. RERANK_MDL = default_llm["BAAI"]["rerank_model"] + "@BAAI"
  108. API_KEY = LLM.get("api_key", "")
  109. PARSERS = LLM.get(
  110. "parsers",
  111. "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,knowledge_graph:Knowledge Graph,email:Email")
  112. # distribution
  113. DEPENDENT_DISTRIBUTION = get_base_config("dependent_distribution", False)
  114. RAG_FLOW_UPDATE_CHECK = False
  115. HOST = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("host", "127.0.0.1")
  116. HTTP_PORT = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("http_port")
  117. SECRET_KEY = get_base_config(
  118. RAG_FLOW_SERVICE_NAME,
  119. {}).get("secret_key", str(date.today()))
  120. TOKEN_EXPIRE_IN = get_base_config(
  121. RAG_FLOW_SERVICE_NAME, {}).get(
  122. "token_expires_in", 3600)
  123. NGINX_HOST = get_base_config(
  124. RAG_FLOW_SERVICE_NAME, {}).get(
  125. "nginx", {}).get("host") or HOST
  126. NGINX_HTTP_PORT = get_base_config(
  127. RAG_FLOW_SERVICE_NAME, {}).get(
  128. "nginx", {}).get("http_port") or HTTP_PORT
  129. RANDOM_INSTANCE_ID = get_base_config(
  130. RAG_FLOW_SERVICE_NAME, {}).get(
  131. "random_instance_id", False)
  132. PROXY = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("proxy")
  133. PROXY_PROTOCOL = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("protocol")
  134. DATABASE_TYPE = os.getenv("DB_TYPE", 'mysql')
  135. DATABASE = decrypt_database_config(name=DATABASE_TYPE)
  136. # Switch
  137. # upload
  138. UPLOAD_DATA_FROM_CLIENT = True
  139. # authentication
  140. AUTHENTICATION_CONF = get_base_config("authentication", {})
  141. # client
  142. CLIENT_AUTHENTICATION = AUTHENTICATION_CONF.get(
  143. "client", {}).get(
  144. "switch", False)
  145. HTTP_APP_KEY = AUTHENTICATION_CONF.get("client", {}).get("http_app_key")
  146. GITHUB_OAUTH = get_base_config("oauth", {}).get("github")
  147. FEISHU_OAUTH = get_base_config("oauth", {}).get("feishu")
  148. WECHAT_OAUTH = get_base_config("oauth", {}).get("wechat")
  149. # site
  150. SITE_AUTHENTICATION = AUTHENTICATION_CONF.get("site", {}).get("switch", False)
  151. # permission
  152. PERMISSION_CONF = get_base_config("permission", {})
  153. PERMISSION_SWITCH = PERMISSION_CONF.get("switch")
  154. COMPONENT_PERMISSION = PERMISSION_CONF.get("component")
  155. DATASET_PERMISSION = PERMISSION_CONF.get("dataset")
  156. HOOK_MODULE = get_base_config("hook_module")
  157. HOOK_SERVER_NAME = get_base_config("hook_server_name")
  158. ENABLE_MODEL_STORE = get_base_config('enable_model_store', False)
  159. # authentication
  160. USE_AUTHENTICATION = False
  161. USE_DATA_AUTHENTICATION = False
  162. AUTOMATIC_AUTHORIZATION_OUTPUT_DATA = True
  163. USE_DEFAULT_TIMEOUT = False
  164. AUTHENTICATION_DEFAULT_TIMEOUT = 7 * 24 * 60 * 60 # s
  165. PRIVILEGE_COMMAND_WHITELIST = []
  166. CHECK_NODES_IDENTITY = False
  167. DOC_ENGINE = os.environ.get('DOC_ENGINE', "elasticsearch")
  168. if DOC_ENGINE == "elasticsearch":
  169. docStoreConn = rag.utils.es_conn.ESConnection()
  170. elif DOC_ENGINE == "infinity":
  171. docStoreConn = rag.utils.infinity_conn.InfinityConnection()
  172. else:
  173. raise Exception(f"Not supported doc engine: {DOC_ENGINE}")
  174. retrievaler = search.Dealer(docStoreConn)
  175. kg_retrievaler = kg_search.KGSearch(docStoreConn)
  176. class CustomEnum(Enum):
  177. @classmethod
  178. def valid(cls, value):
  179. try:
  180. cls(value)
  181. return True
  182. except BaseException:
  183. return False
  184. @classmethod
  185. def values(cls):
  186. return [member.value for member in cls.__members__.values()]
  187. @classmethod
  188. def names(cls):
  189. return [member.name for member in cls.__members__.values()]
  190. class PythonDependenceName(CustomEnum):
  191. Rag_Source_Code = "python"
  192. Python_Env = "miniconda"
  193. class ModelStorage(CustomEnum):
  194. REDIS = "redis"
  195. MYSQL = "mysql"
  196. class RetCode(IntEnum, CustomEnum):
  197. SUCCESS = 0
  198. NOT_EFFECTIVE = 10
  199. EXCEPTION_ERROR = 100
  200. ARGUMENT_ERROR = 101
  201. DATA_ERROR = 102
  202. OPERATING_ERROR = 103
  203. CONNECTION_ERROR = 105
  204. RUNNING = 106
  205. PERMISSION_ERROR = 108
  206. AUTHENTICATION_ERROR = 109
  207. UNAUTHORIZED = 401
  208. SERVER_ERROR = 500
  209. FORBIDDEN = 403
  210. NOT_FOUND = 404