position_helper.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import os
  2. from collections import OrderedDict
  3. from collections.abc import Callable
  4. from typing import Any
  5. from configs import dify_config
  6. from core.tools.utils.yaml_utils import load_yaml_file
  7. def get_position_map(folder_path: str, *, file_name: str = "_position.yaml") -> dict[str, int]:
  8. """
  9. Get the mapping from name to index from a YAML file
  10. :param folder_path:
  11. :param file_name: the YAML file name, default to '_position.yaml'
  12. :return: a dict with name as key and index as value
  13. """
  14. position_file_path = os.path.join(folder_path, file_name)
  15. yaml_content = load_yaml_file(file_path=position_file_path, default_value=[])
  16. positions = [item.strip() for item in yaml_content if item and isinstance(item, str) and item.strip()]
  17. return {name: index for index, name in enumerate(positions)}
  18. def get_tool_position_map(folder_path: str, file_name: str = "_position.yaml") -> dict[str, int]:
  19. """
  20. Get the mapping for tools from name to index from a YAML file.
  21. :param folder_path:
  22. :param file_name: the YAML file name, default to '_position.yaml'
  23. :return: a dict with name as key and index as value
  24. """
  25. position_map = get_position_map(folder_path, file_name=file_name)
  26. return pin_position_map(
  27. position_map,
  28. pin_list=dify_config.POSITION_TOOL_PINS_LIST,
  29. )
  30. def get_provider_position_map(folder_path: str, file_name: str = "_position.yaml") -> dict[str, int]:
  31. """
  32. Get the mapping for providers from name to index from a YAML file.
  33. :param folder_path:
  34. :param file_name: the YAML file name, default to '_position.yaml'
  35. :return: a dict with name as key and index as value
  36. """
  37. position_map = get_position_map(folder_path, file_name=file_name)
  38. return pin_position_map(
  39. position_map,
  40. pin_list=dify_config.POSITION_PROVIDER_PINS_LIST,
  41. )
  42. def pin_position_map(original_position_map: dict[str, int], pin_list: list[str]) -> dict[str, int]:
  43. """
  44. Pin the items in the pin list to the beginning of the position map.
  45. Overall logic: exclude > include > pin
  46. :param position_map: the position map to be sorted and filtered
  47. :param pin_list: the list of pins to be put at the beginning
  48. :return: the sorted position map
  49. """
  50. positions = sorted(original_position_map.keys(), key=lambda x: original_position_map[x])
  51. # Add pins to position map
  52. position_map = {name: idx for idx, name in enumerate(pin_list)}
  53. # Add remaining positions to position map
  54. start_idx = len(position_map)
  55. for name in positions:
  56. if name not in position_map:
  57. position_map[name] = start_idx
  58. start_idx += 1
  59. return position_map
  60. def is_filtered(
  61. include_set: set[str],
  62. exclude_set: set[str],
  63. data: Any,
  64. name_func: Callable[[Any], str],
  65. ) -> bool:
  66. """
  67. Chcek if the object should be filtered out.
  68. Overall logic: exclude > include > pin
  69. :param include_set: the set of names to be included
  70. :param exclude_set: the set of names to be excluded
  71. :param name_func: the function to get the name of the object
  72. :param data: the data to be filtered
  73. :return: True if the object should be filtered out, False otherwise
  74. """
  75. if not data:
  76. return False
  77. if not include_set and not exclude_set:
  78. return False
  79. name = name_func(data)
  80. if name in exclude_set: # exclude_set is prioritized
  81. return True
  82. if include_set and name not in include_set: # filter out only if include_set is not empty
  83. return True
  84. return False
  85. def sort_by_position_map(
  86. position_map: dict[str, int],
  87. data: list[Any],
  88. name_func: Callable[[Any], str],
  89. ) -> list[Any]:
  90. """
  91. Sort the objects by the position map.
  92. If the name of the object is not in the position map, it will be put at the end.
  93. :param position_map: the map holding positions in the form of {name: index}
  94. :param name_func: the function to get the name of the object
  95. :param data: the data to be sorted
  96. :return: the sorted objects
  97. """
  98. if not position_map or not data:
  99. return data
  100. return sorted(data, key=lambda x: position_map.get(name_func(x), float('inf')))
  101. def sort_to_dict_by_position_map(
  102. position_map: dict[str, int],
  103. data: list[Any],
  104. name_func: Callable[[Any], str],
  105. ) -> OrderedDict[str, Any]:
  106. """
  107. Sort the objects into a ordered dict by the position map.
  108. If the name of the object is not in the position map, it will be put at the end.
  109. :param position_map: the map holding positions in the form of {name: index}
  110. :param name_func: the function to get the name of the object
  111. :param data: the data to be sorted
  112. :return: an OrderedDict with the sorted pairs of name and object
  113. """
  114. sorted_items = sort_by_position_map(position_map, data, name_func)
  115. return OrderedDict([(name_func(item), item) for item in sorted_items])