From 88ae2fcd43de3138d2923f16bb59d2580b687579 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 30 十月 2024 01:56:21 +0800
Subject: [PATCH] 程序启动时初始化agent表并从ragflow和毕昇拉取智能体id然后更新agent表

---
 app/config/config.py    |    5 +
 app/task/fetch_agent.py |  140 ++++++++++++++++++++++++++++++++++++++++++++++
 main.py                 |   22 ++++++-
 app/config/config.yaml  |    6 +
 4 files changed, 169 insertions(+), 4 deletions(-)

diff --git a/app/config/config.py b/app/config/config.py
index 87c05a3..2d7bdf2 100644
--- a/app/config/config.py
+++ b/app/config/config.py
@@ -8,8 +8,13 @@
     sgb_websocket_url: str = ''
     fwr_base_url: str = ''
     database_url: str = ''
+    sgb_db_url: str = ''
+    fwr_db_url: str = ''
+    fetch_sgb_agent: str = ''
+    fetch_fwr_agent: str = ''
     PUBLIC_KEY: str
     PRIVATE_KEY: str
+
     def __init__(self, **kwargs):
         # Check if all required fields are provided and set them
         for field in self.__annotations__.keys():
diff --git a/app/config/config.yaml b/app/config/config.yaml
index 401a5b9..9477b79 100644
--- a/app/config/config.yaml
+++ b/app/config/config.yaml
@@ -3,8 +3,12 @@
 sgb_websocket_url: ws://192.168.20.119:13001
 fwr_base_url: http://192.168.20.119:11080
 database_url: mysql+pymysql://root:infini_rag_flow@192.168.20.116:5455/rag_basic
+sgb_db_url: mysql+pymysql://root:1234@192.168.20.119:13306/bisheng
+fwr_db_url: mysql+pymysql://root:infini_rag_flow@192.168.20.119:15455/rag_flow
 PUBLIC_KEY: |
   -----BEGIN PUBLIC KEY-----
   MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB
   -----END PUBLIC KEY-----
