From e6c5e89f09637b8d9ebca6895d781663f12646d6 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 17 十二月 2024 16:43:55 +0800
Subject: [PATCH] dify 文档智能

---
 app/task/fetch_agent.py |   37 +++++++++++++++++++++++++++----------
 1 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/app/task/fetch_agent.py b/app/task/fetch_agent.py
index 5d08434..eec88ad 100644
--- a/app/task/fetch_agent.py
+++ b/app/task/fetch_agent.py
@@ -7,7 +7,8 @@
 
 from app.config.config import settings
 from app.config.const import RAGFLOW, BISHENG, DIFY
-from app.models import DialogModel
+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.service.v2.app_register import AppRegisterDao
@@ -130,9 +131,9 @@
             result = db.query(AgentModel).delete()
             db.commit()  # 鎻愪氦浜嬪姟
         initial_agents = [
-            ('80ee430a-e396-48c4-a12c-7c7cdf5eda51', 1, '鎶ュ憡鐢熸垚', 'BISHENG', 'report'),
+            # ('80ee430a-e396-48c4-a12c-7c7cdf5eda51', 1, '鎶ュ憡鐢熸垚', 'DIFY', 'report'),
             ('basic_excel_merge', 2, '鎶ヨ〃鍚堝苟', 'BASIC', 'excelMerge'),
-            ('bfd090d589d811efb3630242ac190006', 4, '鏂囨。鏅鸿兘', 'BISHENG', 'report'),
+            ('bfd090d589d811efb3630242ac190006', 4, '鏂囨。鏅鸿兘', 'DIFY', 'documentIa'),
             ('da3451da89d911efb9490242ac190006', 3, '鐭ヨ瘑闂瓟', 'RAGFLOW', 'knowledgeQA'),
             ('e96eb7a589db11ef87d20242ac190006', 5, '鏅鸿兘闂瓟', 'RAGFLOW', 'chat'),
             ('basic_excel_talk', 6, '鏅鸿兘鏁版嵁', 'BASIC', 'excelTalk'),
@@ -157,10 +158,10 @@
 
 def sync_agents():
     try:
-        bisheng_data = get_data_from_bisheng(BISHENG_NAMES_TO_SYNC)
+        # bisheng_data = get_data_from_bisheng(BISHENG_NAMES_TO_SYNC)
         ragflow_data = get_data_from_ragflow(RAGFLOW_NAMES_TO_SYNC)
 
-        update_ids_in_local(bisheng_data)
+        # update_ids_in_local(bisheng_data)
         update_ids_in_local(ragflow_data)
 
         print("Agents synchronized successfully")
@@ -184,6 +185,14 @@
         raise
     finally:
         db.close()
+
+def get_rag_user_id(db, tenant_id, app_type):
+
+    user = db.query(UserAppModel).filter(UserAppModel.app_type==app_type, UserAppModel.app_id==tenant_id).first()
+    if user:
+        return user.user_id
+    return tenant_id
+
 
 def get_data_from_bisheng_v2(names: List[str]) -> List[Dict]:
     db = SessionBisheng()
@@ -242,9 +251,7 @@
 def update_ids_in_local_v2(data: List[Dict], dialog_type:str):
     db = SessionLocal()
     agent_id_list = []
-    print("----------------------------------------")
-    print(data)
-    print("*********************************************")
+    type_dict = {"1": RAGFLOW,"2": BISHENG,"4": DIFY}
     try:
         for row in data:
             agent_id_list.append(row["id"])
@@ -252,8 +259,9 @@
             if existing_agent:
                 existing_agent.name = row["name"]
                 existing_agent.description = row["description"]
+                existing_agent.tenant_id = get_rag_user_id(db, row["user_id"], type_dict[dialog_type])
             else:
-                existing = DialogModel(id=row["id"], name=row["name"], description=row["description"], tenant_id=row["user_id"], dialog_type=dialog_type)
+                existing = DialogModel(id=row["id"], name=row["name"], description=row["description"], tenant_id=get_rag_user_id(db, row["user_id"], type_dict[dialog_type]), dialog_type=dialog_type)
                 db.add(existing)
         db.commit()
         for dialog in db.query(DialogModel).filter_by(dialog_type=dialog_type).all():
@@ -269,7 +277,16 @@
 
 
 def get_data_from_ragflow_knowledge():
-    ...
+    db = SessionRagflow()
+    try:
+
+        results = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id).all()
+        formatted_results = [
+            {"id": format_uuid(row[0]), "name": row[1], "description": row[2], "status": str(row[3]),
+             "user_id": str(row[4])} for row in results]
+        return formatted_results
+    finally:
+        db.close()
 
 def sync_agents_v2():
     db = SessionLocal()

--
Gitblit v1.8.0