Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

init_data.py 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 time
  17. import uuid
  18. from api.db import LLMType, UserTenantRole
  19. from api.db.db_models import init_database_tables as init_web_db
  20. from api.db.services import UserService
  21. from api.db.services.llm_service import LLMFactoriesService, LLMService, TenantLLMService, LLMBundle
  22. from api.db.services.user_service import TenantService, UserTenantService
  23. from api.settings import CHAT_MDL, EMBEDDING_MDL, ASR_MDL, IMAGE2TEXT_MDL, PARSERS, LLM_FACTORY, API_KEY
  24. def init_superuser():
  25. user_info = {
  26. "id": uuid.uuid1().hex,
  27. "password": "admin",
  28. "nickname": "admin",
  29. "is_superuser": True,
  30. "email": "admin@ragflow.io",
  31. "creator": "system",
  32. "status": "1",
  33. }
  34. tenant = {
  35. "id": user_info["id"],
  36. "name": user_info["nickname"] + "‘s Kingdom",
  37. "llm_id": CHAT_MDL,
  38. "embd_id": EMBEDDING_MDL,
  39. "asr_id": ASR_MDL,
  40. "parser_ids": PARSERS,
  41. "img2txt_id": IMAGE2TEXT_MDL
  42. }
  43. usr_tenant = {
  44. "tenant_id": user_info["id"],
  45. "user_id": user_info["id"],
  46. "invited_by": user_info["id"],
  47. "role": UserTenantRole.OWNER
  48. }
  49. tenant_llm = []
  50. for llm in LLMService.query(fid=LLM_FACTORY):
  51. tenant_llm.append(
  52. {"tenant_id": user_info["id"], "llm_factory": LLM_FACTORY, "llm_name": llm.llm_name, "model_type": llm.model_type,
  53. "api_key": API_KEY})
  54. if not UserService.save(**user_info):
  55. print("\033[93m【ERROR】\033[0mcan't init admin.")
  56. return
  57. TenantService.insert(**tenant)
  58. UserTenantService.insert(**usr_tenant)
  59. TenantLLMService.insert_many(tenant_llm)
  60. print("【INFO】Super user initialized. \033[93memail: admin@ragflow.io, password: admin\033[0m. Changing the password after logining is strongly recomanded.")
  61. chat_mdl = LLMBundle(tenant["id"], LLMType.CHAT, tenant["llm_id"])
  62. msg = chat_mdl.chat(system="", history=[{"role": "user", "content": "Hello!"}], gen_conf={})
  63. if msg.find("ERROR: ") == 0:
  64. print("\33[91m【ERROR】\33[0m: ", "'{}' dosen't work. {}".format(tenant["llm_id"], msg))
  65. embd_mdl = LLMBundle(tenant["id"], LLMType.EMBEDDING, tenant["embd_id"])
  66. v, c = embd_mdl.encode(["Hello!"])
  67. if c == 0:
  68. print("\33[91m【ERROR】\33[0m:", " '{}' dosen't work!".format(tenant["embd_id"]))
  69. def init_llm_factory():
  70. factory_infos = [{
  71. "name": "OpenAI",
  72. "logo": "",
  73. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  74. "status": "1",
  75. },{
  76. "name": "通义千问",
  77. "logo": "",
  78. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  79. "status": "1",
  80. },{
  81. "name": "智谱AI",
  82. "logo": "",
  83. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  84. "status": "1",
  85. },
  86. # {
  87. # "name": "文心一言",
  88. # "logo": "",
  89. # "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  90. # "status": "1",
  91. # },
  92. ]
  93. llm_infos = [
  94. # ---------------------- OpenAI ------------------------
  95. {
  96. "fid": factory_infos[0]["name"],
  97. "llm_name": "gpt-3.5-turbo",
  98. "tags": "LLM,CHAT,4K",
  99. "max_tokens": 4096,
  100. "model_type": LLMType.CHAT.value
  101. },{
  102. "fid": factory_infos[0]["name"],
  103. "llm_name": "gpt-3.5-turbo-16k-0613",
  104. "tags": "LLM,CHAT,16k",
  105. "max_tokens": 16385,
  106. "model_type": LLMType.CHAT.value
  107. },{
  108. "fid": factory_infos[0]["name"],
  109. "llm_name": "text-embedding-ada-002",
  110. "tags": "TEXT EMBEDDING,8K",
  111. "max_tokens": 8191,
  112. "model_type": LLMType.EMBEDDING.value
  113. },{
  114. "fid": factory_infos[0]["name"],
  115. "llm_name": "whisper-1",
  116. "tags": "SPEECH2TEXT",
  117. "max_tokens": 25*1024*1024,
  118. "model_type": LLMType.SPEECH2TEXT.value
  119. },{
  120. "fid": factory_infos[0]["name"],
  121. "llm_name": "gpt-4",
  122. "tags": "LLM,CHAT,8K",
  123. "max_tokens": 8191,
  124. "model_type": LLMType.CHAT.value
  125. },{
  126. "fid": factory_infos[0]["name"],
  127. "llm_name": "gpt-4-32k",
  128. "tags": "LLM,CHAT,32K",
  129. "max_tokens": 32768,
  130. "model_type": LLMType.CHAT.value
  131. },{
  132. "fid": factory_infos[0]["name"],
  133. "llm_name": "gpt-4-vision-preview",
  134. "tags": "LLM,CHAT,IMAGE2TEXT",
  135. "max_tokens": 765,
  136. "model_type": LLMType.IMAGE2TEXT.value
  137. },
  138. # ----------------------- Qwen -----------------------
  139. {
  140. "fid": factory_infos[1]["name"],
  141. "llm_name": "qwen-turbo",
  142. "tags": "LLM,CHAT,8K",
  143. "max_tokens": 8191,
  144. "model_type": LLMType.CHAT.value
  145. },{
  146. "fid": factory_infos[1]["name"],
  147. "llm_name": "qwen-plus",
  148. "tags": "LLM,CHAT,32K",
  149. "max_tokens": 32768,
  150. "model_type": LLMType.CHAT.value
  151. },{
  152. "fid": factory_infos[1]["name"],
  153. "llm_name": "text-embedding-v2",
  154. "tags": "TEXT EMBEDDING,2K",
  155. "max_tokens": 2048,
  156. "model_type": LLMType.EMBEDDING.value
  157. },{
  158. "fid": factory_infos[1]["name"],
  159. "llm_name": "paraformer-realtime-8k-v1",
  160. "tags": "SPEECH2TEXT",
  161. "max_tokens": 25*1024*1024,
  162. "model_type": LLMType.SPEECH2TEXT.value
  163. },{
  164. "fid": factory_infos[1]["name"],
  165. "llm_name": "qwen-vl-max",
  166. "tags": "LLM,CHAT,IMAGE2TEXT",
  167. "max_tokens": 765,
  168. "model_type": LLMType.IMAGE2TEXT.value
  169. },
  170. # ---------------------- ZhipuAI ----------------------
  171. {
  172. "fid": factory_infos[2]["name"],
  173. "llm_name": "glm-3-turbo",
  174. "tags": "LLM,CHAT,",
  175. "max_tokens": 128 * 1000,
  176. "model_type": LLMType.CHAT.value
  177. }, {
  178. "fid": factory_infos[2]["name"],
  179. "llm_name": "glm-4",
  180. "tags": "LLM,CHAT,",
  181. "max_tokens": 128 * 1000,
  182. "model_type": LLMType.CHAT.value
  183. }, {
  184. "fid": factory_infos[2]["name"],
  185. "llm_name": "glm-4v",
  186. "tags": "LLM,CHAT,IMAGE2TEXT",
  187. "max_tokens": 2000,
  188. "model_type": LLMType.IMAGE2TEXT.value
  189. },
  190. {
  191. "fid": factory_infos[2]["name"],
  192. "llm_name": "embedding-2",
  193. "tags": "TEXT EMBEDDING",
  194. "max_tokens": 512,
  195. "model_type": LLMType.EMBEDDING.value
  196. },
  197. ]
  198. for info in factory_infos:
  199. LLMFactoriesService.save(**info)
  200. for info in llm_infos:
  201. LLMService.save(**info)
  202. def init_web_data():
  203. start_time = time.time()
  204. if not LLMService.get_all().count():init_llm_factory()
  205. if not UserService.get_all().count():
  206. init_superuser()
  207. print("init web data success:{}".format(time.time() - start_time))
  208. if __name__ == '__main__':
  209. init_web_db()
  210. init_web_data()