| | |
| | | |
| | | |
| | | from Log import logger |
| | | from app.config.agent_base_url import RG_APP_TOKEN_LIST, RG_APP_NEW_TOKEN, DF_CHAT_API_KEY |
| | | # from app.api import pwd_context |
| | | from app.config.const import DIFY, ENV_CONF_PATH, RAGFLOW |
| | | from app.config.const import DIFY, ENV_CONF_PATH, RAGFLOW, smart_server, chat_server, workflow_server, TMP_DICT, \ |
| | | rg_api_token, Dialog_STATSU_ON, SYSTEM_ID |
| | | from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \ |
| | | cipher_suite, UserTokenModel |
| | | cipher_suite, UserTokenModel, ApiTokenModel, ComplexChatModel, SystemDataModel |
| | | from app.service.auth import UserAppDao |
| | | from app.service.bisheng import BishengService |
| | | from app.service.difyService import DifyService |
| | | from app.service.ragflow import RagflowService |
| | | from app.service.service_token import get_new_token |
| | | from app.service.v2.app_driver.chat_data import ChatBaseApply |
| | | from app.service.v2.app_register import AppRegisterDao |
| | | from app.config.config import settings |
| | | from app.utils.password_handle import generate_password |
| | | from app.service.v2.chat import get_app_token |
| | | from app.utils.common import get_machine_id |
| | | from app.utils.password_handle import generate_password, password_encrypted, password_decrypted |
| | | |
| | | pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") |
| | | |
| | |
| | | logger.error(e) |
| | | |
| | | |
| | | async def app_register_sync(db): |
| | | app_dict = {} |
| | | with open(os.path.join(ENV_CONF_PATH, "app_register_conf.json"), 'r', encoding='utf-8') as file: |
| | | # 加载JSON数据 |
| | | app_dict = json.load(file) |
| | | try: |
| | | for app_id, status in app_dict.items(): |
| | | AppRegisterDao(db).update_and_insert_app(app_id, status) |
| | | except Exception as e: |
| | | logger.error(e) |
| | | # async def app_register_sync(db): |
| | | # app_dict = {} |
| | | # with open(os.path.join(ENV_CONF_PATH, "app_register_conf.json"), 'r', encoding='utf-8') as file: |
| | | # # 加载JSON数据 |
| | | # app_dict = json.load(file) |
| | | # try: |
| | | # for app_id, status in app_dict.items(): |
| | | # AppRegisterDao(db).update_and_insert_app(app_id, status) |
| | | # except Exception as e: |
| | | # logger.error(e) |
| | | |
| | | |
| | | async def basic_agent_sync(db): |
| | | agent_list = [] |
| | | complex_list = [] |
| | | with open(os.path.join(ENV_CONF_PATH, "default_agent_conf.json"), 'r', encoding='utf-8') as file: |
| | | # 加载JSON数据 |
| | | agent_dict = json.load(file) |
| | | agent_list = agent_dict.get("basic", []) |
| | | complex_list = agent_dict.get("complex", []) |
| | | user = db.query(UserModel).filter_by(permission="admin").first() |
| | | for agent in agent_list: |
| | | dialog = db.query(DialogModel).filter(DialogModel.id == agent["id"]).first() |
| | |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | now_complex_list = [] |
| | | for agent in complex_list: |
| | | now_complex_list.append(agent["id"]) |
| | | dialog = db.query(ComplexChatModel).filter(ComplexChatModel.id == agent["id"]).first() |
| | | if dialog: |
| | | try: |
| | | dialog.name = agent["name"] |
| | | dialog.description = agent["description"] |
| | | dialog.icon = agent["icon"] |
| | | dialog.mode = agent["mode"] |
| | | dialog.chat_mode = agent["chat_mode"] |
| | | dialog.status = Dialog_STATSU_ON |
| | | # dialog.parameters = json.dumps(agent["parameters"]) |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | | else: |
| | | try: |
| | | dialog = ComplexChatModel(id=agent["id"], name=agent["name"], description=agent["description"], |
| | | icon=agent["icon"], tenant_id=user.id if user else "", dialog_type=agent["dialogType"], mode=agent["mode"],chat_mode = agent["chat_mode"],chat_model = agent.get("chat_model"),chat_model_ds = agent.get("chat_model_ds"),chat_provider = agent.get("chat_provider")) |
| | | db.add(dialog) |
| | | db.commit() |
| | | db.refresh(dialog) |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | |
| | | for i in db.query(ComplexChatModel).filter(ComplexChatModel.status == "1").all(): |
| | | if i.id not in now_complex_list: |
| | | try: |
| | | db.query(ComplexChatModel).filter(ComplexChatModel.id==i.id).update(({"status": "0"})) |
| | | db.commit() |
| | | except: |
| | | ... |
| | | |
| | | |
| | | |
| | | async def user_update_app(userid, db): |
| | |
| | | async def admin_account_sync(db): |
| | | try: |
| | | config = {} |
| | | app_dict = {} |
| | | # tmp_dict = {chat_server:RAGFLOW, workflow_server:DIFY} |
| | | now_account =[] |
| | | with open(os.path.join(ENV_CONF_PATH, "account.yaml"), 'r', encoding='utf-8') as file: |
| | | with open(os.path.join(ENV_CONF_PATH, "admin.yaml"), 'r', encoding='utf-8') as file: |
| | | # 加载JSON数据 |
| | | config = yaml.safe_load(file) |
| | | account_list = db.query(UserTokenModel).all() |
| | |
| | | now_account.append(account.id) |
| | | else: |
| | | db.query(UserTokenModel).filter_by(id=account.id).delete() |
| | | |
| | | for k, v in config.items(): |
| | | if k not in now_account: |
| | | new_account = UserTokenModel(id=k, account=v["account"], password=v["password"]) |
| | | db.add(new_account) |
| | | if k in TMP_DICT: |
| | | app_dict[TMP_DICT[k]] = v.get("id") |
| | | if k == smart_server: |
| | | db_user = db.query(UserModel).filter(UserModel.username == config["smart_server"]["account"]).first() |
| | | if db_user: |
| | | print("admin_user_sync: 用户已经存在!") |
| | | continue |
| | | hashed_password = pwd_context.hash(await password_decrypted(config["smart_server"]["password"])) # config["smart_server"]["password"] |
| | | user_model = UserModel(username=config["smart_server"]["account"], hashed_password=hashed_password, |
| | | email="", |
| | | phone="", login_name="", sync_flag="", creator=0, permission="admin") |
| | | db.add(user_model) |
| | | # db.commit() |
| | | # db.refresh(user_model) |
| | | else: |
| | | if k not in now_account: |
| | | new_account = UserTokenModel(id=k, account=v["account"], password=v["password"]) |
| | | db.add(new_account) |
| | | |
| | | db.commit() |
| | | |
| | | # with open(os.path.join(ENV_CONF_PATH, "app_register_conf.json"), 'r', encoding='utf-8') as file: |
| | | # # 加载JSON数据 |
| | | # app_dict = json.load(file) |
| | | try: |
| | | for app_id, name in app_dict.items(): |
| | | AppRegisterDao(db).update_and_insert_app(app_id, 1, name) |
| | | except Exception as e: |
| | | logger.error(e) |
| | | |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | |
| | | if db_user: |
| | | print("admin_user_sync: 用户已经存在!") |
| | | return |
| | | register_dict = {} |
| | | |
| | | for app in [RAGFLOW, DIFY]: |
| | | register_dict[app] = {"id": config[app].get("id", "123"), "name": config[app]["account"], |
| | | "pwd":config[app]["password"], |
| | | "email": config[app]["account"]} |
| | | # register_dict = {} |
| | | # |
| | | # for app in [RAGFLOW, DIFY]: |
| | | # register_dict[app] = {"id": config[app].get("id", "123"), "name": config[app]["account"], |
| | | # "pwd":config[app]["password"], |
| | | # "email": config[app]["account"]} |
| | | |
| | | # 存储用户信息 |
| | | hashed_password = pwd_context.hash(config["smart_server"]["password"]) |
| | |
| | | db.add(user_model) |
| | | db.commit() |
| | | db.refresh(user_model) |
| | | u_id = user_model.id |
| | | user_app_dao = UserAppDao(db) |
| | | for k, v in register_dict.items(): |
| | | await user_app_dao.update_and_insert_data(v.get("name"), user_model.encrypted_password(v.get("pwd")), v.get("email"), u_id, str(v.get("id")), k) |
| | | # u_id = user_model.id |
| | | # user_app_dao = UserAppDao(db) |
| | | # for k, v in register_dict.items(): |
| | | # await user_app_dao.update_and_insert_data(v.get("name"), user_model.encrypted_password(v.get("pwd")), v.get("email"), u_id, str(v.get("id")), k) |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | db.rollback() |
| | | |
| | | |
| | | async def sync_rg_api_token(db): |
| | | token = "" |
| | | try: |
| | | app_token = db.query(ApiTokenModel).filter_by(app_id=rg_api_token).first() |
| | | if app_token: |
| | | print("rg_api_token: 已经存在!") |
| | | return |
| | | user_token = db.query(UserTokenModel).filter(UserTokenModel.id == chat_server).first() |
| | | chat = ChatBaseApply() |
| | | token_list_url = f"{settings.fwr_base_url}{RG_APP_TOKEN_LIST}" |
| | | token_list = await chat.chat_get(token_list_url, {}, await chat.get_chat_headers(user_token.access_token)) |
| | | if token_list and token_list.get("code") == 0: |
| | | if len(token_list.get("data", [])) == 0: |
| | | print("rg_api_token: 创建成功!") |
| | | new_token_url = f"{settings.fwr_base_url}{RG_APP_NEW_TOKEN}" |
| | | new_token = await chat.chat_post(new_token_url, {}, await chat.get_chat_headers(user_token.access_token)) |
| | | if new_token and new_token.get("code") == 0: |
| | | token = new_token.get("data", {}).get("token") |
| | | else: |
| | | token = token_list.get("data")[0].get("token") |
| | | print("rg_api_token: 已有token!") |
| | | if token: |
| | | |
| | | db.add(ApiTokenModel(id=rg_api_token, app_id=rg_api_token, type="platform", token=token)) |
| | | db.commit() |
| | | print("rg_api_token: 更新成功!") |
| | | |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | |
| | | |
| | | |
| | | async def sync_complex_api_token(db): |
| | | token = "" |
| | | try: |
| | | complex_list = db.query(ComplexChatModel).all() |
| | | for i in complex_list: |
| | | user_token = db.query(ApiTokenModel).filter(ApiTokenModel.app_id == i.id).first() |
| | | if not user_token: |
| | | try: |
| | | chat = ChatBaseApply() |
| | | url = settings.dify_base_url + DF_CHAT_API_KEY.format(i.id) |
| | | access_token = await get_app_token(db, workflow_server) |
| | | param = await chat.chat_get(url, {}, await chat.get_headers(access_token)) |
| | | if param and param.get("data"): |
| | | token = param.get("data", [{}])[0].get("token") |
| | | token_id = param.get("data", [{}])[0].get("id") |
| | | # dialog.parameters = json.dumps(param) |
| | | else: |
| | | param = await chat.chat_post(url, {}, await chat.get_headers(access_token)) |
| | | if param: |
| | | token = param.get("token") |
| | | token_id = param.get("id") |
| | | |
| | | if token: |
| | | db.add(ApiTokenModel(id=token_id, app_id=i.id, type="app", token=token)) |
| | | db.commit() |
| | | print("df_api_token: 更新成功!") |
| | | except Exception as e: |
| | | print(e) |
| | | |
| | | |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | |
| | | |
| | | async def system_license_sync(db): |
| | | with open(os.path.join(ENV_CONF_PATH, "system.yaml") , 'r', encoding='utf-8') as file: |
| | | # 加载JSON数据 |
| | | config = yaml.safe_load(file) |
| | | try: |
| | | system = db.query(SystemDataModel).filter_by(id=SYSTEM_ID).first() |
| | | if system: |
| | | system.version = config["smart_system"].get("version") |
| | | system.machine_id = get_machine_id() |
| | | else: |
| | | system = SystemDataModel(id=SYSTEM_ID, version=config["smart_system"].get("version"), title=config["smart_system"].get("title"), desc=config["smart_system"].get("desc"), machine_id=get_machine_id()) |
| | | db.add(system) |
| | | db.commit() |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |