chat_data.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import json
  2. from app.config.config import settings
  3. # from Log import logger
  4. from app.service.v2.app_driver.chat_base import ChatBase
  5. from app.utils.rsa_crypto import RagflowCrypto
  6. class ChatBaseApply(ChatBase):
  7. async def chat_get(self, url, params, headers):
  8. res = await self.http_get(url, params, headers)
  9. if res.status_code == 200:
  10. return res.json()
  11. else:
  12. return {}
  13. async def chat_ping(self, url, params, headers):
  14. res = await self.http_get(url, params, headers)
  15. return res.status_code
  16. async def chat_post(self, url, data, headers):
  17. res = await self.http_post(url, data, headers)
  18. if res.status_code == 200:
  19. return res.json()
  20. else:
  21. return {}
  22. async def chat_login(self, url, data, headers):
  23. res = await self.http_post(url, data, headers)
  24. if res.status_code == 200:
  25. res_json = res.json()
  26. authorization = res.headers.get('Authorization')
  27. if authorization:
  28. res_json["data"]["access_token"] = authorization
  29. return res_json
  30. else:
  31. return {}
  32. @staticmethod
  33. async def password_encrypt(password):
  34. password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
  35. return password
  36. if __name__ == "__main__":
  37. async def aa():
  38. chat_id = "bcb56e4b-8f21-41f1-b22a-80335fe58345"
  39. token = "app-9sbGzhtFuGIducdepzQgX06v"
  40. base_url = "http://192.168.20.116"
  41. url = f"{base_url}/v1/parameters"
  42. chat = ChatBaseApply()
  43. data = {
  44. "question": "电网技术总结300字",
  45. "stream": True,
  46. "session_id": "9969c152cce411ef8a140242ac1b0002"
  47. }
  48. params = {
  49. "user": "1"
  50. }
  51. headers = {
  52. 'Content-Type': 'application/json',
  53. 'Authorization': f"Bearer {token}"
  54. }
  55. ans = await chat.chat_parameters(url, params, headers)
  56. print(ans)
  57. import asyncio
  58. asyncio.run(aa())