chat_data.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import json
  2. from Log import logger
  3. from app.config.config import settings
  4. # from Log import logger
  5. from app.service.v2.app_driver.chat_base import ChatBase
  6. from app.utils.rsa_crypto import RagflowCrypto
  7. class ChatBaseApply(ChatBase):
  8. async def chat_get(self, url, params, headers):
  9. res = await self.http_get(url, params, headers)
  10. if res.status_code == 200:
  11. return res.json()
  12. else:
  13. return {}
  14. async def chat_post(self, url, data, headers):
  15. res = await self.http_post(url, data, headers)
  16. if res.status_code == 200 or res.status_code == 201:
  17. return res.json()
  18. else:
  19. return {}
  20. async def chat_put(self, url, data, headers):
  21. res = await self.http_put(url, data, headers)
  22. if res.status_code == 200 or res.status_code == 201:
  23. return res.json()
  24. else:
  25. logger.error(res.text)
  26. return {}
  27. async def chat_ping(self, url, params, headers):
  28. res = await self.http_get(url, params, headers)
  29. # print(res.text)
  30. if res.status_code != 200:
  31. return 0
  32. if res.json().get("code") == "unauthorized" or res.json().get("code") == 401:
  33. return 0
  34. return 200
  35. async def chat_login(self, url, data, headers):
  36. res = await self.http_post(url, data, headers)
  37. if res.status_code == 200:
  38. res_json = res.json()
  39. authorization = res.headers.get('Authorization')
  40. if authorization:
  41. res_json["data"]["access_token"] = authorization
  42. return res_json
  43. else:
  44. return {}
  45. async def chat_upload(self, url, files, data, headers):
  46. res = await self.http_upload_file(url, headers=headers, files=files, data=data)
  47. if res.status_code == 200 or res.status_code == 201:
  48. return res.json()
  49. else:
  50. return {}
  51. @staticmethod
  52. async def password_encrypt(password):
  53. password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
  54. return password
  55. @staticmethod
  56. async def get_chat_headers(token):
  57. return {
  58. 'Content-Type': 'application/json',
  59. 'Authorization': token
  60. }
  61. if __name__ == "__main__":
  62. async def aa():
  63. chat_id = "bcb56e4b-8f21-41f1-b22a-80335fe58345"
  64. token = "app-9sbGzhtFuGIducdepzQgX06v"
  65. base_url = "http://192.168.20.116"
  66. url = f"{base_url}/v1/parameters"
  67. chat = ChatBaseApply()
  68. data = {
  69. "question": "电网技术总结300字",
  70. "stream": True,
  71. "session_id": "9969c152cce411ef8a140242ac1b0002"
  72. }
  73. params = {
  74. "user": "1"
  75. }
  76. headers = {
  77. 'Content-Type': 'application/json',
  78. 'Authorization': f"Bearer {token}"
  79. }
  80. # ans = await chat.chat_parameters(url, params, headers)
  81. # print(ans)
  82. ping_url = "http://192.168.20.116:11080/v1/user/info"
  83. # ping_url = "http://smartai.com:8294/v1/llm/list"
  84. # ping_url = "http://192.168.20.119:13002/console/api/workspaces"
  85. user_token = "eyJhbG|ciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjEzNzdiYzctZTViYy00YjhiLTgxYTYtNWZkOTVhODVlMmE4IiwiZXhwIjoxNzM5MjU3Njk1LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.w7xQrepd1dYR4iPXcbuthIZjdm45bTJFbolOM_SE9aQ"
  86. user_token = "IjU4OTM5M2UyZjMyNzExZWZhZmVjMDI0MmFjMTIwMDA2Ig.Z702yg.Pmyy58wg-YBU5t50mHuqvwTDyjc"
  87. # token = "Bearer {}"
  88. token = "{}"
  89. res = await chat.chat_ping(ping_url, {}, await chat.get_chat_headers(token.format(user_token)))
  90. print(res)
  91. import asyncio
  92. asyncio.run(aa())