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.

init_data.py 14KB

1 vuosi sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. import time
  18. import uuid
  19. from api.db import LLMType, UserTenantRole
  20. from api.db.db_models import init_database_tables as init_web_db, LLMFactories, LLM, TenantLLM
  21. from api.db.services import UserService
  22. from api.db.services.llm_service import LLMFactoriesService, LLMService, TenantLLMService, LLMBundle
  23. from api.db.services.user_service import TenantService, UserTenantService
  24. from api.settings import CHAT_MDL, EMBEDDING_MDL, ASR_MDL, IMAGE2TEXT_MDL, PARSERS, LLM_FACTORY, API_KEY, LLM_BASE_URL
  25. def init_superuser():
  26. user_info = {
  27. "id": uuid.uuid1().hex,
  28. "password": "admin",
  29. "nickname": "admin",
  30. "is_superuser": True,
  31. "email": "admin@ragflow.io",
  32. "creator": "system",
  33. "status": "1",
  34. }
  35. tenant = {
  36. "id": user_info["id"],
  37. "name": user_info["nickname"] + "‘s Kingdom",
  38. "llm_id": CHAT_MDL,
  39. "embd_id": EMBEDDING_MDL,
  40. "asr_id": ASR_MDL,
  41. "parser_ids": PARSERS,
  42. "img2txt_id": IMAGE2TEXT_MDL
  43. }
  44. usr_tenant = {
  45. "tenant_id": user_info["id"],
  46. "user_id": user_info["id"],
  47. "invited_by": user_info["id"],
  48. "role": UserTenantRole.OWNER
  49. }
  50. tenant_llm = []
  51. for llm in LLMService.query(fid=LLM_FACTORY):
  52. tenant_llm.append(
  53. {"tenant_id": user_info["id"], "llm_factory": LLM_FACTORY, "llm_name": llm.llm_name, "model_type": llm.model_type,
  54. "api_key": API_KEY, "api_base": LLM_BASE_URL})
  55. if not UserService.save(**user_info):
  56. print("\033[93m【ERROR】\033[0mcan't init admin.")
  57. return
  58. TenantService.insert(**tenant)
  59. UserTenantService.insert(**usr_tenant)
  60. TenantLLMService.insert_many(tenant_llm)
  61. print(
  62. "【INFO】Super user initialized. \033[93memail: admin@ragflow.io, password: admin\033[0m. Changing the password after logining is strongly recomanded.")
  63. chat_mdl = LLMBundle(tenant["id"], LLMType.CHAT, tenant["llm_id"])
  64. msg = chat_mdl.chat(system="", history=[
  65. {"role": "user", "content": "Hello!"}], gen_conf={})
  66. if msg.find("ERROR: ") == 0:
  67. print(
  68. "\33[91m【ERROR】\33[0m: ",
  69. "'{}' dosen't work. {}".format(
  70. tenant["llm_id"],
  71. msg))
  72. embd_mdl = LLMBundle(tenant["id"], LLMType.EMBEDDING, tenant["embd_id"])
  73. v, c = embd_mdl.encode(["Hello!"])
  74. if c == 0:
  75. print(
  76. "\33[91m【ERROR】\33[0m:",
  77. " '{}' dosen't work!".format(
  78. tenant["embd_id"]))
  79. factory_infos = [{
  80. "name": "OpenAI",
  81. "logo": "",
  82. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  83. "status": "1",
  84. }, {
  85. "name": "Tongyi-Qianwen",
  86. "logo": "",
  87. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  88. "status": "1",
  89. }, {
  90. "name": "ZHIPU-AI",
  91. "logo": "",
  92. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  93. "status": "1",
  94. },
  95. {
  96. "name": "Ollama",
  97. "logo": "",
  98. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  99. "status": "1",
  100. }, {
  101. "name": "Moonshot",
  102. "logo": "",
  103. "tags": "LLM,TEXT EMBEDDING",
  104. "status": "1",
  105. }, {
  106. "name": "FastEmbed",
  107. "logo": "",
  108. "tags": "TEXT EMBEDDING",
  109. "status": "1",
  110. }, {
  111. "name": "Xinference",
  112. "logo": "",
  113. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  114. "status": "1",
  115. },{
  116. "name": "Youdao",
  117. "logo": "",
  118. "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  119. "status": "1",
  120. },{
  121. "name": "DeepSeek",
  122. "logo": "",
  123. "tags": "LLM",
  124. "status": "1",
  125. },
  126. # {
  127. # "name": "文心一言",
  128. # "logo": "",
  129. # "tags": "LLM,TEXT EMBEDDING,SPEECH2TEXT,MODERATION",
  130. # "status": "1",
  131. # },
  132. ]
  133. def init_llm_factory():
  134. llm_infos = [
  135. # ---------------------- OpenAI ------------------------
  136. {
  137. "fid": factory_infos[0]["name"],
  138. "llm_name": "gpt-4o",
  139. "tags": "LLM,CHAT,128K",
  140. "max_tokens": 128000,
  141. "model_type": LLMType.CHAT.value + "," + LLMType.IMAGE2TEXT.value
  142. }, {
  143. "fid": factory_infos[0]["name"],
  144. "llm_name": "gpt-3.5-turbo",
  145. "tags": "LLM,CHAT,4K",
  146. "max_tokens": 4096,
  147. "model_type": LLMType.CHAT.value
  148. }, {
  149. "fid": factory_infos[0]["name"],
  150. "llm_name": "gpt-3.5-turbo-16k-0613",
  151. "tags": "LLM,CHAT,16k",
  152. "max_tokens": 16385,
  153. "model_type": LLMType.CHAT.value
  154. }, {
  155. "fid": factory_infos[0]["name"],
  156. "llm_name": "text-embedding-ada-002",
  157. "tags": "TEXT EMBEDDING,8K",
  158. "max_tokens": 8191,
  159. "model_type": LLMType.EMBEDDING.value
  160. }, {
  161. "fid": factory_infos[0]["name"],
  162. "llm_name": "whisper-1",
  163. "tags": "SPEECH2TEXT",
  164. "max_tokens": 25 * 1024 * 1024,
  165. "model_type": LLMType.SPEECH2TEXT.value
  166. }, {
  167. "fid": factory_infos[0]["name"],
  168. "llm_name": "gpt-4",
  169. "tags": "LLM,CHAT,8K",
  170. "max_tokens": 8191,
  171. "model_type": LLMType.CHAT.value
  172. }, {
  173. "fid": factory_infos[0]["name"],
  174. "llm_name": "gpt-4-turbo",
  175. "tags": "LLM,CHAT,8K",
  176. "max_tokens": 8191,
  177. "model_type": LLMType.CHAT.value
  178. },{
  179. "fid": factory_infos[0]["name"],
  180. "llm_name": "gpt-4-32k",
  181. "tags": "LLM,CHAT,32K",
  182. "max_tokens": 32768,
  183. "model_type": LLMType.CHAT.value
  184. }, {
  185. "fid": factory_infos[0]["name"],
  186. "llm_name": "gpt-4-vision-preview",
  187. "tags": "LLM,CHAT,IMAGE2TEXT",
  188. "max_tokens": 765,
  189. "model_type": LLMType.IMAGE2TEXT.value
  190. },
  191. # ----------------------- Qwen -----------------------
  192. {
  193. "fid": factory_infos[1]["name"],
  194. "llm_name": "qwen-turbo",
  195. "tags": "LLM,CHAT,8K",
  196. "max_tokens": 8191,
  197. "model_type": LLMType.CHAT.value
  198. }, {
  199. "fid": factory_infos[1]["name"],
  200. "llm_name": "qwen-plus",
  201. "tags": "LLM,CHAT,32K",
  202. "max_tokens": 32768,
  203. "model_type": LLMType.CHAT.value
  204. }, {
  205. "fid": factory_infos[1]["name"],
  206. "llm_name": "qwen-max-1201",
  207. "tags": "LLM,CHAT,6K",
  208. "max_tokens": 5899,
  209. "model_type": LLMType.CHAT.value
  210. }, {
  211. "fid": factory_infos[1]["name"],
  212. "llm_name": "text-embedding-v2",
  213. "tags": "TEXT EMBEDDING,2K",
  214. "max_tokens": 2048,
  215. "model_type": LLMType.EMBEDDING.value
  216. }, {
  217. "fid": factory_infos[1]["name"],
  218. "llm_name": "paraformer-realtime-8k-v1",
  219. "tags": "SPEECH2TEXT",
  220. "max_tokens": 25 * 1024 * 1024,
  221. "model_type": LLMType.SPEECH2TEXT.value
  222. }, {
  223. "fid": factory_infos[1]["name"],
  224. "llm_name": "qwen-vl-max",
  225. "tags": "LLM,CHAT,IMAGE2TEXT",
  226. "max_tokens": 765,
  227. "model_type": LLMType.IMAGE2TEXT.value
  228. },
  229. # ---------------------- ZhipuAI ----------------------
  230. {
  231. "fid": factory_infos[2]["name"],
  232. "llm_name": "glm-3-turbo",
  233. "tags": "LLM,CHAT,",
  234. "max_tokens": 128 * 1000,
  235. "model_type": LLMType.CHAT.value
  236. }, {
  237. "fid": factory_infos[2]["name"],
  238. "llm_name": "glm-4",
  239. "tags": "LLM,CHAT,",
  240. "max_tokens": 128 * 1000,
  241. "model_type": LLMType.CHAT.value
  242. }, {
  243. "fid": factory_infos[2]["name"],
  244. "llm_name": "glm-4v",
  245. "tags": "LLM,CHAT,IMAGE2TEXT",
  246. "max_tokens": 2000,
  247. "model_type": LLMType.IMAGE2TEXT.value
  248. },
  249. {
  250. "fid": factory_infos[2]["name"],
  251. "llm_name": "embedding-2",
  252. "tags": "TEXT EMBEDDING",
  253. "max_tokens": 512,
  254. "model_type": LLMType.EMBEDDING.value
  255. },
  256. # ------------------------ Moonshot -----------------------
  257. {
  258. "fid": factory_infos[4]["name"],
  259. "llm_name": "moonshot-v1-8k",
  260. "tags": "LLM,CHAT,",
  261. "max_tokens": 7900,
  262. "model_type": LLMType.CHAT.value
  263. }, {
  264. "fid": factory_infos[4]["name"],
  265. "llm_name": "moonshot-v1-32k",
  266. "tags": "LLM,CHAT,",
  267. "max_tokens": 32768,
  268. "model_type": LLMType.CHAT.value
  269. }, {
  270. "fid": factory_infos[4]["name"],
  271. "llm_name": "moonshot-v1-128k",
  272. "tags": "LLM,CHAT",
  273. "max_tokens": 128 * 1000,
  274. "model_type": LLMType.CHAT.value
  275. },
  276. # ------------------------ FastEmbed -----------------------
  277. {
  278. "fid": factory_infos[5]["name"],
  279. "llm_name": "BAAI/bge-small-en-v1.5",
  280. "tags": "TEXT EMBEDDING,",
  281. "max_tokens": 512,
  282. "model_type": LLMType.EMBEDDING.value
  283. }, {
  284. "fid": factory_infos[5]["name"],
  285. "llm_name": "BAAI/bge-small-zh-v1.5",
  286. "tags": "TEXT EMBEDDING,",
  287. "max_tokens": 512,
  288. "model_type": LLMType.EMBEDDING.value
  289. }, {
  290. }, {
  291. "fid": factory_infos[5]["name"],
  292. "llm_name": "BAAI/bge-base-en-v1.5",
  293. "tags": "TEXT EMBEDDING,",
  294. "max_tokens": 512,
  295. "model_type": LLMType.EMBEDDING.value
  296. }, {
  297. }, {
  298. "fid": factory_infos[5]["name"],
  299. "llm_name": "BAAI/bge-large-en-v1.5",
  300. "tags": "TEXT EMBEDDING,",
  301. "max_tokens": 512,
  302. "model_type": LLMType.EMBEDDING.value
  303. }, {
  304. "fid": factory_infos[5]["name"],
  305. "llm_name": "sentence-transformers/all-MiniLM-L6-v2",
  306. "tags": "TEXT EMBEDDING,",
  307. "max_tokens": 512,
  308. "model_type": LLMType.EMBEDDING.value
  309. }, {
  310. "fid": factory_infos[5]["name"],
  311. "llm_name": "nomic-ai/nomic-embed-text-v1.5",
  312. "tags": "TEXT EMBEDDING,",
  313. "max_tokens": 8192,
  314. "model_type": LLMType.EMBEDDING.value
  315. }, {
  316. "fid": factory_infos[5]["name"],
  317. "llm_name": "jinaai/jina-embeddings-v2-small-en",
  318. "tags": "TEXT EMBEDDING,",
  319. "max_tokens": 2147483648,
  320. "model_type": LLMType.EMBEDDING.value
  321. }, {
  322. "fid": factory_infos[5]["name"],
  323. "llm_name": "jinaai/jina-embeddings-v2-base-en",
  324. "tags": "TEXT EMBEDDING,",
  325. "max_tokens": 2147483648,
  326. "model_type": LLMType.EMBEDDING.value
  327. },
  328. # ------------------------ Youdao -----------------------
  329. {
  330. "fid": factory_infos[7]["name"],
  331. "llm_name": "maidalun1020/bce-embedding-base_v1",
  332. "tags": "TEXT EMBEDDING,",
  333. "max_tokens": 512,
  334. "model_type": LLMType.EMBEDDING.value
  335. },
  336. # ------------------------ DeepSeek -----------------------
  337. {
  338. "fid": factory_infos[8]["name"],
  339. "llm_name": "deepseek-chat",
  340. "tags": "LLM,CHAT,",
  341. "max_tokens": 32768,
  342. "model_type": LLMType.CHAT.value
  343. },
  344. {
  345. "fid": factory_infos[8]["name"],
  346. "llm_name": "deepseek-coder",
  347. "tags": "LLM,CHAT,",
  348. "max_tokens": 16385,
  349. "model_type": LLMType.CHAT.value
  350. },
  351. ]
  352. for info in factory_infos:
  353. try:
  354. LLMFactoriesService.save(**info)
  355. except Exception as e:
  356. pass
  357. for info in llm_infos:
  358. try:
  359. LLMService.save(**info)
  360. except Exception as e:
  361. pass
  362. LLMFactoriesService.filter_delete([LLMFactories.name == "Local"])
  363. LLMService.filter_delete([LLM.fid == "Local"])
  364. LLMService.filter_delete([LLM.fid == "Moonshot", LLM.llm_name == "flag-embedding"])
  365. TenantLLMService.filter_delete([TenantLLM.llm_factory == "Moonshot", TenantLLM.llm_name == "flag-embedding"])
  366. LLMFactoriesService.filter_delete([LLMFactoriesService.model.name == "QAnything"])
  367. LLMService.filter_delete([LLMService.model.fid == "QAnything"])
  368. TenantLLMService.filter_update([TenantLLMService.model.llm_factory == "QAnything"], {"llm_factory": "Youdao"})
  369. """
  370. drop table llm;
  371. drop table llm_factories;
  372. update tenant set parser_ids='naive:General,qa:Q&A,resume:Resume,manual:Manual,table:Table,paper:Paper,book:Book,laws:Laws,presentation:Presentation,picture:Picture,one:One';
  373. alter table knowledgebase modify avatar longtext;
  374. alter table user modify avatar longtext;
  375. alter table dialog modify icon longtext;
  376. """
  377. def init_web_data():
  378. start_time = time.time()
  379. init_llm_factory()
  380. if not UserService.get_all().count():
  381. init_superuser()
  382. print("init web data success:{}".format(time.time() - start_time))
  383. if __name__ == '__main__':
  384. init_web_db()
  385. init_web_data()