zhaoqingang
2024-12-16 88360b4ac6f051f62a91e93d602fd393935071ab
app/api/chat.py
@@ -11,6 +11,7 @@
from app.api import get_current_user_websocket
from app.config.config import settings
from app.config.const import IMAGE_TO_TEXT, DOCUMENT_TO_REPORT, DOCUMENT_TO_CLEANING
from app.models import MenuCapacityModel
from app.models.agent_model import AgentModel, AgentType
from app.models.base_model import get_db
from app.models.user_model import UserModel
@@ -35,22 +36,25 @@
    tasks = []
    await websocket.accept()
    print(f"Client {agent_id} connected")
    agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
    agent = db.query(MenuCapacityModel).filter(MenuCapacityModel.chat_id == agent_id).first()
    if not agent:
        agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
        agent_type = agent.agent_type
        chat_type = agent.type
    else:
        agent_type = agent.capacity_type
        chat_type = agent.chat_type
    if not agent:
        ret = {"message": "Agent not found", "type": "close"}
        await websocket.send_json(ret)
        return
    print(1111)
    agent_type = agent.agent_type
    print(agent_type)
    if chat_id == "" or chat_id == "0":
        ret = {"message": "Chat ID not found", "type": "close"}
        await websocket.send_json(ret)
        return
    if agent_type == AgentType.RAGFLOW:
        print(222)
        ragflow_service = RagflowService(settings.fwr_base_url)
        token = await get_ragflow_token(db, current_user.id)
        try:
@@ -230,7 +234,7 @@
                    await websocket.send_json({"message": "Invalid request", "type": "error"})
                    continue
                logger.error(agent.type)
                if agent.type == "questionTalk":
                if chat_type == "questionTalk":
                    try:
                        data = await service.questions_talk(question, chat_id)
@@ -314,7 +318,7 @@
        # token = get_dify_token(db, current_user.id)
        try:
            async def forward_to_dify():
                if agent.type == "imageTalk":
                if chat_type == "imageTalk":
                    token = DfTokenDao(db).get_token_by_id(IMAGE_TO_TEXT)
                    if not token:
                        await websocket.send_json({"message": "Invalid token", "type": "error"})
@@ -412,7 +416,7 @@
                                result = {"message": f"内部错误: {e2}", "type": "close"}
                                await websocket.send_json(result)
                                print(f"Error process message of ragflow: {e2}")
                elif agent.type == "reportWorkflow":
                elif chat_type == "reportWorkflow":
                    token = DfTokenDao(db).get_token_by_id(DOCUMENT_TO_CLEANING)
                    if not token: