| | |
| | | from typing import Union, Dict, List |
| | | from fastapi import HTTPException |
| | | from starlette import status |
| | | |
| | | from Log import logger |
| | | 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() |
| | |
| | | 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) |
| | |
| | | "inputs": {}, |
| | | "query": message, |
| | | "response_mode": "streaming", |
| | | "conversation_id": conversation_id, |
| | | "conversation_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 with httpx.AsyncClient() as client: |
| | | 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 |
| | | |
| | | |
| | | |