| | |
| | | |
| | | import jwt |
| | | # from cryptography.fernet import Fernet |
| | | from fastapi import FastAPI, Depends, HTTPException |
| | | from fastapi import FastAPI, Depends, HTTPException, Header |
| | | from fastapi.security import OAuth2PasswordBearer |
| | | from passlib.context import CryptContext |
| | | from pydantic import BaseModel |
| | |
| | | # 记录异常信息,但继续处理其他文件 |
| | | print(f"Error processing file URL: {e}") |
| | | |
| | | def get_api_key(authorization: str = Header(...)): |
| | | if not authorization.startswith("Bearer "): |
| | | raise HTTPException(status_code=401, detail="Invalid Authorization header format.") |
| | | return authorization.split(" ")[1] |
| | | |
| | | |
| | | |
| | | if __name__=="__main__": |
| | | |
| | |
| | | if agent_type == AgentType.RAGFLOW: |
| | | ragflow_service = RagflowService(base_url=settings.fwr_base_url) |
| | | try: |
| | | token = await get_ragflow_token(db, current_user.id) |
| | | result = await ragflow_service.get_chat_sessions(token, agent_id) |
| | | result = await get_session_history(db, current_user.id, agent_id, page, limit) |
| | | if not result: |
| | | result = await get_session_history(db, current_user.id, agent_id, page, limit) |
| | | token = await get_ragflow_token(db, current_user.id) |
| | | result = await ragflow_service.get_chat_sessions(token, agent_id) |
| | | except Exception as e: |
| | | print(e) |
| | | raise HTTPException(status_code=500, detail=str(e)) |
| | |
| | | chat_history = message.get('chatHistory', []) |
| | | message["role"] = "user" |
| | | if len(chat_history) == 0: |
| | | print("----------------------", token) |
| | | chat_history = await ragflow_service.get_session_history(token, chat_id) |
| | | if len(chat_history) == 0: |
| | | chat_history = await ragflow_service.set_session(token, agent_id, |
| | | message, chat_id, True) |
| | | # print("chat_history------------------------", chat_history) |
| | | print("chat_history------------------------", chat_history) |
| | | if len(chat_history) == 0: |
| | | result = {"message": "内部错误:创建会话失败", "type": "close"} |
| | | await websocket.send_json(result) |
| | |
| | | # 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"}) |
| | |
| | | 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": |
| | | |
| | | while True: |
| | | receive_message = await websocket.receive_json() |
| | |
| | | from app.models.base_model import get_db |
| | | from app.models.user_model import UserModel |
| | | from app.service.dialog import get_dialog_list, create_dialog_service, update_dialog_status_service, \ |
| | | delete_dialog_service, update_dialog_icon_service, get_dialog_manage_list |
| | | delete_dialog_service, update_dialog_icon_service, get_dialog_manage_list, sync_dialog_service |
| | | |
| | | dialog_router = APIRouter() |
| | | |
| | |
| | | |
| | | @dialog_router.put("/update_icon", response_model=Response) |
| | | async def change_dialog_icon(dialog: dialogDataUpdate, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)): |
| | | is_create = await update_dialog_icon_service(db, dialog.id, dialog.icon) |
| | | is_create = await update_dialog_icon_service(db, dialog.id, dialog.icon, dialog.name, dialog.description) |
| | | if not is_create: |
| | | return Response(code=500, msg="dialog update failure", data={}) |
| | | return Response(code=200, msg="dialog update success", data={}) |
| | |
| | | async def dialog_list_api(dialog:dialogList, |
| | | current_user: UserModel = Depends(get_current_user), |
| | | db=Depends(get_db)): |
| | | return Response(code=200, msg="", data=await get_dialog_manage_list(db, current_user.id, dialog.keyword, dialog.label, dialog.status, dialog.pageSize, dialog.current, dialog.mode)) |
| | | return Response(code=200, msg="", data=await get_dialog_manage_list(db, current_user.id, dialog.keyword, dialog.label, dialog.status, dialog.pageSize, dialog.current, dialog.mode)) |
| | | |
| | | |
| | | @dialog_router.get("/sync", response_model=Response) |
| | | async def sync_dialog_api(dialogId: str, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)): |
| | | is_create = await sync_dialog_service(db, dialogId) |
| | | if not is_create: |
| | | return Response(code=500, msg="dialog update failure", data={}) |
| | | return Response(code=200, msg="dialog update success", data={}) |
| | |
| | | |
| | | |
| | | @system_router.get("/info", response_model=Response) |
| | | async def api_get_system_data(current_user: UserModel = Depends(get_current_user), db=Depends(get_db)): |
| | | async def api_get_system_data(db=Depends(get_db)): |
| | | |
| | | data = await services_get_system_data(db) |
| | | return Response(code=200, msg="successfully", data=data) |
| | |
| | | from starlette.responses import StreamingResponse, Response |
| | | from werkzeug.http import HTTP_STATUS_CODES |
| | | |
| | | from app.api import get_current_user |
| | | from app.api import get_current_user, get_api_key |
| | | from app.config.const import dialog_chat, advanced_chat, base_chat, agent_chat, workflow_chat, basic_chat, \ |
| | | smart_message_error, http_400, http_500, http_200 |
| | | from app.models import UserModel |
| | | from app.models.base_model import get_db |
| | | from app.models.v2.chat import RetrievalRequest |
| | | 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_chat_sessions_list, service_chat_session_log, service_chunk_retrieval, service_base_chunk_retrieval |
| | | |
| | | chat_router_v2 = APIRouter() |
| | | |
| | |
| | | media_type="text/event-stream") |
| | | if not session_id: |
| | | session = await service_chat_sessions(db, chatId, dialog.query) |
| | | print(session) |
| | | if not session or session.get("code") != 0: |
| | | error_msg = json.dumps( |
| | | {"message": smart_message_error, "error": "\n**ERROR**: chat agent error", "status": http_500}) |
| | |
| | | @chat_router_v2.get("/chat/session_log") |
| | | async def api_chat_sessions(sessionId:str, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | data = await service_chat_session_log(db, sessionId) |
| | | return Response(data, media_type="application/json", status_code=http_200) |
| | | return Response(data, media_type="application/json", status_code=http_200) |
| | | |
| | | |
| | | |
| | | # @chat_router_v2.post("/conversation/mindmap") |
| | | # async def api_conversation_mindmap(chatId:str, current:int=1, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | # data = await service_chat_sessions_list(db, chatId, current, pageSize, current_user.id, keyword) |
| | | # return Response(data, media_type="application/json", status_code=http_200) |
| | | |
| | | |
| | | |
| | | @chat_router_v2.post("/retrieval") |
| | | async def retrieve_chunks(request_data: RetrievalRequest, api_key: str = Depends(get_api_key)): |
| | | records = await service_chunk_retrieval(request_data.query, request_data.knowledge_id, request_data.retrieval_setting.top_k, request_data.retrieval_setting.score_threshold, api_key) |
| | | return {"records": records} |
| | | |
| | |
| | | RG_APP_TOKEN_LIST= "/v1/system/token_list" |
| | | RG_USER_LOGIN= "/v1/user/login" |
| | | RG_PING= "/v1/system/version" |
| | | RG_ORIGINAL_URL = "/api/v1/retrieval" |
| | | |
| | | ### ---------- |
| | | DF_CHAT_AGENT= "/v1/chat-messages" |
| | |
| | | password: gAAAAABnvAq8bErFiR9x_ZcODjUeOdrDo8Z5UVOzyqo6SxIhAvLpw81kciQN0frwIFVfY9wrxH1WqrpTICpEwfH7r2SkLjS7SQ== |
| | | |
| | | chat_server: |
| | | id: 2c039666c29d11efa4670242ac1b0006 |
| | | account: zhao1@example.com |
| | | password: gAAAAABnpFLtotY2OIRH12BJh4MzMgn5Zil7-DVpIeuqlFwvr0g6g_n4ULogn-LNhCbtk6cCDkzZlqAHvBSX2e_zf7AsoyzbiQ== |
| | | id: fe24dd2c9be611ef92880242ac160006 |
| | | account: user@example.com |
| | | password: gAAAAABnvs3e3fZOYfUUAJ6uT80dkhNeN7rhylzZErTWRZThNSLzMbZGetPCe9A2BJ86V0nZBLMNNu8w6rWp4dC7JxYxByJcow== |
| | | |
| | | workflow_server: |
| | | account: admin@basic.com |
| | | password: gAAAAABnpFLtotY2OIRH12BJh4MzMgn5Zil7-DVpIeuqlFwvr0g6g_n4ULogn-LNhCbtk6cCDkzZlqAHvBSX2e_zf7AsoyzbiQ== |
| | | account: basic@mail.com |
| | | password: gAAAAABnvs5i7xUn9pb2szCozJciGSiWPGv80PH_2HFFzNM2r1ZLTOQqftnUso_bvchtmwAmccfNrf53sf9_WMFVTc0hjTKRRQ== |
| | |
| | | dify_database_url: postgresql+psycopg2://postgres:difyai123456@192.168.20.119:15432/dify |
| | | |
| | | |
| | | |
| | |
| | | secret_key: your-secret-key |
| | | sgb_base_url: http://192.168.20.116:13001 |
| | | sgb_websocket_url: ws://192.168.20.116:13001 |
| | | fwr_base_url: http://192.168.20.116:11080 |
| | | sgb_base_url: http://192.168.20.119:13001 |
| | | sgb_websocket_url: ws://192.168.20.119:13001 |
| | | fwr_base_url: http://192.168.20.119:11080 |
| | | database_url: mysql+pymysql://root:rag_gateway@192.168.20.116:23306/rag_gateway |
| | | 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.116:15455/rag_flow |
| | | 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 |
| | |
| | | dify_workflow_clean: app-OpF0drPu0XcgqcekQpT4FA8a |
| | | dify_workflow_report: app-0MAkdFWqh9zxwmU69O0BFU1s |
| | | dify_database_url: postgresql+psycopg2://postgres:difyai123456@192.168.20.116:15432/dify |
| | | |
| | | |
| | | |
| | |
| | | "rank": 105, |
| | | "dialog": [ |
| | | { |
| | | "id": "6b8ee426c67511efb1510242ac1b0006", |
| | | "chat_id": "6b8ee426c67511efb1510242ac1b0006", |
| | | "id": "ee0a3e38f5c211efb7600242ac1a0006", |
| | | "chat_id": "ee0a3e38f5c211efb7600242ac1a0006", |
| | | "chat_type": "knowledgeQA", |
| | | "agentType": 1 |
| | | } |
| | |
| | | from app.models.base_model import SessionLocal |
| | | from app.service.v2.initialize_data import dialog_menu_sync, default_group_sync, default_role_sync, \ |
| | | basic_agent_sync, admin_account_sync, sync_rg_api_token |
| | | from app.task.sync_account_token import sync_token |
| | | |
| | | |
| | | async def sync_default_data(): |
| | |
| | | await basic_agent_sync(db) # 开发的agent |
| | | await admin_account_sync(db) # |
| | | await sync_rg_api_token(db) # |
| | | await sync_token() # 启动同步token任务 |
| | | |
| | | except Exception as e: |
| | | print(e) |
| | |
| | | id: str |
| | | status: Optional[str] = "1" |
| | | icon: Optional[str] = "" |
| | | name: Optional[str] = "" |
| | | description: Optional[str] = None |
| | | |
| | | |
| | | class dialogList(BaseModel): |
New file |
| | |
| | | from pydantic import BaseModel |
| | | |
| | | |
| | | |
| | | class RetrievalSetting(BaseModel): |
| | | top_k: int |
| | | score_threshold: float |
| | | |
| | | |
| | | class RetrievalRequest(BaseModel): |
| | | knowledge_id: str |
| | | query: str |
| | | retrieval_setting: RetrievalSetting |
| | | |
| | | |
| | | |
New file |
| | |
| | | import json |
| | | from typing import Optional, Type, List |
| | | from pydantic import BaseModel |
| | | |
| | | |
| | | |
| | | |
| | | class ChatData(BaseModel): |
| | | sessionId: Optional[str] = "" |
| | | |
| | | class Config: |
| | | extra = 'allow' # 允许其他动态字段 |
| | |
| | | |
| | | SECRET_KEY = settings.secret_key |
| | | ALGORITHM = "HS256" |
| | | ACCESS_TOKEN_EXPIRE_MINUTES = 3000 |
| | | ACCESS_TOKEN_EXPIRE_MINUTES = 24*60 |
| | | |
| | | pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") |
| | | |
| | |
| | | |
| | | from app.config.agent_base_url import DF_CHAT_PARAMETERS, DF_CHAT_API_KEY |
| | | from app.config.config import settings |
| | | from app.config.const import Dialog_STATSU_DELETE, DF_TYPE, Dialog_STATSU_ON, workflow_server |
| | | from app.config.const import Dialog_STATSU_DELETE, DF_TYPE, Dialog_STATSU_ON, workflow_server, RG_TYPE |
| | | from app.models import KnowledgeModel, GroupModel, DialogModel, ConversationModel, group_dialog_table, LabelWorkerModel, \ |
| | | LabelModel, ApiTokenModel |
| | | from app.models.user_model import UserModel, UserTokenModel |
| | | from Log import logger |
| | | from app.service.v2.app_driver.chat_data import ChatBaseApply |
| | | from app.service.v2.chat import get_chat_token, add_chat_token, get_app_token |
| | | from app.task.fetch_agent import get_one_from_ragflow_dialog |
| | | |
| | | |
| | | async def get_dialog_list(db, user_id, keyword, label, status, page_size, page_index): |
| | |
| | | return True |
| | | |
| | | |
| | | async def update_dialog_icon_service(db, dialog_id, icon): |
| | | async def update_dialog_icon_service(db, dialog_id, icon, name, description): |
| | | update = {"icon": icon, "update_date": datetime.now()} |
| | | if name: |
| | | update["name"] = name |
| | | if description or description == "": |
| | | update["description"] = description |
| | | try: |
| | | db.query(DialogModel).filter_by(id=dialog_id).update({"icon": icon, "update_date": datetime.now()}) |
| | | db.query(DialogModel).filter_by(id=dialog_id).update(update) |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | |
| | | r["user"] = user_dict.get(r["user_id"], {}) |
| | | r["label"] = label_dict.get(r["id"], []) |
| | | return {"total": total, "rows": rows} |
| | | |
| | | |
| | | |
| | | async def sync_dialog_service(db, dialog_id): |
| | | dialog = db.query(DialogModel).filter(DialogModel.id == dialog_id).first() |
| | | if dialog and dialog.dialog_type == RG_TYPE: |
| | | try: |
| | | app_dialog = get_one_from_ragflow_dialog(dialog_id) |
| | | if app_dialog: |
| | | dialog.name = app_dialog["name"] |
| | | dialog.description = app_dialog["description"] |
| | | dialog.update_date = datetime.now() |
| | | db.add(dialog) |
| | | db.commit() |
| | | db.refresh(dialog) |
| | | except Exception as e: |
| | | logger.error(e) |
| | | db.rollback() |
| | | return False |
| | | return True |
| | |
| | | return {} |
| | | |
| | | data = response.json() |
| | | ret_code = data.get("retcode") |
| | | ret_code = data.get("retcode", data.get("code")) |
| | | if ret_code == 401: |
| | | raise HTTPException( |
| | | status_code=status.HTTP_401_UNAUTHORIZED, |
| | |
| | | } |
| | | async with httpx.AsyncClient() as client: |
| | | response = await client.post(url, headers=headers, json=data) |
| | | print(response.status_code) |
| | | print(response.text) |
| | | data = self._handle_response(response) |
| | | return [ |
| | | { |
| | |
| | | from Log import logger |
| | | from app.config.config import settings |
| | | from app.config.const import BISHENG, RAGFLOW, DIFY |
| | | from app.models import UserModel, UserAppModel |
| | | from app.config.const import BISHENG, RAGFLOW, DIFY, chat_server |
| | | from app.models import UserModel, UserAppModel, UserTokenModel |
| | | from app.models.token_model import TokenModel |
| | | from app.service.auth import UserAppDao |
| | | from app.service.bisheng import BishengService |
| | |
| | | async def get_ragflow_token(db, user_id: int): |
| | | # token = await UserAppDao(db).get_data_by_id(user_id, RAGFLOW) |
| | | token = db.query(TokenModel).filter(TokenModel.user_id == user_id).first() |
| | | token = db.query(UserTokenModel).filter(UserTokenModel.id == chat_server).first() |
| | | if not token: |
| | | token = await UserAppDao(db).get_data_by_id(user_id, RAGFLOW) |
| | | if not token: |
| | | return None |
| | | return token.access_token |
| | | else: |
| | | return token.ragflow_token |
| | | return token.access_token |
| | | |
| | | |
| | | async def get_dify_token(db, user_id: int): |
| | |
| | | |
| | | async def services_update_system_data(db, title, desc, logo): |
| | | try: |
| | | shutil.move(os.path.join(APP_STATIC_PATH, logo), os.path.join(APP_STATIC_PATH, "logo.png")) |
| | | if os.path.exists(os.path.join(APP_STATIC_PATH, logo)): |
| | | shutil.move(os.path.join(APP_STATIC_PATH, logo), os.path.join(APP_STATIC_PATH, "logo.png")) |
| | | system = db.query(SystemDataModel).filter_by(id=SYSTEM_ID).first() |
| | | system.title = title |
| | | system.desc = desc |
| | |
| | | import asyncio |
| | | import io |
| | | import json |
| | | |
| | | import fitz |
| | | from fastapi import HTTPException |
| | | |
| | | 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 |
| | | DF_CHAT_WORKFLOW, DF_UPLOAD_FILE, RG_ORIGINAL_URL |
| | | from app.config.config import settings |
| | | from app.config.const import * |
| | | from app.models import DialogModel, ApiTokenModel, UserTokenModel |
| | |
| | | query = chat_data.query |
| | | else: |
| | | query = "start new workflow" |
| | | session = await add_session_log(db, session_id, query, chat_id, user_id, mode, conversation_id, 3) |
| | | session = await add_session_log(db, session_id,query if query else "start new conversation", chat_id, user_id, mode, conversation_id, 3) |
| | | if session: |
| | | conversation_id = session.conversation_id |
| | | try: |
| | |
| | | data["outputs"] = await data_process(data.get("outputs", {})) |
| | | data["files"] = await data_process(data.get("files", [])) |
| | | data["process_data"] = "" |
| | | if data.get("status") == "failed": |
| | | status = http_500 |
| | | error = data.get("error", "") |
| | | node_list.append(ans) |
| | | event = [smart_workflow_started, smart_node_started, smart_node_finished][ |
| | | [workflow_started, node_started, node_finished].index(ans.get("event"))] |
| | |
| | | answer_workflow = data.get("outputs", {}).get("output") |
| | | download_url = data.get("outputs", {}).get("download_url") |
| | | event = smart_workflow_finished |
| | | if data.get("status") == "failed": |
| | | status = http_500 |
| | | error = data.get("error", "") |
| | | node_list.append(ans) |
| | | |
| | | elif ans.get("event") == message_end: |
| | |
| | | except: |
| | | ... |
| | | finally: |
| | | await update_session_log(db, session_id, {"role": "assistant", "answer": answer_event or answer_agent or answer_workflow, |
| | | await update_session_log(db, session_id, {"role": "assistant", "answer": answer_event or answer_agent or answer_workflow or error, |
| | | "download_url":download_url, |
| | | "node_list": node_list, "task_id": task_id, "id": message_id, |
| | | "error": error}, conversation_id) |
| | |
| | | |
| | | async def service_chat_sessions(db, chat_id, name): |
| | | token = await get_chat_token(db, rg_api_token) |
| | | # print(token) |
| | | if not token: |
| | | return {} |
| | | url = settings.fwr_base_url + RG_CHAT_SESSIONS.format(chat_id) |
| | |
| | | text = await read_word(file) |
| | | |
| | | return await get_str_token(text) |
| | | |
| | | |
| | | async def service_chunk_retrieval(query, knowledge_id, top_k, similarity_threshold, api_key): |
| | | print(query) |
| | | |
| | | try: |
| | | request_data = json.loads(query) |
| | | payload = { |
| | | "question": request_data.get("query", ""), |
| | | "dataset_ids": request_data.get("dataset_ids", []), |
| | | "page_size": top_k, |
| | | "similarity_threshold": similarity_threshold |
| | | } |
| | | except json.JSONDecodeError as e: |
| | | fixed_json = query.replace("'", '"') |
| | | try: |
| | | request_data = json.loads(fixed_json) |
| | | payload = { |
| | | "question": request_data.get("query", ""), |
| | | "dataset_ids": request_data.get("dataset_ids", []), |
| | | "page_size": top_k, |
| | | "similarity_threshold": similarity_threshold |
| | | } |
| | | except Exception: |
| | | payload = { |
| | | "question":query, |
| | | "dataset_ids":[knowledge_id], |
| | | "page_size": top_k, |
| | | "similarity_threshold": similarity_threshold |
| | | } |
| | | url = settings.fwr_base_url + RG_ORIGINAL_URL |
| | | chat = ChatBaseApply() |
| | | response = await chat.chat_post(url, payload, await chat.get_headers(api_key)) |
| | | if not response: |
| | | raise HTTPException(status_code=500, detail="服务异常!") |
| | | records = [ |
| | | { |
| | | "content": chunk["content"], |
| | | "score": chunk["similarity"], |
| | | "title": chunk.get("document_keyword", "Unknown Document"), |
| | | "metadata": {"document_id": chunk["document_id"], |
| | | "path": f"{settings.fwr_base_url}/document/{chunk['document_id']}?ext={chunk.get('document_keyword').split('.')[-1]}&prefix=document", |
| | | 'highlight': chunk.get("highlight") , "image_id": chunk.get("image_id"), "positions": chunk.get("positions"),} |
| | | } |
| | | for chunk in response.get("data", {}).get("chunks", []) |
| | | ] |
| | | return records |
| | | |
| | | async def service_base_chunk_retrieval(query, knowledge_id, top_k, similarity_threshold, api_key): |
| | | # request_data = json.loads(query) |
| | | payload = { |
| | | "question": query, |
| | | "dataset_ids": [knowledge_id], |
| | | "page_size": top_k, |
| | | "similarity_threshold": similarity_threshold |
| | | } |
| | | url = settings.fwr_base_url + RG_ORIGINAL_URL |
| | | # url = "http://192.168.20.116:11080/" + RG_ORIGINAL_URL |
| | | chat = ChatBaseApply() |
| | | response = await chat.chat_post(url, payload, await chat.get_headers(api_key)) |
| | | if not response: |
| | | raise HTTPException(status_code=500, detail="服务异常!") |
| | | records = [ |
| | | { |
| | | "content": chunk["content"], |
| | | "score": chunk["similarity"], |
| | | "title": chunk.get("document_keyword", "Unknown Document"), |
| | | "metadata": {"document_id": chunk["document_id"]} |
| | | } |
| | | for chunk in response.get("data", {}).get("chunks", []) |
| | | ] |
| | | return records |
| | | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | q = json.dumps({"query": "设备", "dataset_ids": ["fc68db52f43111efb94a0242ac120004"]}) |
| | | top_k = 2 |
| | | similarity_threshold = 0.5 |
| | | api_key = "ragflow-Y4MGYwY2JlZjM2YjExZWY4ZWU5MDI0Mm" |
| | | # a = service_chunk_retrieval(q, top_k, similarity_threshold, api_key) |
| | | # print(a) |
| | | async def a(): |
| | | b = await service_chunk_retrieval(q, top_k, similarity_threshold, api_key) |
| | | print(b) |
| | | asyncio.run(a()) |
| | |
| | | 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() |
| | | |
| | | |
| | | def sync_knowledge(): |
| | | db = SessionLocal() |
| | | |
| | |
| | | from contextlib import asynccontextmanager |
| | | from apscheduler.schedulers.background import BackgroundScheduler |
| | | from fastapi import FastAPI |
| | | from fastapi.middleware.cors import CORSMiddleware |
| | | from starlette.staticfiles import StaticFiles |
| | | |
| | | from app.api.auth import router as auth_router |
| | | from app.api.canvas import canvas_router |
| | | from app.api.chat import router as chat_router |
| | |
| | | from app.api.v2.public_api import public_api |
| | | from app.api.report import router as report_router |
| | | from app.api.resource import menu_router |
| | | # from app.api.sync_data import sync_router |
| | | from app.api.user import user_router |
| | | from app.api.group import group_router |
| | | from app.api.role import role_router |
| | | from app.models.base_model import init_db |
| | | from app.task.delete_execl_file import delete_file_after_delay |
| | | # from app.models.base_model import init_db |
| | | from app.task.fetch_agent import sync_agents, initialize_agents, sync_agents_v2, sync_knowledge, \ |
| | | from app.task.fetch_agent import sync_agents_v2, sync_knowledge, \ |
| | | sync_resources_from_json |
| | | from app.init_config.init_run_data import sync_default_data |
| | | from app.task.sync_account_token import start_sync_token_task, sync_token |
| | | from app.task.sync_account_token import start_sync_token_task |
| | | |
| | | init_db() |
| | | |
| | |
| | | # 使用 Lifespan 事件处理程序 |
| | | @asynccontextmanager |
| | | async def lifespan(app: FastAPI): |
| | | # 初始化代理 |
| | | # initialize_agents() |
| | | # # 在应用启动时同步代理 |
| | | # sync_agents() |
| | | |
| | | |
| | | await sync_default_data() |
| | | await sync_default_data() # Todo |
| | | sync_agents_v2() # 智能体 |
| | | sync_knowledge() # 知识库 |
| | | sync_resources_from_json() |
| | | await sync_token() # 启动同步token任务 |
| | | yield |
| | | # 在应用关闭时执行清理操作(如果需要) |
| | | pass |
| | |
| | | lifespan=lifespan |
| | | ) |
| | | |
| | | # 设置 CORS 中间件 |
| | | # app.add_middleware( |
| | | # CORSMiddleware, |
| | | # allow_origins=["*"], |
| | | # allow_credentials=True, |
| | | # allow_methods=["*"], # 允许所有方法 |
| | | # allow_headers=["*"], # 允许所有头部 |
| | | # ) |
| | | |
| | | # 创建调度器 |
| | | 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(delete_file_after_delay, 'interval', minutes=10, id="delete_file_after_delay") |
| | | |
| | | scheduler.start() |
| | | |
| | | app.include_router(auth_router, prefix='/api/auth', tags=["auth"]) |