| | |
| | | from typing import Union, Dict, List |
| | | from fastapi import HTTPException |
| | | from starlette import status |
| | | |
| | | from app.config.config import settings |
| | | from app.utils.rsa_crypto import RagflowCrypto |
| | | |
| | |
| | | |
| | | def _handle_response(self, response: httpx.Response) -> Union[Dict, List]: |
| | | if response.status_code != 200: |
| | | if response.status_code == 201: |
| | | return response.json() |
| | | return {} |
| | | |
| | | data = response.json() |
| | |
| | | status_code=status.HTTP_401_UNAUTHORIZED, |
| | | detail="登录过期", |
| | | ) |
| | | if ret_code != 0: |
| | | return {} |
| | | # if ret_code != 0: |
| | | # return {} |
| | | |
| | | # 检查返回的数据类型 |
| | | if isinstance(data.get("data"), dict): |
| | |
| | | elif isinstance(data.get("data"), list): |
| | | return data.get("data", []) |
| | | else: |
| | | return {} |
| | | return data |
| | | |
| | | async def register(self, username: str, password: str): |
| | | password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password) |
| | |
| | | raise Exception("Authorization header not found in response") |
| | | return authorization |
| | | |
| | | async def chat(self, token: str, chat_id: str, message: str, upload_file_id: str, conversation_id: str): |
| | | async def chat(self, token: str, user_id: int, message: str, upload_file_id: str, conversation_id: str): |
| | | |
| | | target_url = f"{self.base_url}/v1/chat-messages" |
| | | files = [] |
| | |
| | | "query": message, |
| | | "response_mode": "streaming", |
| | | "conversation_id": conversation_id, |
| | | "user": chat_id, |
| | | "user": str(user_id), |
| | | "files": files |
| | | } |
| | | |
| | |
| | | if response.status_code == 200: |
| | | try: |
| | | async for answer in response.aiter_text(): |
| | | print(f"response of ragflow chat: {answer}") |
| | | # print(f"response of ragflow chat: {answer}") |
| | | yield answer |
| | | except GeneratorExit as e: |
| | | print(e) |
| | |
| | | |
| | | |
| | | |
| | | async def get_session_history(self, token: str, chat_id: str, is_all: int=0): |
| | | url = f"{self.base_url}/v1/conversation/get?conversation_id={chat_id}" |
| | | headers = {"Authorization": token} |
| | | async def get_session_history(self, token: str, conversation_id: str, user: str): |
| | | url = f"{self.base_url}/v1/messages" |
| | | params = { |
| | | 'user': user, |
| | | 'conversation_id': conversation_id |
| | | } |
| | | headers = {"Authorization": f'Bearer {token}'} |
| | | async with httpx.AsyncClient() as client: |
| | | response = await client.get(url, headers=headers) |
| | | response = await client.get(url, params=params, headers=headers) |
| | | # print(response.text) |
| | | # print(response.status_code) |
| | | # print(response.res) |
| | | data = self._handle_response(response) |
| | | # print("----------------data----------------------:", data) |
| | | if is_all: |
| | | return data |
| | | return data.get("message", []) |
| | | |
| | | async def upload(self, token: str, filename: str, file: bytes) -> dict: |
| | | url = f"{self.base_url}/console/api/files/upload" |
| | | return data |
| | | |
| | | async def upload(self, token: str, filename: str, file: bytes, user_id) -> dict: |
| | | url = f"{self.base_url}/v1/files/upload" |
| | | headers = { |
| | | 'Content-Type': 'application/json', |
| | | # 'Content-Type': 'application/json', |
| | | 'Authorization': f'Bearer {token}' |
| | | } |
| | | |
| | | data = { |
| | | 'user': str(user_id) |
| | | } |
| | | # 创建表单数据,包含文件 |
| | | files = {"file": (filename, file)} |
| | | async with httpx.AsyncClient() as client: |
| | | response = await client.post(url, headers=headers, files=files) |
| | | response = await client.post(url, headers=headers, files=files, data=data) |
| | | data = self._handle_response(response) |
| | | # file_path = data.get("file_path", "") |
| | | result = { |
| | | "file_path": data |
| | | } |
| | | |
| | | return result |
| | | return data |
| | | |
| | | async def save_images(self, url: str, filename: str): |
| | | url = f"{self.base_url}{url}" |
| | | |
| | | async with httpx.AsyncClient() as client: |
| | | response = await client.get(url) |
| | | |
| | | response.raise_for_status() |
| | | |
| | | # 打开一个文件用于写入 |
| | | with open(f"app/images/{filename}", 'wb') as f: |
| | | # 写入请求的内容 |
| | | f.write(response.content) |
| | | |
| | | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | async def a(): |
| | | a = DifyService("http://192.168.20.119:11080") |
| | | b = await a.get_knowledge_list("ImY3ZTZlZWQwYTY2NTExZWY5ZmFiMDI0MmFjMTMwMDA2Ig.Zzxwmw.uI_HAWzOkipQuga1aeQtoeIc3IM", 1, |
| | | 10) |
| | | a = DifyService("http://192.168.20.116") |
| | | b = await a.get_session_history("app-YmOAMDsPpDDlqryMHnc9TzTO", "f94c6328-8ff0-4713-af3f-e823d547682d", |
| | | "63") |
| | | print(b) |
| | | |
| | | import asyncio |