2024-08-15 09:17:36 +08:00
|
|
|
|
#
|
|
|
|
|
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
2024-11-14 17:13:48 +08:00
|
|
|
|
import logging
|
2024-09-18 14:30:45 +08:00
|
|
|
|
import base64
|
2024-08-15 09:17:36 +08:00
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import uuid
|
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
|
|
from api.db import LLMType, UserTenantRole
|
|
|
|
|
from api.db.db_models import init_database_tables as init_web_db, LLMFactories, LLM, TenantLLM
|
|
|
|
|
from api.db.services import UserService
|
|
|
|
|
from api.db.services.canvas_service import CanvasTemplateService
|
|
|
|
|
from api.db.services.document_service import DocumentService
|
|
|
|
|
from api.db.services.knowledgebase_service import KnowledgebaseService
|
|
|
|
|
from api.db.services.llm_service import LLMFactoriesService, LLMService, TenantLLMService, LLMBundle
|
|
|
|
|
from api.db.services.user_service import TenantService, UserTenantService
|
2024-11-15 17:30:56 +08:00
|
|
|
|
from api import settings
|
2024-08-15 09:17:36 +08:00
|
|
|
|
from api.utils.file_utils import get_project_base_directory
|
|
|
|
|
|
2024-09-25 18:30:27 +08:00
|
|
|
|
|
2024-09-18 14:30:45 +08:00
|
|
|
|
def encode_to_base64(input_string):
|
|
|
|
|
base64_encoded = base64.b64encode(input_string.encode('utf-8'))
|
|
|
|
|
return base64_encoded.decode('utf-8')
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
2024-09-25 18:30:27 +08:00
|
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
|
def init_superuser():
|
|
|
|
|
user_info = {
|
|
|
|
|
"id": uuid.uuid1().hex,
|
2024-09-18 14:30:45 +08:00
|
|
|
|
"password": encode_to_base64("admin"),
|
2024-08-15 09:17:36 +08:00
|
|
|
|
"nickname": "admin",
|
|
|
|
|
"is_superuser": True,
|
|
|
|
|
"email": "admin@ragflow.io",
|
|
|
|
|
"creator": "system",
|
|
|
|
|
"status": "1",
|
|
|
|
|
}
|
|
|
|
|
tenant = {
|
|
|
|
|
"id": user_info["id"],
|
|
|
|
|
"name": user_info["nickname"] + "‘s Kingdom",
|
2024-11-15 17:30:56 +08:00
|
|
|
|
"llm_id": settings.CHAT_MDL,
|
|
|
|
|
"embd_id": settings.EMBEDDING_MDL,
|
|
|
|
|
"asr_id": settings.ASR_MDL,
|
|
|
|
|
"parser_ids": settings.PARSERS,
|
|
|
|
|
"img2txt_id": settings.IMAGE2TEXT_MDL
|
2024-08-15 09:17:36 +08:00
|
|
|
|
}
|
|
|
|
|
usr_tenant = {
|
|
|
|
|
"tenant_id": user_info["id"],
|
|
|
|
|
"user_id": user_info["id"],
|
|
|
|
|
"invited_by": user_info["id"],
|
|
|
|
|
"role": UserTenantRole.OWNER
|
|
|
|
|
}
|
|
|
|
|
tenant_llm = []
|
2024-11-15 17:30:56 +08:00
|
|
|
|
for llm in LLMService.query(fid=settings.LLM_FACTORY):
|
2024-08-15 09:17:36 +08:00
|
|
|
|
tenant_llm.append(
|
2024-11-15 17:30:56 +08:00
|
|
|
|
{"tenant_id": user_info["id"], "llm_factory": settings.LLM_FACTORY, "llm_name": llm.llm_name,
|
|
|
|
|
"model_type": llm.model_type,
|
|
|
|
|
"api_key": settings.API_KEY, "api_base": settings.LLM_BASE_URL})
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
|
if not UserService.save(**user_info):
|
2024-11-14 17:13:48 +08:00
|
|
|
|
logging.error("can't init admin.")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
return
|
|
|
|
|
TenantService.insert(**tenant)
|
|
|
|
|
UserTenantService.insert(**usr_tenant)
|
|
|
|
|
TenantLLMService.insert_many(tenant_llm)
|
2024-11-14 17:13:48 +08:00
|
|
|
|
logging.info(
|
|
|
|
|
"Super user initialized. email: admin@ragflow.io, password: admin. Changing the password after login is strongly recommended.")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
|
chat_mdl = LLMBundle(tenant["id"], LLMType.CHAT, tenant["llm_id"])
|
|
|
|
|
msg = chat_mdl.chat(system="", history=[
|
2024-11-15 17:30:56 +08:00
|
|
|
|
{"role": "user", "content": "Hello!"}], gen_conf={})
|
2024-08-15 09:17:36 +08:00
|
|
|
|
if msg.find("ERROR: ") == 0:
|
2024-11-14 17:13:48 +08:00
|
|
|
|
logging.error(
|
2025-06-18 09:41:09 +08:00
|
|
|
|
"'{}' doesn't work. {}".format(
|
2024-08-15 09:17:36 +08:00
|
|
|
|
tenant["llm_id"],
|
|
|
|
|
msg))
|
|
|
|
|
embd_mdl = LLMBundle(tenant["id"], LLMType.EMBEDDING, tenant["embd_id"])
|
|
|
|
|
v, c = embd_mdl.encode(["Hello!"])
|
|
|
|
|
if c == 0:
|
2024-11-14 17:13:48 +08:00
|
|
|
|
logging.error(
|
2025-06-18 09:41:09 +08:00
|
|
|
|
"'{}' doesn't work!".format(
|
2024-08-15 09:17:36 +08:00
|
|
|
|
tenant["embd_id"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_llm_factory():
|
|
|
|
|
try:
|
|
|
|
|
LLMService.filter_delete([(LLM.fid == "MiniMax" or LLM.fid == "Minimax")])
|
2024-11-14 10:18:25 +08:00
|
|
|
|
LLMService.filter_delete([(LLM.fid == "cohere")])
|
|
|
|
|
LLMFactoriesService.filter_delete([LLMFactories.name == "cohere"])
|
2024-11-12 17:35:13 +08:00
|
|
|
|
except Exception:
|
2024-08-15 09:17:36 +08:00
|
|
|
|
pass
|
|
|
|
|
|
2025-03-14 09:54:38 +08:00
|
|
|
|
factory_llm_infos = settings.FACTORY_LLM_INFOS
|
|
|
|
|
for factory_llm_info in factory_llm_infos:
|
2025-03-14 11:45:44 +08:00
|
|
|
|
info = deepcopy(factory_llm_info)
|
|
|
|
|
llm_infos = info.pop("llm")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
try:
|
2025-03-14 11:45:44 +08:00
|
|
|
|
LLMFactoriesService.save(**info)
|
2024-11-12 17:35:13 +08:00
|
|
|
|
except Exception:
|
2024-08-15 09:17:36 +08:00
|
|
|
|
pass
|
|
|
|
|
LLMService.filter_delete([LLM.fid == factory_llm_info["name"]])
|
|
|
|
|
for llm_info in llm_infos:
|
|
|
|
|
llm_info["fid"] = factory_llm_info["name"]
|
|
|
|
|
try:
|
|
|
|
|
LLMService.save(**llm_info)
|
2024-11-12 17:35:13 +08:00
|
|
|
|
except Exception:
|
2024-08-15 09:17:36 +08:00
|
|
|
|
pass
|
|
|
|
|
|
2025-05-07 19:36:16 +08:00
|
|
|
|
LLMFactoriesService.filter_delete([(LLMFactories.name == "Local") | (LLMFactories.name == "novita.ai")])
|
2024-08-15 09:17:36 +08:00
|
|
|
|
LLMService.filter_delete([LLM.fid == "Local"])
|
|
|
|
|
LLMService.filter_delete([LLM.llm_name == "qwen-vl-max"])
|
|
|
|
|
LLMService.filter_delete([LLM.fid == "Moonshot", LLM.llm_name == "flag-embedding"])
|
|
|
|
|
TenantLLMService.filter_delete([TenantLLM.llm_factory == "Moonshot", TenantLLM.llm_name == "flag-embedding"])
|
|
|
|
|
LLMFactoriesService.filter_delete([LLMFactoriesService.model.name == "QAnything"])
|
|
|
|
|
LLMService.filter_delete([LLMService.model.fid == "QAnything"])
|
|
|
|
|
TenantLLMService.filter_update([TenantLLMService.model.llm_factory == "QAnything"], {"llm_factory": "Youdao"})
|
2024-11-14 10:18:25 +08:00
|
|
|
|
TenantLLMService.filter_update([TenantLLMService.model.llm_factory == "cohere"], {"llm_factory": "Cohere"})
|
2024-08-15 09:17:36 +08:00
|
|
|
|
TenantService.filter_update([1 == 1], {
|
2025-01-22 19:43:14 +08:00
|
|
|
|
"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,audio:Audio,email:Email,tag:Tag"})
|
2024-08-15 09:17:36 +08:00
|
|
|
|
## insert openai two embedding models to the current openai user.
|
2024-10-11 11:29:19 +08:00
|
|
|
|
# print("Start to insert 2 OpenAI embedding models...")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
tenant_ids = set([row["tenant_id"] for row in TenantLLMService.get_openai_models()])
|
|
|
|
|
for tid in tenant_ids:
|
|
|
|
|
for row in TenantLLMService.query(llm_factory="OpenAI", tenant_id=tid):
|
|
|
|
|
row = row.to_dict()
|
|
|
|
|
row["model_type"] = LLMType.EMBEDDING.value
|
|
|
|
|
row["llm_name"] = "text-embedding-3-small"
|
|
|
|
|
row["used_tokens"] = 0
|
|
|
|
|
try:
|
|
|
|
|
TenantLLMService.save(**row)
|
|
|
|
|
row = deepcopy(row)
|
|
|
|
|
row["llm_name"] = "text-embedding-3-large"
|
|
|
|
|
TenantLLMService.save(**row)
|
2024-11-12 17:35:13 +08:00
|
|
|
|
except Exception:
|
2024-08-15 09:17:36 +08:00
|
|
|
|
pass
|
|
|
|
|
break
|
|
|
|
|
for kb_id in KnowledgebaseService.get_all_ids():
|
2025-04-11 20:10:49 +08:00
|
|
|
|
KnowledgebaseService.update_document_number_in_init(kb_id=kb_id, doc_num=DocumentService.get_kb_doc_count(kb_id))
|
2025-01-09 17:07:21 +08:00
|
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_graph_templates():
|
|
|
|
|
dir = os.path.join(get_project_base_directory(), "agent", "templates")
|
|
|
|
|
for fnm in os.listdir(dir):
|
|
|
|
|
try:
|
2025-03-10 10:13:11 +08:00
|
|
|
|
cnvs = json.load(open(os.path.join(dir, fnm), "r",encoding="utf-8"))
|
2024-08-15 09:17:36 +08:00
|
|
|
|
try:
|
|
|
|
|
CanvasTemplateService.save(**cnvs)
|
2024-12-08 14:21:12 +08:00
|
|
|
|
except Exception:
|
2024-08-15 09:17:36 +08:00
|
|
|
|
CanvasTemplateService.update_by_id(cnvs["id"], cnvs)
|
2024-11-12 17:35:13 +08:00
|
|
|
|
except Exception:
|
2024-11-14 17:13:48 +08:00
|
|
|
|
logging.exception("Add graph templates error: ")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_web_data():
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
init_llm_factory()
|
2024-11-15 17:30:56 +08:00
|
|
|
|
# if not UserService.get_all().count():
|
2024-09-25 18:30:27 +08:00
|
|
|
|
# init_superuser()
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
|
add_graph_templates()
|
2024-11-14 17:13:48 +08:00
|
|
|
|
logging.info("init web data success:{}".format(time.time() - start_time))
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
init_web_db()
|
|
|
|
|
init_web_data()
|