语义控制过滤器

语义控制过滤器是一种方法,通过使用智能体的计划和控件文本的嵌入,根据它们之间的语义相似性来过滤控件。

配置

要激活语义控制过滤,您需要在 config_dev.yaml 文件中的 CONTROL_FILTER 列表中添加 SEMANTIC。以下是 config_dev.yaml 文件中详细的语义控制过滤器配置:

  • CONTROL_FILTER: 您想要应用于控件的过滤方法列表。要激活语义控制过滤,请将 SEMANTIC 添加到列表中。
  • CONTROL_FILTER_TOP_K_SEMANTIC: 过滤后保留的控件数量。
  • CONTROL_FILTER_MODEL_SEMANTIC_NAME: 用于语义相似性的控制过滤器模型名称。默认设置为 "all-MiniLM-L6-v2"。

参考

基类:BasicControlFilter

表示用于控制过滤的语义模型的类。

control_filter(control_dicts, plans, top_k)

根据控件项与一组关键词的相似性来过滤控件项。

参数
  • control_dicts

    要过滤的控件项字典。

  • plans

    用于过滤的计划列表。

  • top_k

    要返回的前 k 个控件项的数量。

返回
  • 已过滤的控件项。

源代码在 automator/ui_control/control_filter.py
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def control_filter(self, control_dicts, plans, top_k):
    """
    Filters control items based on their similarity to a set of keywords.
    :param control_dicts: The dictionary of control items to be filtered.
    :param plans: The list of plans to be used for filtering.
    :param top_k: The number of top control items to return.
    :return: The filtered control items.
    """
    scores_items = []
    filtered_control_dict = {}

    for label, control_item in control_dicts.items():
        control_text = control_item.element_info.name.lower()
        score = self.control_filter_score(control_text, plans)
        scores_items.append((label, score))
    topk_scores_items = heapq.nlargest(top_k, (scores_items), key=lambda x: x[1])
    topk_items = [
        (score_item[0], score_item[1]) for score_item in topk_scores_items
    ]

    for label, control_item in control_dicts.items():
        if label in topk_items:
            filtered_control_dict[label] = control_item
    return filtered_control_dict

control_filter_score(control_text, plans)

根据控件项文本与一组关键词之间的相似性计算其得分。

参数
  • control_text

    控件项的文本。

  • plans

    用于计算相似性的计划。

返回
  • 表示控件文本与关键词之间相似性的得分 (0-1)。

源代码在 automator/ui_control/control_filter.py
197
198
199
200
201
202
203
204
205
206
207
def control_filter_score(self, control_text, plans):
    """
    Calculates the score for a control item based on the similarity between its text and a set of keywords.
    :param control_text: The text of the control item.
    :param plans: The plan to be used for calculating the similarity.
    :return: The score (0-1) indicating the similarity between the control text and the keywords.
    """

    plan_embedding = self.get_embedding(plans)
    control_text_embedding = self.get_embedding(control_text)
    return max(self.cos_sim(control_text_embedding, plan_embedding).tolist()[0])