You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #
  2. # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import base64
  17. import datetime
  18. import io
  19. import json
  20. import os
  21. import pickle
  22. import socket
  23. import time
  24. import uuid
  25. import requests
  26. from enum import Enum, IntEnum
  27. import importlib
  28. from Cryptodome.PublicKey import RSA
  29. from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
  30. from filelock import FileLock
  31. from . import file_utils
  32. SERVICE_CONF = "service_conf.yaml"
  33. def conf_realpath(conf_name):
  34. conf_path = f"conf/{conf_name}"
  35. return os.path.join(file_utils.get_project_base_directory(), conf_path)
  36. def get_base_config(key, default=None, conf_name=SERVICE_CONF) -> dict:
  37. local_config = {}
  38. local_path = conf_realpath(f'local.{conf_name}')
  39. if default is None:
  40. default = os.environ.get(key.upper())
  41. if os.path.exists(local_path):
  42. local_config = file_utils.load_yaml_conf(local_path)
  43. if not isinstance(local_config, dict):
  44. raise ValueError(f'Invalid config file: "{local_path}".')
  45. if key is not None and key in local_config:
  46. return local_config[key]
  47. config_path = conf_realpath(conf_name)
  48. config = file_utils.load_yaml_conf(config_path)
  49. if not isinstance(config, dict):
  50. raise ValueError(f'Invalid config file: "{config_path}".')
  51. config.update(local_config)
  52. return config.get(key, default) if key is not None else config
  53. use_deserialize_safe_module = get_base_config('use_deserialize_safe_module', False)
  54. class CoordinationCommunicationProtocol(object):
  55. HTTP = "http"
  56. GRPC = "grpc"
  57. class BaseType:
  58. def to_dict(self):
  59. return dict([(k.lstrip("_"), v) for k, v in self.__dict__.items()])
  60. def to_dict_with_type(self):
  61. def _dict(obj):
  62. module = None
  63. if issubclass(obj.__class__, BaseType):
  64. data = {}
  65. for attr, v in obj.__dict__.items():
  66. k = attr.lstrip("_")
  67. data[k] = _dict(v)
  68. module = obj.__module__
  69. elif isinstance(obj, (list, tuple)):
  70. data = []
  71. for i, vv in enumerate(obj):
  72. data.append(_dict(vv))
  73. elif isinstance(obj, dict):
  74. data = {}
  75. for _k, vv in obj.items():
  76. data[_k] = _dict(vv)
  77. else:
  78. data = obj
  79. return {"type": obj.__class__.__name__, "data": data, "module": module}
  80. return _dict(self)
  81. class CustomJSONEncoder(json.JSONEncoder):
  82. def __init__(self, **kwargs):
  83. self._with_type = kwargs.pop("with_type", False)
  84. super().__init__(**kwargs)
  85. def default(self, obj):
  86. if isinstance(obj, datetime.datetime):
  87. return obj.strftime('%Y-%m-%d %H:%M:%S')
  88. elif isinstance(obj, datetime.date):
  89. return obj.strftime('%Y-%m-%d')
  90. elif isinstance(obj, datetime.timedelta):
  91. return str(obj)
  92. elif issubclass(type(obj), Enum) or issubclass(type(obj), IntEnum):
  93. return obj.value
  94. elif isinstance(obj, set):
  95. return list(obj)
  96. elif issubclass(type(obj), BaseType):
  97. if not self._with_type:
  98. return obj.to_dict()
  99. else:
  100. return obj.to_dict_with_type()
  101. elif isinstance(obj, type):
  102. return obj.__name__
  103. else:
  104. return json.JSONEncoder.default(self, obj)
  105. def rag_uuid():
  106. return uuid.uuid1().hex
  107. def string_to_bytes(string):
  108. return string if isinstance(string, bytes) else string.encode(encoding="utf-8")
  109. def bytes_to_string(byte):
  110. return byte.decode(encoding="utf-8")
  111. def json_dumps(src, byte=False, indent=None, with_type=False):
  112. dest = json.dumps(src, indent=indent, cls=CustomJSONEncoder, with_type=with_type)
  113. if byte:
  114. dest = string_to_bytes(dest)
  115. return dest
  116. def json_loads(src, object_hook=None, object_pairs_hook=None):
  117. if isinstance(src, bytes):
  118. src = bytes_to_string(src)
  119. return json.loads(src, object_hook=object_hook, object_pairs_hook=object_pairs_hook)
  120. def current_timestamp():
  121. return int(time.time() * 1000)
  122. def timestamp_to_date(timestamp, format_string="%Y-%m-%d %H:%M:%S"):
  123. if not timestamp:
  124. timestamp = time.time()
  125. timestamp = int(timestamp) / 1000
  126. time_array = time.localtime(timestamp)
  127. str_date = time.strftime(format_string, time_array)
  128. return str_date
  129. def date_string_to_timestamp(time_str, format_string="%Y-%m-%d %H:%M:%S"):
  130. time_array = time.strptime(time_str, format_string)
  131. time_stamp = int(time.mktime(time_array) * 1000)
  132. return time_stamp
  133. def serialize_b64(src, to_str=False):
  134. dest = base64.b64encode(pickle.dumps(src))
  135. if not to_str:
  136. return dest
  137. else:
  138. return bytes_to_string(dest)
  139. def deserialize_b64(src):
  140. src = base64.b64decode(string_to_bytes(src) if isinstance(src, str) else src)
  141. if use_deserialize_safe_module:
  142. return restricted_loads(src)
  143. return pickle.loads(src)
  144. safe_module = {
  145. 'numpy',
  146. 'rag_flow'
  147. }
  148. class RestrictedUnpickler(pickle.Unpickler):
  149. def find_class(self, module, name):
  150. import importlib
  151. if module.split('.')[0] in safe_module:
  152. _module = importlib.import_module(module)
  153. return getattr(_module, name)
  154. # Forbid everything else.
  155. raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
  156. (module, name))
  157. def restricted_loads(src):
  158. """Helper function analogous to pickle.loads()."""
  159. return RestrictedUnpickler(io.BytesIO(src)).load()
  160. def get_lan_ip():
  161. if os.name != "nt":
  162. import fcntl
  163. import struct
  164. def get_interface_ip(ifname):
  165. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  166. return socket.inet_ntoa(
  167. fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', string_to_bytes(ifname[:15])))[20:24])
  168. ip = socket.gethostbyname(socket.getfqdn())
  169. if ip.startswith("127.") and os.name != "nt":
  170. interfaces = [
  171. "bond1",
  172. "eth0",
  173. "eth1",
  174. "eth2",
  175. "wlan0",
  176. "wlan1",
  177. "wifi0",
  178. "ath0",
  179. "ath1",
  180. "ppp0",
  181. ]
  182. for ifname in interfaces:
  183. try:
  184. ip = get_interface_ip(ifname)
  185. break
  186. except IOError as e:
  187. pass
  188. return ip or ''
  189. def from_dict_hook(in_dict: dict):
  190. if "type" in in_dict and "data" in in_dict:
  191. if in_dict["module"] is None:
  192. return in_dict["data"]
  193. else:
  194. return getattr(importlib.import_module(in_dict["module"]), in_dict["type"])(**in_dict["data"])
  195. else:
  196. return in_dict
  197. def decrypt_database_password(password):
  198. encrypt_password = get_base_config("encrypt_password", False)
  199. encrypt_module = get_base_config("encrypt_module", False)
  200. private_key = get_base_config("private_key", None)
  201. if not password or not encrypt_password:
  202. return password
  203. if not private_key:
  204. raise ValueError("No private key")
  205. module_fun = encrypt_module.split("#")
  206. pwdecrypt_fun = getattr(importlib.import_module(module_fun[0]), module_fun[1])
  207. return pwdecrypt_fun(private_key, password)
  208. def decrypt_database_config(database=None, passwd_key="passwd", name="database"):
  209. if not database:
  210. database = get_base_config(name, {})
  211. database[passwd_key] = decrypt_database_password(database[passwd_key])
  212. return database
  213. def update_config(key, value, conf_name=SERVICE_CONF):
  214. conf_path = conf_realpath(conf_name=conf_name)
  215. if not os.path.isabs(conf_path):
  216. conf_path = os.path.join(file_utils.get_project_base_directory(), conf_path)
  217. with FileLock(os.path.join(os.path.dirname(conf_path), ".lock")):
  218. config = file_utils.load_yaml_conf(conf_path=conf_path) or {}
  219. config[key] = value
  220. file_utils.rewrite_yaml_conf(conf_path=conf_path, config=config)
  221. def get_uuid():
  222. return uuid.uuid1().hex
  223. def datetime_format(date_time: datetime.datetime) -> datetime.datetime:
  224. return datetime.datetime(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.minute, date_time.second)
  225. def get_format_time() -> datetime.datetime:
  226. return datetime_format(datetime.datetime.now())
  227. def str2date(date_time: str):
  228. return datetime.datetime.strptime(date_time, '%Y-%m-%d')
  229. def elapsed2time(elapsed):
  230. seconds = elapsed / 1000
  231. minuter, second = divmod(seconds, 60)
  232. hour, minuter = divmod(minuter, 60)
  233. return '%02d:%02d:%02d' % (hour, minuter, second)
  234. def decrypt(line):
  235. file_path = os.path.join(file_utils.get_project_base_directory(), "conf", "private.pem")
  236. rsa_key = RSA.importKey(open(file_path).read(), "Welcome")
  237. cipher = Cipher_pkcs1_v1_5.new(rsa_key)
  238. return cipher.decrypt(base64.b64decode(line), "Fail to decrypt password!").decode('utf-8')
  239. def download_img(url):
  240. if not url: return ""
  241. response = requests.get(url)
  242. return "data:" + \
  243. response.headers.get('Content-Type', 'image/jpg') + ";" + \
  244. "base64," + base64.b64encode(response.content).decode("utf-8")