| | |
| | | complex_knowledge_chat_deep, complex_knowledge_chat |
| | | from app.models import UserModel |
| | | from app.models.base_model import get_db |
| | | from app.models.v2.chat import RetrievalRequest, ChatDataRequest, ComplexChatDao |
| | | from app.models.v2.chat import RetrievalRequest, ChatDataRequest, ComplexChatDao, SetModelRequest |
| | | from app.models.v2.session_model import ChatData |
| | | from app.service.v2.chat import service_chat_dialog, get_chat_info, service_chat_basic, \ |
| | | service_chat_workflow, service_chat_parameters, service_chat_sessions, service_chat_upload, \ |
| | | service_chat_sessions_list, service_chat_session_log, service_chunk_retrieval, service_complex_chat, \ |
| | | service_complex_upload |
| | | service_complex_upload, service_complex_model, service_get_complex_model |
| | | |
| | | chat_router_v2 = APIRouter() |
| | | |
| | |
| | | return Response(data, media_type="application/json", status_code=status_code) |
| | | |
| | | |
| | | @chat_router_v2.put("/complex/model") |
| | | async def api_complex_model(chatModel:SetModelRequest, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | status_code = http_200 |
| | | data = await service_complex_model(db, chatModel.chatType, chatModel.modelType, chatModel.modelName, chatModel.modelProvider) |
| | | if data: |
| | | status_code = http_500 |
| | | return Response(data, media_type="application/json", status_code=status_code) |
| | | |
| | | |
| | | @chat_router_v2.get("/complex/model") |
| | | async def api_get_complex_model(current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | status_code = http_200 |
| | | data = await service_get_complex_model(db) |
| | | if data: |
| | | status_code = http_500 |
| | | return Response(data, media_type="application/json", status_code=status_code) |
| | | |
| | | |
| | | |
| | |
| | | RG_USER_LOGIN= "/v1/user/login" |
| | | RG_PING= "/v1/system/version" |
| | | RG_ORIGINAL_URL = "/api/v1/retrieval" |
| | | RG_CHAT_UPDATE_URL = "/api/v1/chats/{}" |
| | | |
| | | ### ---------- |
| | | DF_CHAT_AGENT= "/v1/chat-messages" |
| | |
| | | DF_CHAT_API_KEY= "/console/api/apps/{}/api-keys" |
| | | DF_USER_LOGIN= "/console/api/login" |
| | | DF_PING = "/console/api/workspaces" |
| | | DF_UPLOAD_FILE = "/v1/files/upload" |
| | | DF_UPLOAD_FILE = "/v1/files/upload" |
| | | DF_WORKFLOW_DRAFT = "/console/api/apps/{}/workflows/draft" |
| | | DF_WORKFLOW_PUBLISH = "/console/api/apps/{}/workflows/publish" |
| | |
| | | "enabled": false |
| | | }, |
| | | "user_input_form": [ |
| | | { |
| | | "file-list": { |
| | | "allowed_file_extensions": [ |
| | | "xlsx" |
| | | ], |
| | | "allowed_file_types": [ |
| | | "document" |
| | | ], |
| | | "allowed_file_upload_methods": [ |
| | | "local_file" |
| | | ], |
| | | "label": "文档解析站", |
| | | "max_length": 30, |
| | | "options": [], |
| | | "required": true, |
| | | "type": "file-list", |
| | | "variable": "file_list" |
| | | { |
| | | "file-list": { |
| | | "allowed_file_extensions": [ |
| | | "xlsx" |
| | | ], |
| | | "allowed_file_types": [ |
| | | "document" |
| | | ], |
| | | "allowed_file_upload_methods": [ |
| | | "local_file" |
| | | ], |
| | | "label": "文档解析站", |
| | | "max_length": 30, |
| | | "options": [], |
| | | "required": true, |
| | | "type": "file-list", |
| | | "variable": "file_list" |
| | | } |
| | | } |
| | | } |
| | | ], |
| | | "file_upload":{ |
| | | "file_upload": { |
| | | "enabled": false |
| | | } |
| | | }, |
| | |
| | | }, |
| | | "user_input_form": [ |
| | | { |
| | | "file-list": { |
| | | "allowed_file_extensions": [ |
| | | "xlsx", "xls","csv","db" |
| | | ], |
| | | "allowed_file_types": [ |
| | | "document" |
| | | ], |
| | | "allowed_file_upload_methods": [ |
| | | "local_file" |
| | | ], |
| | | "label": "上传文档", |
| | | "max_length": 30, |
| | | "options": [], |
| | | "required": true, |
| | | "type": "file-list", |
| | | "variable": "file_list", |
| | | "fileUploadConfig": { |
| | | "file_size_limit": 30, |
| | | "batch_count_limit": 5, |
| | | "image_file_size_limit": 10, |
| | | "video_file_size_limit": 100, |
| | | "audio_file_size_limit": 50, |
| | | "workflow_file_upload_limit": 100 |
| | | "file-list": { |
| | | "allowed_file_extensions": [ |
| | | "xlsx", |
| | | "xls", |
| | | "csv", |
| | | "db" |
| | | ], |
| | | "allowed_file_types": [ |
| | | "document" |
| | | ], |
| | | "allowed_file_upload_methods": [ |
| | | "local_file" |
| | | ], |
| | | "label": "上传文档", |
| | | "max_length": 30, |
| | | "options": [], |
| | | "required": true, |
| | | "type": "file-list", |
| | | "variable": "file_list", |
| | | "fileUploadConfig": { |
| | | "file_size_limit": 30, |
| | | "batch_count_limit": 5, |
| | | "image_file_size_limit": 10, |
| | | "video_file_size_limit": 100, |
| | | "audio_file_size_limit": 50, |
| | | "workflow_file_upload_limit": 100 |
| | | } |
| | | } |
| | | } |
| | | ], |
| | | "file_upload":{ |
| | | "file_upload": { |
| | | "enabled": false |
| | | } |
| | | }, |
| | |
| | | "enabled": true |
| | | }, |
| | | "user_input_form": [], |
| | | "file_upload":{ |
| | | "file_upload": { |
| | | "enabled": false |
| | | } |
| | | }, |
| | |
| | | "enabled": true |
| | | }, |
| | | "user_input_form": [], |
| | | "file_upload":{ |
| | | "file_upload": { |
| | | "enabled": false |
| | | } |
| | | }, |
| | |
| | | "enabled": true |
| | | }, |
| | | "user_input_form": [], |
| | | "file_upload":{ |
| | | "file_upload": { |
| | | "enabled": false |
| | | } |
| | | }, |
| | |
| | | ], |
| | | "complex": [ |
| | | { |
| | | "id": "9992ba72fff111ef8d890242ac120004", |
| | | "id": "b436d1ae135311f0add70242ac1a0006", |
| | | "name": "知识库专家", |
| | | "description": "知识库专家", |
| | | "icon": "intellFrame4", |
| | |
| | | "parameters": { |
| | | }, |
| | | "dialogType": "1", |
| | | "mode": "complex-chat" |
| | | "mode": "complex-chat", |
| | | "chat_model": "qwen-32b-awq___OpenAI-API", |
| | | "chat_provider": "OpenAI-API-Compatible" |
| | | }, |
| | | { |
| | | "id": "ad127cb5-a1ef-49a3-82ed-6467a670fd10", |
| | |
| | | "parameters": { |
| | | }, |
| | | "dialogType": "4", |
| | | "mode": "complex-chat" |
| | | "mode": "complex-chat", |
| | | "chat_model": "qwen-32b-awq", |
| | | "chat_model_ds": "qwq-32b-awq", |
| | | "chat_provider": "openai_api_compatible" |
| | | }, |
| | | { |
| | | "id": "c6a9fda3-bfb8-40ef-9510-545766c7d6a0", |
| | |
| | | "mode": "complex-chat" |
| | | }, |
| | | { |
| | | "id": "8f3e8d9c0bab11f0896e0242ac120006", |
| | | "id": "c964acd6135311f0bf5b0242ac1a0006", |
| | | "name": "知识库专家-深度", |
| | | "description": "知识库专家", |
| | | "icon": "intellFrame4", |
| | |
| | | "parameters": { |
| | | }, |
| | | "dialogType": "1", |
| | | "mode": "complex-chat" |
| | | "mode": "complex-chat", |
| | | "chat_model_ds": "qwq-32b-awq___OpenAI-API", |
| | | "chat_provider": "OpenAI-API-Compatible" |
| | | } |
| | | ] |
| | | } |
| | |
| | | smart_system: |
| | | title: SmartAI大模型平台 |
| | | desc: SmartAI大模型平台 |
| | | version: 1.0.3 |
| | | version: 1.0.4 |
| | |
| | | await sync_token() # 账号token登录 |
| | | await sync_complex_api_token(db) # 账号token登录 |
| | | await system_license_sync(db) # 同步系统license |
| | | |
| | | except Exception as e: |
| | | print(e) |
| | | finally: |
| | |
| | | # yield session |
| | | try: |
| | | yield session |
| | | finally: |
| | | session.close() |
| | | except Exception as e: |
| | | ... |
| | | # session.close() |
| | |
| | | |
| | | |
| | | |
| | | class SetModelRequest(BaseModel): |
| | | chatType: int |
| | | modelType: int |
| | | modelName: str |
| | | modelProvider: str |
| | | |
| | | |
| | | |
| | | class ComplexChatModel(Base): |
| | | __tablename__ = 'complex_chat' |
| | |
| | | mode = Column(String(36)) |
| | | parameters = Column(Text) |
| | | chat_mode = Column(Integer) #1= 普通对话,2=联网,3=知识库,4=深度 |
| | | chat_model = Column(String(255)) # 模型 |
| | | chat_model_ds = Column(String(255)) # 模型 |
| | | chat_provider = Column(String(255)) # 模型提供商 |
| | | |
| | | def to_json(self): |
| | | return { |
| | |
| | | session = self.db.query(ComplexChatModel).filter_by(id=chat_id).first() |
| | | return session |
| | | |
| | | async def update_complex_chat_by_id(self, chat_id: str, session, message: dict, conversation_id=None) -> ComplexChatModel | None: |
| | | if not session: |
| | | session = await self.get_complex_chat_by_id(chat_id) |
| | | if session: |
| | | try: |
| | | # TODO |
| | | session.update_date = current_time() |
| | | self.db.commit() |
| | | self.db.refresh(session) |
| | | except Exception as e: |
| | | # logger.error(e) |
| | | self.db.rollback() |
| | | return session |
| | | async def update_complex_chat_by_id(self, chat_id: str, kwargs:dict) -> None: |
| | | |
| | | try: |
| | | self.db.query(ComplexChatModel).filter_by(id=chat_id).update(kwargs) |
| | | self.db.commit() |
| | | except Exception as e: |
| | | # logger.error(e) |
| | | self.db.rollback() |
| | | |
| | | |
| | | async def update_or_insert_by_id(self, chat_id: str, **kwargs) -> ComplexChatModel: |
| | | existing_session = await self.get_complex_chat_by_id(chat_id) |
| | | if existing_session: |
| | | return await self.update_complex_chat_by_id(chat_id, existing_session, kwargs.get("message")) |
| | | return await self.update_complex_chat_by_id(chat_id, kwargs) |
| | | |
| | | existing_session = await self.create_complex_chat(chat_id, **kwargs) |
| | | return existing_session |
| | |
| | | return session |
| | | |
| | | |
| | | async def aget_complex_chat(self) -> List: |
| | | return self.db.query(ComplexChatModel).filter(ComplexChatModel.status!=Dialog_STATSU_DELETE).all() |
| | | |
| | | |
| | | |
| | | class ComplexChatSessionModel(Base): |
| | | __tablename__ = "complex_chat_sessions" |
| | |
| | | return response |
| | | |
| | | @staticmethod |
| | | async def http_put(url, data, headers, timeout=300): |
| | | async with httpx.AsyncClient(timeout=timeout) as client: |
| | | response = await client.put(url, json=data, headers=headers) |
| | | return response |
| | | |
| | | @staticmethod |
| | | async def http_upload_file(url, data, files, headers, timeout=300): |
| | | async with httpx.AsyncClient(timeout=timeout) as client: |
| | | response = await client.post(url, headers=headers, files=files, data=data) |
| | |
| | | import json |
| | | |
| | | from Log import logger |
| | | from app.config.config import settings |
| | | # from Log import logger |
| | | from app.service.v2.app_driver.chat_base import ChatBase |
| | |
| | | else: |
| | | return {} |
| | | |
| | | async def chat_put(self, url, data, headers): |
| | | |
| | | res = await self.http_put(url, data, headers) |
| | | if res.status_code == 200 or res.status_code == 201: |
| | | return res.json() |
| | | else: |
| | | logger.error(res.text) |
| | | return {} |
| | | |
| | | async def chat_ping(self, url, params, headers): |
| | | res = await self.http_get(url, params, headers) |
| | | # print(res.text) |
| | |
| | | import asyncio |
| | | import datetime |
| | | import io |
| | | import json |
| | | import time |
| | |
| | | |
| | | from Log import logger |
| | | from app.config.agent_base_url import RG_CHAT_DIALOG, DF_CHAT_AGENT, DF_CHAT_PARAMETERS, RG_CHAT_SESSIONS, \ |
| | | DF_CHAT_WORKFLOW, DF_UPLOAD_FILE, RG_ORIGINAL_URL |
| | | DF_CHAT_WORKFLOW, DF_UPLOAD_FILE, RG_ORIGINAL_URL, RG_CHAT_UPDATE_URL, DF_WORKFLOW_DRAFT, DF_WORKFLOW_PUBLISH |
| | | from app.config.config import settings |
| | | from app.config.const import * |
| | | from app.models import DialogModel, ApiTokenModel, UserTokenModel, ComplexChatSessionDao, ChatDataRequest, \ |
| | |
| | | |
| | | |
| | | async def add_session_log(db, session_id: str, question: str, chat_id: str, user_id, event_type: str, |
| | | conversation_id: str, agent_type, query: dict=None): |
| | | conversation_id: str, agent_type, query: dict = None): |
| | | try: |
| | | session = await ChatSessionDao(db).update_or_insert_by_id( |
| | | session_id=session_id, |
| | |
| | | return app_token.token |
| | | return "" |
| | | |
| | | async def get_workflow_token(db): |
| | | user_token = db.query(UserTokenModel).filter(UserTokenModel.id == workflow_server).first() |
| | | return user_token.access_token if user_token else "" |
| | | |
| | | |
| | | async def add_chat_token(db, data): |
| | | try: |
| | |
| | | return ChatAgent(), url |
| | | |
| | | |
| | | |
| | | async def get_user_kb(db, user_id: int, kb_ids: list) -> list: |
| | | res = [] |
| | | user = db.query(UserModel).filter(UserModel.id == user_id).first() |
| | |
| | | query = db.query(KnowledgeModel) |
| | | if user.permission != "admin": |
| | | klg_list = [j.id for i in user.groups for j in i.knowledges] |
| | | for i in db.query(KnowledgeUserModel).filter(KnowledgeUserModel.user_id == user_id, KnowledgeUserModel.status == 1).all(): |
| | | for i in db.query(KnowledgeUserModel).filter(KnowledgeUserModel.user_id == user_id, |
| | | KnowledgeUserModel.status == 1).all(): |
| | | if i.kb_id not in klg_list: |
| | | klg_list.append(i.kb_id) |
| | | query = query.filter(or_(KnowledgeModel.id.in_(klg_list), KnowledgeModel.tenant_id == str(user_id))) |
| | | kb_list= query.all() |
| | | kb_list = query.all() |
| | | for kb in kb_list: |
| | | if kb.id in kb_ids: |
| | | if kb.permission == "team": |
| | |
| | | kb_id = await get_user_kb(db, user_id, kb_ids) |
| | | if not kb_id: |
| | | yield "data: " + json.dumps({"message": smart_message_error, |
| | | "error": "\n**ERROR**: The agent has no knowledge base to work with!", "status": http_400}, |
| | | "error": "\n**ERROR**: The agent has no knowledge base to work with!", |
| | | "status": http_400}, |
| | | ensure_ascii=False) + "\n\n" |
| | | return |
| | | chat = ChatDialog() |
| | |
| | | "error": error}, conversation_id) |
| | | |
| | | |
| | | |
| | | |
| | | async def service_chat_basic(db, chat_id: str, chat_data: ChatData, session_id: str, user_id, mode: str): |
| | | |
| | | if chat_id == basic_report_talk: |
| | | complex_chat = await ComplexChatDao(db).get_complex_chat_by_mode(chat_data.report_mode) |
| | | if complex_chat: |
| | | ... |
| | | |
| | | |
| | | |
| | | async def service_chat_parameters(db, chat_id, user_id): |
| | |
| | | session_log = await ChatSessionDao(db).get_session_by_id(session_id) |
| | | if not session_log: |
| | | return {} |
| | | log_info =session_log.log_to_json() |
| | | if session_log.event_type == complex_chat: |
| | | |
| | | log_info = session_log.log_to_json() |
| | | if session_log.event_type == complex_chat: |
| | | total, message_list = await ComplexChatSessionDao(db).get_session_list(session_id) |
| | | log_info["message"] = [message.log_to_json() for message in message_list[::-1]] |
| | | |
| | |
| | | return records |
| | | |
| | | |
| | | async def add_complex_log(db, message_id, chat_id, session_id, chat_mode, query, user_id, mode, agent_type, message_type, conversation_id="", node_data=None, query_data=None): |
| | | async def add_complex_log(db, message_id, chat_id, session_id, chat_mode, query, user_id, mode, agent_type, |
| | | message_type, conversation_id="", node_data=None, query_data=None): |
| | | if not node_data: |
| | | node_data = [] |
| | | if not query_data: |
| | |
| | | if session: |
| | | conversation_id = session.conversation_id |
| | | await complex_log.create_session(message_id, |
| | | chat_id=chat_id, |
| | | session_id=session_id, |
| | | chat_mode=chat_mode, |
| | | message_type=message_type, |
| | | content=query, |
| | | event_type=mode, |
| | | tenant_id=user_id, |
| | | conversation_id=conversation_id, |
| | | node_data=json.dumps(node_data), |
| | | query=json.dumps(query_data), |
| | | agent_type=agent_type) |
| | | chat_id=chat_id, |
| | | session_id=session_id, |
| | | chat_mode=chat_mode, |
| | | message_type=message_type, |
| | | content=query, |
| | | event_type=mode, |
| | | tenant_id=user_id, |
| | | conversation_id=conversation_id, |
| | | node_data=json.dumps(node_data), |
| | | query=json.dumps(query_data), |
| | | agent_type=agent_type) |
| | | return conversation_id, True |
| | | |
| | | except Exception as e: |
| | | logger.error(e) |
| | | return conversation_id, False |
| | | |
| | | |
| | | async def add_query_files(db, message_id): |
| | | query = {} |
| | |
| | | if complex_log: |
| | | query = json.loads(complex_log.query) |
| | | return query.get("files", []) |
| | | |
| | | |
| | | async def service_complex_chat(db, chat_id, mode, user_id, chat_request: ChatDataRequest): |
| | | answer_event = "" |
| | |
| | | task_id = "" |
| | | error = "" |
| | | node_list = [] |
| | | reference= {} |
| | | reference = {} |
| | | conversation_id = "" |
| | | query_data = chat_request.to_dict() |
| | | new_message_id = str(uuid.uuid4()) |
| | |
| | | if not files and chat_request.parentId: |
| | | files = await add_query_files(db, chat_request.parentId) |
| | | if chat_request.chatMode != complex_content_optimization_chat: |
| | | await add_session_log(db, chat_request.sessionId, chat_request.query if chat_request.query else "未命名会话", chat_id, user_id, |
| | | mode, "", DF_TYPE) |
| | | conversation_id, message = await add_complex_log(db, new_message_id, chat_id, chat_request.sessionId, chat_request.chatMode, chat_request.query, user_id, mode, DF_TYPE, 1, query_data=query_data) |
| | | await add_session_log(db, chat_request.sessionId, chat_request.query if chat_request.query else "未命名会话", |
| | | chat_id, user_id, |
| | | mode, "", DF_TYPE) |
| | | conversation_id, message = await add_complex_log(db, new_message_id, chat_id, chat_request.sessionId, |
| | | chat_request.chatMode, chat_request.query, user_id, mode, |
| | | DF_TYPE, 1, query_data=query_data) |
| | | if not message: |
| | | yield "data: " + json.dumps({"message": smart_message_error, |
| | | "error": "\n**ERROR**: 创建会话失败!", "status": http_500}, |
| | |
| | | session = await service_chat_sessions(db, chat_id, chat_request.query) |
| | | # print(session) |
| | | if not session or session.get("code") != 0: |
| | | yield "data: " + json.dumps( |
| | | yield "data: " + json.dumps( |
| | | {"message": smart_message_error, "error": "\n**ERROR**: chat agent error", "status": http_500}) |
| | | return |
| | | conversation_id = session.get("data", {}).get("id") |
| | |
| | | url = settings.fwr_base_url + RG_CHAT_DIALOG.format(chat_id) |
| | | chat = ChatDialog() |
| | | try: |
| | | async for ans in chat.chat_completions(url, await chat.complex_request_data(chat_request.query, chat_request.knowledgeId, conversation_id), |
| | | async for ans in chat.chat_completions(url, await chat.complex_request_data(chat_request.query, |
| | | chat_request.knowledgeId, |
| | | conversation_id), |
| | | await chat.get_headers(token)): |
| | | data = {} |
| | | error = "" |
| | |
| | | reference = data.get("reference", {}) |
| | | event = smart_message_cover |
| | | message_str = "data: " + json.dumps( |
| | | {"event": event, "data": data, "error": error, "status": status, "message_id":message_id, |
| | | "parent_id": new_message_id, |
| | | "session_id": chat_request.sessionId}, |
| | | {"event": event, "data": data, "error": error, "status": status, "message_id": message_id, |
| | | "parent_id": new_message_id, |
| | | "session_id": chat_request.sessionId}, |
| | | ensure_ascii=False) + "\n\n" |
| | | for i in range(0, len(message_str), max_chunk_size): |
| | | chunk = message_str[i:i + max_chunk_size] |
| | |
| | | token = await get_chat_token(db, chat_id) |
| | | chat, url = await get_chat_object(mode) |
| | | async for ans in chat.chat_completions(url, |
| | | await chat.complex_request_data(chat_request.query, conversation_id, str(user_id), files=files, inputs=inputs), |
| | | await chat.complex_request_data(chat_request.query, conversation_id, |
| | | str(user_id), files=files, |
| | | inputs=inputs), |
| | | await chat.get_headers(token)): |
| | | # print(ans) |
| | | data = {} |
| | |
| | | continue |
| | | |
| | | yield "data: " + json.dumps( |
| | | {"event": event, "data": data, "error": error, "status": status, "task_id": task_id, "message_id":message_id, |
| | | {"event": event, "data": data, "error": error, "status": status, "task_id": task_id, |
| | | "message_id": message_id, |
| | | "parent_id": new_message_id, |
| | | "session_id": chat_request.sessionId}, |
| | | ensure_ascii=False) + "\n\n" |
| | |
| | | # "node_list": node_list, "task_id": task_id, "id": message_id, |
| | | # "error": error}, conversation_id) |
| | | if message_id: |
| | | await add_complex_log(db, message_id, chat_id, chat_request.sessionId, chat_request.chatMode, answer_event or answer_agent or answer_workflow or answer_dialog or error, user_id, mode, DF_TYPE, 2, conversation_id, node_data=node_list or reference, query_data=query_data) |
| | | await add_complex_log(db, message_id, chat_id, chat_request.sessionId, chat_request.chatMode, |
| | | answer_event or answer_agent or answer_workflow or answer_dialog or error, user_id, |
| | | mode, DF_TYPE, 2, conversation_id, node_data=node_list or reference, |
| | | query_data=query_data) |
| | | |
| | | |
| | | async def service_complex_upload(db, chat_id, file, user_id): |
| | | files = [] |
| | |
| | | logger.error(e) |
| | | return json.dumps(files) if files else "" |
| | | |
| | | |
| | | async def service_complex_model(db, chat_type, model_type, model_name, model_provider): |
| | | if chat_type == 1 and model_type == 1: |
| | | return await set_dialog_model(db, complex_knowledge_chat, model_name) |
| | | elif chat_type == 1 and model_type == 2: |
| | | return await set_dialog_model(db, complex_knowledge_chat_deep, model_name, model_provider) |
| | | else: |
| | | if model_type == 1: |
| | | chats = [complex_dialog_chat, complex_network_chat, complex_mindmap_chat, complex_content_optimization_chat] |
| | | else: |
| | | chats = [complex_dialog_chat, complex_network_chat] |
| | | return await set_workflow_model(db,chats |
| | | , # , complex_network_chat, complex_mindmap_chat, complex_content_optimization_chat |
| | | model_type, model_name, model_provider) |
| | | |
| | | |
| | | async def set_dialog_model(db, chat_mode, model_name, model_provider): |
| | | chat = await ComplexChatDao(db).get_complex_chat_by_mode(chat_mode) |
| | | if chat: |
| | | access_token = await get_chat_token(db, rg_api_token) |
| | | url = settings.fwr_base_url + RG_CHAT_UPDATE_URL.format(chat.id) |
| | | chat_base = ChatBaseApply() |
| | | payload = { |
| | | "name": chat.name, |
| | | "llm": { |
| | | "model_name": model_name |
| | | } |
| | | } |
| | | response = await chat_base.chat_put(url, payload, await chat_base.get_headers(access_token)) |
| | | # print(response) |
| | | if not response: |
| | | return "服务异常,修改失败!" |
| | | await ComplexChatDao(db).update_complex_chat_by_id(chat.id, {"chat_model": model_name, "chat_model_ds": model_name, "chat_provider": model_provider, "update_date": datetime.datetime.now()}) |
| | | return "" |
| | | |
| | | async def set_workflow_model(db, chat_modes, model_type, model_name, model_provider): |
| | | chat_base = ChatBaseApply() |
| | | token = await get_workflow_token(db) |
| | | for chat_mode in chat_modes: |
| | | chat = await ComplexChatDao(db).get_complex_chat_by_mode(chat_mode) |
| | | if chat: |
| | | |
| | | get_draft_url = settings.dify_base_url + DF_WORKFLOW_DRAFT.format(chat.id) |
| | | draft_data = await chat_base.chat_get(get_draft_url, {}, await chat_base.get_headers(token)) |
| | | if draft_data: |
| | | graph = draft_data.get("graph") |
| | | for node in graph.get("nodes"): |
| | | if node.get("data", {}).get("type") == "llm": |
| | | if model_type == 1 and "深度搜索" not in node.get("data", {}).get("title"): |
| | | node["data"]["model"]["name"] = model_name |
| | | node["data"]["model"]["provider"] = model_provider |
| | | elif model_type == 2 and "深度搜索" in node.get("data", {}).get("title"): |
| | | node["data"]["model"]["name"] = model_name |
| | | node["data"]["model"]["provider"] = model_provider |
| | | |
| | | draft_data_query = {"conversation_variables": draft_data.get("conversation_variables"), |
| | | "environment_variables": draft_data.get("environment_variables"), |
| | | "hash": draft_data.get("hash"), |
| | | "features": draft_data.get("features"), |
| | | "graph": graph} |
| | | set_draft_data = await chat_base.chat_post(get_draft_url, draft_data_query, await chat_base.get_headers(token)) |
| | | if set_draft_data and set_draft_data.get("result") == "success": |
| | | publish_url = settings.dify_base_url + DF_WORKFLOW_PUBLISH.format(chat.id) |
| | | publish_data = await chat_base.chat_post(publish_url, {}, await chat_base.get_headers(token)) |
| | | if publish_data and publish_data.get("result") == "success": |
| | | update_kwargs = {"chat_provider": model_provider, "update_date": datetime.datetime.now()} |
| | | if model_type == 1: |
| | | update_kwargs["chat_model"] = model_name |
| | | else: |
| | | update_kwargs["chat_model_ds"] = model_name |
| | | await ComplexChatDao(db).update_complex_chat_by_id(chat.id, update_kwargs) |
| | | |
| | | |
| | | async def service_get_complex_model(db): |
| | | res = {} |
| | | for complexs in await ComplexChatDao(db).aget_complex_chat(): |
| | | if complexs.chat_mode == complex_knowledge_chat: |
| | | res["dialog"] = {"modelName": complexs.chat_model, "modelProvider": complexs.chat_provider} |
| | | elif complexs.chat_mode == complex_knowledge_chat_deep: |
| | | res["dialog_ds"] = {"modelName": complexs.chat_model_ds, "modelProvider": complexs.chat_provider} |
| | | else: |
| | | res["workflow"] = {"modelName": complexs.chat_model, "modelProvider": complexs.chat_provider} |
| | | res["workflow_ds"] = {"modelName": complexs.chat_model_ds, "modelProvider": complexs.chat_provider} |
| | | |
| | | return json.dumps(res) |
| | | |
| | | if __name__ == "__main__": |
| | | q = json.dumps({"query": "设备", "dataset_ids": ["fc68db52f43111efb94a0242ac120004"]}) |
| | | top_k = 2 |
| | |
| | | else: |
| | | try: |
| | | dialog = ComplexChatModel(id=agent["id"], name=agent["name"], description=agent["description"], |
| | | icon=agent["icon"], tenant_id=user.id if user else "", dialog_type=agent["dialogType"], mode=agent["mode"],chat_mode = agent["chat_mode"]) |
| | | icon=agent["icon"], tenant_id=user.id if user else "", dialog_type=agent["dialogType"], mode=agent["mode"],chat_mode = agent["chat_mode"],chat_model = agent.get("chat_model"),chat_model_ds = agent.get("chat_model_ds"),chat_provider = agent.get("chat_provider")) |
| | | db.add(dialog) |
| | | db.commit() |
| | | db.refresh(dialog) |
| | |
| | | async def system_license_sync(db): |
| | | with open(os.path.join(ENV_CONF_PATH, "system.yaml") , 'r', encoding='utf-8') as file: |
| | | # 加载JSON数据 |
| | | config = json.load(file) |
| | | config = yaml.safe_load(file) |
| | | |
| | | try: |
| | | system = db.query(SystemDataModel).filter_by(id=SYSTEM_ID).first() |
| | | if system: |
| | |
| | | scheduler = BackgroundScheduler() |
| | | scheduler.add_job(sync_agents_v2, 'interval', minutes=60, id="sync_resource_data") |
| | | scheduler.add_job(start_sync_token_task, 'interval', minutes=5, id="sync_token_1") |
| | | scheduler.add_job(sync_resource, 'interval', minutes=5, id="sync_resource_1") |
| | | scheduler.add_job(sync_resource, 'interval', minutes=30, id="sync_resource_1") |
| | | |
| | | scheduler.start() |
| | | |