position_helper.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 sort_and_filter_position_map(
  27. position_map,
  28. pin_list=dify_config.POSITION_TOOL_PINS_LIST,
  29. include_list=dify_config.POSITION_TOOL_INCLUDES_LIST,
  30. exclude_list=dify_config.POSITION_TOOL_EXCLUDES_LIST
  31. )
  32. def get_provider_position_map(folder_path: str, file_name: str = "_position.yaml") -> dict[str, int]:
  33. """
  34. Get the mapping for providers from name to index from a YAML file.
  35. :param folder_path:
  36. :param file_name: the YAML file name, default to '_position.yaml'
  37. :return: a dict with name as key and index as value
  38. """
  39. position_map = get_position_map(folder_path, file_name=file_name)
  40. return sort_and_filter_position_map(
  41. position_map,
  42. pin_list=dify_config.POSITION_PROVIDER_PINS_LIST,
  43. include_list=dify_config.POSITION_PROVIDER_INCLUDES_LIST,
  44. exclude_list=dify_config.POSITION_PROVIDER_EXCLUDES_LIST
  45. )
  46. def sort_and_filter_position_map(original_position_map: dict[str, int], pin_list: list[str], include_list: list[str], exclude_list: list[str]) -> dict[str, int]:
  47. """
  48. Sort and filter the positions
  49. :param position_map: the position map to be sorted and filtered
  50. :param pin_list: the list of pins to be put at the beginning
  51. :param include_set: the set of names to be included
  52. :param exclude_set: the set of names to be excluded
  53. :return: the sorted and filtered position map
  54. """
  55. positions = sorted(original_position_map.keys(), key=lambda x: original_position_map[x])
  56. include_set = set(include_list) if include_list else set(positions)
  57. exclude_set = set(exclude_list) if exclude_list else set()
  58. # Add pins to position map
  59. position_map = {name: idx for idx, name in enumerate(pin_list) if name in original_position_map}
  60. # Add remaining positions to position map, respecting include and exclude lists
  61. start_idx = len(position_map)
  62. for name in positions:
  63. if name in include_set and name not in exclude_set and name not in position_map:
  64. position_map[name] = start_idx
  65. start_idx += 1
  66. return position_map
  67. def sort_by_position_map(
  68. position_map: dict[str, int],
  69. data: list[Any],
  70. name_func: Callable[[Any], str],
  71. ) -> list[Any]:
  72. """
  73. Sort the objects by the position map.
  74. If the name of the object is not in the position map, it will be put at the end.
  75. :param position_map: the map holding positions in the form of {name: index}
  76. :param name_func: the function to get the name of the object
  77. :param data: the data to be sorted
  78. :return: the sorted objects
  79. """
  80. if not position_map or not data:
  81. return data
  82. filtered_data = [item for item in data if name_func(item) in position_map]
  83. return sorted(filtered_data, key=lambda x: position_map.get(name_func(x), float('inf')))
  84. def sort_to_dict_by_position_map(
  85. position_map: dict[str, int],
  86. data: list[Any],
  87. name_func: Callable[[Any], str],
  88. ) -> OrderedDict[str, Any]:
  89. """
  90. Sort the objects into a ordered dict by the position map.
  91. If the name of the object is not in the position map, it will be put at the end.
  92. :param position_map: the map holding positions in the form of {name: index}
  93. :param name_func: the function to get the name of the object
  94. :param data: the data to be sorted
  95. :return: an OrderedDict with the sorted pairs of name and object
  96. """
  97. sorted_items = sort_by_position_map(position_map, data, name_func)
  98. return OrderedDict([(name_func(item), item) for item in sorted_items])