ragflow.py 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. import httpx
  2. from app.config.config import settings
  3. from app.utils.rsa_crypto import RagflowCrypto
  4. class RagflowService:
  5. def __init__(self, base_url: str):
  6. self.base_url = base_url
  7. async def register(self, username: str, password: str):
  8. password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
  9. async with httpx.AsyncClient() as client:
  10. response = await client.post(
  11. f"{self.base_url}/v1/user/register",
  12. json={"nickname": username, "email": f"{username}@example.com", "password": password},
  13. headers={'Content-Type': 'application/json'}
  14. )
  15. if response.status_code != 200:
  16. raise Exception(f"Ragflow registration failed: {response.text}")
  17. async def login(self, username: str, password: str) -> str:
  18. password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
  19. async with httpx.AsyncClient() as client:
  20. response = await client.post(
  21. f"{self.base_url}/v1/user/login",
  22. json={"email": f"{username}@example.com", "password": password},
  23. headers={'Content-Type': 'application/json'}
  24. )
  25. if response.status_code != 200:
  26. raise Exception(f"Ragflow login failed: {response.text}")
  27. return response.json().get('data', {}).get('access_token')