-PRIVATE_KEY: str
\ No newline at end of file
+PRIVATE_KEY: str
+fetch_sgb_agent: 鎶ュ憡鐢熸垚
+fetch_fwr_agent: 鐭ヨ瘑闂瓟,鏂囨。鏅鸿兘,鏅鸿兘闂瓟
\ No newline at end of file
diff --git a/app/task/fetch_agent.py b/app/task/fetch_agent.py
new file mode 100644
index 0000000..be0e07c
--- /dev/null
+++ b/app/task/fetch_agent.py
@@ -0,0 +1,140 @@
+from typing import Dict, List, Tuple
+
+from sqlalchemy import create_engine, Column, String, Integer
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.orm import sessionmaker
+
+from app.config.config import settings
+from app.models.agent_model import AgentModel
+from app.models.base_model import SessionLocal, Base
+
+# 鍒涘缓鏁版嵁搴撳紩鎿庡拰浼氳瘽宸ュ巶
+engine_bisheng = create_engine(settings.sgb_db_url)
+engine_ragflow = create_engine(settings.fwr_db_url)
+
+SessionBisheng = sessionmaker(autocommit=False, autoflush=False, bind=engine_bisheng)
+SessionRagflow = sessionmaker(autocommit=False, autoflush=False, bind=engine_ragflow)
+
+
+class Flow(Base):
+    __tablename__ = 'flow'
+    id = Column(String(255), primary_key=True)
+    name = Column(String(255), nullable=False)
+    status = Column(Integer, nullable=False)
+
+
+class Dialog(Base):
+    __tablename__ = 'dialog'
+    id = Column(String(255), primary_key=True)
+    name = Column(String(255), nullable=False)
+    status = Column(String(1), nullable=False)
+
+
+# 瑙f瀽鍚嶅瓧
+def parse_names(names_str: str) -> List[str]:
+    return [name.strip() for name in names_str.split(',')]
+
+
+BISHENG_NAMES_TO_SYNC = parse_names(settings.fetch_sgb_agent)
+RAGFLOW_NAMES_TO_SYNC = parse_names(settings.fetch_fwr_agent)
+
+
+def get_data_from_bisheng(names: List[str]) -> List[Tuple]:
+    db = SessionBisheng()
+    try:
+        if names:
+            query = db.query(Flow.id, Flow.name) \
+                .filter(Flow.status == 2, Flow.name.in_(names))
+        else:
+            query = db.query(Flow.id, Flow.name) \
+                .filter(Flow.status == 2)
+
+        results = query.all()
+        print(f"Executing query: {query}")
+        # 鏍煎紡鍖杋d涓篣UID
+        formatted_results = [(format_uuid(row[0]), row[1]) for row in results]
+        return formatted_results
+    finally:
+        db.close()
+
+
+def format_uuid(uuid_str: str) -> str:
+    # 纭繚杈撳叆瀛楃涓查暱搴︿负32
+    if len(uuid_str) != 32:
+        raise ValueError("Input string must be 32 characters long")
+
+    # 鎻掑叆杩炲瓧绗�
+    formatted_uuid = f"{uuid_str[:8]}-{uuid_str[8:12]}-{uuid_str[12:16]}-{uuid_str[16:20]}-{uuid_str[20:]}"
+    return formatted_uuid
+
+
+def get_data_from_ragflow(names: List[str]) -> List[Tuple]:
+    db = SessionRagflow()
+    try:
+        if names:
+            query = db.query(Dialog.id, Dialog.name) \
+                .filter(Dialog.status == 1, Dialog.name.in_(names))
+        else:
+            query = db.query(Dialog.id, Dialog.name) \
+                .filter(Dialog.status == 1)
+
+        results = query.all()
+        print(f"Executing query: {query}")
+        return results
+    finally:
+        db.close()
+
+
+def update_ids_in_local(data: List[Tuple]):
+    db = SessionLocal()
+    try:
+        for row in data:
+            name = row[1]
+            new_id = row[0]
+            existing_agent = db.query(AgentModel).filter_by(name=name).first()
+            if existing_agent:
+                existing_agent.id = new_id
+                db.add(existing_agent)
+        db.commit()
+    except IntegrityError:
+        db.rollback()
+        raise
+    finally:
+        db.close()
+
+
+def initialize_agents():
+    db = SessionLocal()
+    try:
+        initial_agents = [
+            ('80ee430a-e396-48c4-a12c-7c7cdf5eda51', 1, '鎶ュ憡鐢熸垚', 'BISHENG', 'report'),
+            ('basic_excel_merge', 2, '鎶ヨ〃鍚堝苟', 'BASIC', 'excelMerge'),
+            ('bfd090d589d811efb3630242ac190006', 4, '鏂囨。鏅鸿兘', 'RAGFLOW', 'documentChat'),
+            ('da3451da89d911efb9490242ac190006', 3, '鐭ヨ瘑闂瓟', 'RAGFLOW', 'knowledgeQA'),
+            ('e96eb7a589db11ef87d20242ac190006', 5, '鏅鸿兘闂瓟', 'RAGFLOW', 'chat')
+        ]
+
+        for agent in initial_agents:
+            agent_id = format_uuid(agent[0]) if len(agent[0]) == 32 else agent[0]
+            db.add(AgentModel(id=agent_id, sort=agent[1], name=agent[2], agent_type=agent[3], type=agent[4]))
+
+        db.commit()
+        print("Initial agents inserted successfully")
+    except IntegrityError:
+        db.rollback()
+        raise
+    finally:
+        db.close()
+
+
+def sync_agents():
+    try:
+        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(ragflow_data)
+
+        print("Agents synchronized successfully")
+    except Exception as e:
+        print(f"Failed to sync agents: {str(e)}")
diff --git a/main.py b/main.py
index 2470d4b..e629cbf 100644
--- a/main.py
+++ b/main.py
@@ -1,3 +1,5 @@
+from contextlib import asynccontextmanager
+
 from fastapi import FastAPI
 from app.api.auth import router as auth_router
 from app.api.chat import router as chat_router
@@ -6,12 +8,26 @@
 from app.api.files import router as files_router
 from app.api.report import router as report_router
 from app.models.base_model import init_db
+from app.task.fetch_agent import sync_agents, initialize_agents
+
+
+# 浣跨敤 Lifespan 浜嬩欢澶勭悊绋嬪簭
+@asynccontextmanager
+async def lifespan(app: FastAPI):
+    # 鍒濆鍖栦唬鐞�
+    initialize_agents()
+    # 鍦ㄥ簲鐢ㄥ惎鍔ㄦ椂鍚屾浠g悊
+    sync_agents()
+    yield
+    # 鍦ㄥ簲鐢ㄥ叧闂椂鎵ц娓呯悊鎿嶄綔锛堝鏋滈渶瑕侊級
+    pass
 
 init_db()
 app = FastAPI(
-  title="basic_rag_gateway",
-  version="0.1",
-  description="",
+    title="basic_rag_gateway",
+    version="0.1",
+    description="",
+    lifespan=lifespan
 )
 
 app.include_router(auth_router, prefix='/api/auth', tags=["auth"])

--
Gitblit v1.8.0