chat_data.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 or res.status_code == 201:
  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. @staticmethod
  37. async def get_chat_headers(token):
  38. return {
  39. 'Content-Type': 'application/json',
  40. 'Authorization': token
  41. }
  42. if __name__ == "__main__":
  43. async def aa():
  44. chat_id = "bcb56e4b-8f21-41f1-b22a-80335fe58345"
  45. token = "app-9sbGzhtFuGIducdepzQgX06v"
  46. base_url = "http://192.168.20.116"
  47. url = f"{base_url}/v1/parameters"
  48. chat = ChatBaseApply()
  49. data = {
  50. "question": "电网技术总结300字",
  51. "stream": True,
  52. "session_id": "9969c152cce411ef8a140242ac1b0002"
  53. }
  54. params = {
  55. "user": "1"
  56. }
  57. headers = {
  58. 'Content-Type': 'application/json',
  59. 'Authorization': f"Bearer {token}"
  60. }
  61. # ans = await chat.chat_parameters(url, params, headers)
  62. # print(ans)
  63. ping_url = "http://192.168.20.119:13002/console/api/workspaces"
  64. user_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNzBhNWVhZWYtYzU4Yi00YTE0LTliOGQtYWE2MzY0ZmE1ZDE3IiwiZXhwIjoxNzM5MjU2MDMyLCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.tlxdRIVL2Ewgb0V4M5pj6v_U2yyvQxtCDrbvSxLjUBs"
  65. token = "Bearer {}"
  66. res = await chat.chat_ping(ping_url, {}, await chat.get_chat_headers(token.format(user_token)))
  67. print(res)
  68. import asyncio
  69. asyncio.run(aa())