| | |
| | | |
| | | from app.config.config import settings |
| | | from app.config.const import RAGFLOW, BISHENG, DIFY, ENV_CONF_PATH, Dialog_STATSU_DELETE, Dialog_STATSU_ON |
| | | from app.models import KnowledgeModel |
| | | 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 |
| | |
| | | description = Column(Text) # 说明 |
| | | status = Column(String(1)) # 状态 |
| | | doc_num = Column(Integer) # 文档 |
| | | embd_id = Column(String(128)) # 文档 |
| | | |
| | | |
| | | class RgUserTenant(Base): |
| | |
| | | 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": [], |
| | |
| | | } |
| | | } |
| | | 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") |
| | | 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", "parameters": para} for row in results] |
| | | "user_id": str(row[4]), "mode": "agent-dialog", "parameters": para} for row in results if row[0] not in chat_ids] |
| | | return formatted_results |
| | | finally: |
| | | db.close() |
| | | |
| | | |
| | | def get_data_from_dy_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], "parameters": {}} 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() |
| | |
| | | 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_dy_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) \ |
| | | .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])} 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: |