| | |
| | | from sqlalchemy.orm import sessionmaker, Session |
| | | |
| | | from app.config.config import settings |
| | | from app.config.const import RAGFLOW, BISHENG, DIFY, ENV_CONF_PATH |
| | | from app.models import KnowledgeModel |
| | | from app.config.const import RAGFLOW, BISHENG, DIFY, ENV_CONF_PATH, Dialog_STATSU_DELETE, Dialog_STATSU_ON |
| | | from app.models import KnowledgeModel, ComplexChatDao |
| | | from app.models.dialog_model import DialogModel |
| | | from app.models.user_model import UserAppModel |
| | | from app.models.agent_model import AgentModel |
| | | from app.models.base_model import SessionLocal, Base |
| | | from app.models.resource_model import ResourceModel |
| | | from app.models.resource_model import ResourceModel, ResourceTypeModel |
| | | from app.service.v2.app_register import AppRegisterDao |
| | | |
| | | # 创建数据库引擎和会话工厂 |
| | |
| | | status = Column(String(1), nullable=False) |
| | | description = Column(String(255), nullable=False) |
| | | tenant_id = Column(String(36), nullable=False) |
| | | kb_ids = Column(String(128), nullable=False) |
| | | |
| | | |
| | | class DfApps(Base): |
| | |
| | | description = Column(Text) # 说明 |
| | | status = Column(String(1)) # 状态 |
| | | doc_num = Column(Integer) # 文档 |
| | | embd_id = Column(String(128)) # 文档 |
| | | |
| | | |
| | | class RgUserTenant(Base): |
| | |
| | | ('basic_excel_talk', 6, '智能数据', 'BASIC', 'excelTalk'), |
| | | ('basic_question_talk', 7, '出题组卷', 'BASIC', 'questionTalk'), |
| | | ('9d75142a-66eb-4e23-b7d4-03efe4584915', 8, '小数绘图', 'DIFY', 'imageTalk'), |
| | | ('basic_paper_talk', 9, '文档出卷', 'BASIC', 'paperTalk'), |
| | | ('2f6ddf93-7ba6-4b2d-b991-d96421404600', 9, '文档出卷', 'DIFY', 'paperTalk'), |
| | | ('basic_report_clean', 10, '文档报告', 'DIFY', 'reportWorkflow') |
| | | ] |
| | | |
| | |
| | | db.close() |
| | | |
| | | |
| | | def get_data_from_ragflow_v2(names: List[str]) -> List[Dict]: |
| | | def get_data_from_ragflow_v2(base_db, names: List[str], tenant_id) -> List[Dict]: |
| | | db = SessionRagflow() |
| | | para = { |
| | | "user_input_form": [], |
| | | "retriever_resource": { |
| | | "enabled": True |
| | | }, |
| | | "file_upload": { |
| | | "enabled": False |
| | | } |
| | | } |
| | | try: |
| | | chat_ids = ComplexChatDao(base_db).get_complex_chat_ids() |
| | | # print(chat_ids) |
| | | if names: |
| | | query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id) \ |
| | | .filter(Dialog.name.in_(names), Dialog.status == "1") |
| | | else: |
| | | query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id).filter( |
| | | Dialog.status == "1") |
| | | query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id, Dialog.kb_ids).filter( |
| | | Dialog.status == "1", Dialog.tenant_id == tenant_id) |
| | | |
| | | results = query.all() |
| | | formatted_results = [ |
| | | {"id": row[0], "name": row[1], "description": row[2], "status": "1" if row[3] == "1" else "2", |
| | | "user_id": str(row[4]), "mode": "agent-dialog"} for row in results] |
| | | "user_id": str(row[4]), "mode": "agent-dialog", "parameters": para, "kb_ids": row[5]} for row in results if row[0] not in chat_ids] |
| | | return formatted_results |
| | | finally: |
| | | db.close() |
| | | |
| | | |
| | | def get_data_from_dify_v2(names: List[str]) -> List[Dict]: |
| | | def get_data_from_dy_v2(base_db, names: List[str]) -> List[Dict]: |
| | | db = SessionDify() |
| | | try: |
| | | chat_ids = ComplexChatDao(base_db).get_complex_chat_ids() |
| | | # print(chat_ids) |
| | | if names: |
| | | query = db.query(DfApps.id, DfApps.name, DfApps.description, DfApps.status, DfApps.tenant_id, DfApps.mode) \ |
| | | .filter(DfApps.name.in_(names)) |
| | |
| | | results = query.all() |
| | | formatted_results = [ |
| | | {"id": str(row[0]), "name": row[1], "description": row[2], "status": "1", |
| | | "user_id": str(row[4]), "mode": row[5]} for row in results] |
| | | "user_id": str(row[4]), "mode": row[5], "parameters": {}} for row in results if str(row[0]) not in chat_ids] |
| | | return formatted_results |
| | | finally: |
| | | db.close() |
| | |
| | | if existing_agent: |
| | | existing_agent.name = row["name"] |
| | | existing_agent.description = row["description"] |
| | | # existing_agent.status = row["status"] |
| | | existing_agent.mode = row["mode"] |
| | | # existing_agent.tenant_id = get_rag_user_id(db, row["user_id"], type_dict[dialog_type]) |
| | | existing_agent.kb_ids = row.get("kb_ids", "") |
| | | if existing_agent.status == Dialog_STATSU_DELETE: |
| | | existing_agent.status = Dialog_STATSU_ON |
| | | if row["parameters"]: |
| | | existing_agent.parameters = json.dumps(row["parameters"]) |
| | | else: |
| | | existing = DialogModel(id=row["id"], status=row["status"], name=row["name"], |
| | | description=row["description"], |
| | | description=row["description"], kb_ids=row.get("kb_ids", ""), |
| | | tenant_id=get_rag_user_id(db, row["user_id"], type_dict[dialog_type]), |
| | | dialog_type=dialog_type, mode=row["mode"]) |
| | | dialog_type=dialog_type, mode=row["mode"], parameters=json.dumps(row["parameters"])) |
| | | db.add(existing) |
| | | db.commit() |
| | | for dialog in db.query(DialogModel).filter_by(dialog_type=dialog_type).all(): |
| | | if dialog.id not in agent_id_list: |
| | | # print(dialog.id) |
| | | db.query(DialogModel).filter_by(id=dialog.id).update({"status": "2"}) |
| | | db.query(DialogModel).filter_by(id=dialog.id).update({"status": Dialog_STATSU_DELETE}) |
| | | db.commit() |
| | | except IntegrityError: |
| | | db.rollback() |
| | |
| | | db.close() |
| | | |
| | | |
| | | def get_data_from_ragflow_knowledge(): |
| | | def get_data_from_ragflow_knowledge(tenant_id): |
| | | db = SessionRagflow() |
| | | try: |
| | | |
| | | results = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status, |
| | | RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).all() |
| | | RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).filter(RgKnowledge.tenant_id==tenant_id).all() |
| | | formatted_results = [ |
| | | {"id": row[0], "name": row[1], "description": row[2], "status": str(row[3]), |
| | | "user_id": str(row[4]), "doc_num": row[5], "permission": row[6]} for row in results] |
| | |
| | | try: |
| | | app_register = AppRegisterDao(db).get_apps() |
| | | for app in app_register: |
| | | if app["id"] == RAGFLOW: |
| | | ragflow_data = get_data_from_ragflow_v2([]) |
| | | if ragflow_data: |
| | | update_ids_in_local_v2(ragflow_data, "1") |
| | | elif app["id"] == BISHENG: |
| | | bisheng_data = get_data_from_bisheng_v2([]) |
| | | if bisheng_data: |
| | | update_ids_in_local_v2(bisheng_data, "2") |
| | | elif app["id"] == DIFY: |
| | | dify_data = get_data_from_dify_v2([]) |
| | | if dify_data: |
| | | update_ids_in_local_v2(dify_data, "4") |
| | | try: |
| | | if app["id"] == RAGFLOW: |
| | | ragflow_data = get_data_from_ragflow_v2(db, [], app["name"]) |
| | | if ragflow_data: |
| | | update_ids_in_local_v2(ragflow_data, "1") |
| | | elif app["id"] == DIFY: |
| | | dify_data = get_data_from_dy_v2(db, []) |
| | | if dify_data: |
| | | update_ids_in_local_v2(dify_data, "4") |
| | | except Exception as e: |
| | | print(f"Failed to sync agents: {str(e)}") |
| | | print("v2 Agents synchronized successfully") |
| | | except Exception as e: |
| | | print(f"v2 Failed to sync agents: {str(e)}") |
| | |
| | | try: |
| | | |
| | | row = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status, |
| | | RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).filter( |
| | | RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission, RgKnowledge.embd_id).filter( |
| | | RgKnowledge.id == klg_id).first() |
| | | return {"id": row[0], "name": row[1], "description": row[2], "status": str(row[3]), |
| | | "user_id": str(row[4]), "doc_num": row[5], "permission": row[6]} if row else {} |
| | | "user_id": str(row[4]), "doc_num": row[5], "permission": row[6], "embd_id": row[7]} if row else {} |
| | | finally: |
| | | db.close() |
| | | |
| | | |
| | | def get_one_from_ragflow_dialog(dialog_id): |
| | | db = SessionRagflow() |
| | | try: |
| | | row = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id, Dialog.kb_ids) \ |
| | | .filter(Dialog.id==dialog_id).first() |
| | | return {"id": row[0], "name": row[1], "description": row[2], "status": str(row[3]), |
| | | "user_id": str(row[4]), "kb_ids": row[5]} if row else {} |
| | | finally: |
| | | db.close() |
| | | |
| | |
| | | app_register = AppRegisterDao(db).get_apps() |
| | | for app in app_register: |
| | | if app["id"] == RAGFLOW: |
| | | ragflow_data = get_data_from_ragflow_knowledge() |
| | | ragflow_data = get_data_from_ragflow_knowledge(app["name"]) |
| | | if ragflow_data: |
| | | update_ids_in_local_knowledge(ragflow_data, "1") |
| | | # elif app["id"] == BISHENG: |
| | |
| | | db.commit() |
| | | finally: |
| | | db.close() |
| | | |
| | | |
| | | def import_type_table(session: Session, node: dict, parent=None): |
| | | resource_type = ResourceTypeModel( |
| | | id=node['id'], |
| | | name=node['name'], |
| | | description=node.get('description') |
| | | ) |
| | | if parent: |
| | | resource_type.parent = parent |
| | | session.add(resource_type) |
| | | session.commit() |
| | | |
| | | |
| | | def import_tree(session: Session, node: dict, parent=None): |
| | |
| | | def sync_resources_from_json(): |
| | | db = SessionLocal() |
| | | try: |
| | | if db.query(ResourceTypeModel).count() == 0: |
| | | with open(os.path.join(ENV_CONF_PATH, "resource_type.json"), 'r', encoding='utf-8') as file: |
| | | type_json_data = json.load(file) |
| | | |
| | | db.query(ResourceTypeModel).delete() |
| | | db.commit() |
| | | |
| | | for node in type_json_data: |
| | | import_type_table(db, node) |
| | | print("add resourceType record successfully") |
| | | else: |
| | | print("sync resourcesType successfully") |
| | | if db.query(ResourceModel).count() == 0: |
| | | with open(os.path.join(ENV_CONF_PATH, "resource.json"), 'r', encoding='utf-8') as file: |
| | | json_data = json.load(file) |
| | |
| | | else: |
| | | print("sync resources successfully") |
| | | except Exception as e: |
| | | print(f"Failed to sync resources: {str(e)}") |
| | | print(f"Failed to sync resources or resource type: {str(e)}") |
| | | finally: |
| | | db.close() |
| | | |