chat_data.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if res.status_code != 200:
  16. return 0
  17. if res.json().get("code") == "unauthorized" or res.json().get("code") == 401:
  18. return 0
  19. return 200
  20. async def chat_post(self, url, data, headers):
  21. res = await self.http_post(url, data, headers)
  22. if res.status_code == 200 or res.status_code == 201:
  23. return res.json()
  24. else:
  25. return {}
  26. async def chat_login(self, url, data, headers):
  27. res = await self.http_post(url, data, headers)
  28. if res.status_code == 200:
  29. res_json = res.json()
  30. authorization = res.headers.get('Authorization')
  31. if authorization:
  32. res_json["data"]["access_token"] = authorization
  33. return res_json
  34. else:
  35. return {}
  36. @staticmethod
  37. async def password_encrypt(password):
  38. password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
  39. return password
  40. @staticmethod
  41. async def get_chat_headers(token):
  42. return {
  43. 'Content-Type': 'application/json',
  44. 'Authorization': token
  45. }
  46. if __name__ == "__main__":
  47. async def aa():
  48. chat_id = "bcb56e4b-8f21-41f1-b22a-80335fe58345"
  49. token = "app-9sbGzhtFuGIducdepzQgX06v"
  50. base_url = "http://192.168.20.116"
  51. url = f"{base_url}/v1/parameters"
  52. chat = ChatBaseApply()
  53. data = {
  54. "question": "电网技术总结300字",
  55. "stream": True,
  56. "session_id": "9969c152cce411ef8a140242ac1b0002"
  57. }
  58. params = {
  59. "user": "1"
  60. }
  61. headers = {
  62. 'Content-Type': 'application/json',
  63. 'Authorization': f"Bearer {token}"
  64. }
  65. # ans = await chat.chat_parameters(url, params, headers)
  66. # print(ans)
  67. ping_url = "http://192.168.20.116:11080/v1/system/version"
  68. # ping_url = "http://192.168.20.119:13002/console/api/workspaces"
  69. user_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjEzNzdiYzctZTViYy00YjhiLTgxYTYtNWZkOTVhODVlMmE4IiwiZXhwIjoxNzM5MjU3Njk1LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.w7xQrepd1dYR4iPXcbuthIZjdm45bTJFbolOM_SE9aQ"
  70. # token = "Bearer {}"
  71. token = "{}"
  72. res = await chat.chat_ping(ping_url, {}, await chat.get_chat_headers(token.format(user_token)))
  73. print(res)
  74. import asyncio
  75. asyncio.run(aa())