控件过滤

应用程序中可能有许多控制项,这些控制项可能与任务无关。UFO 可以过滤掉不相关的控制项,只关注相关的控制项。这种过滤过程可以降低任务的复杂性。

除了在 config_dev.yaml 中配置 CONTROL_LIST 上的控制类型以供选择外,UFO 还支持根据智能体的计划与控制项信息之间的语义相似性或关键字匹配来过滤控制项。我们目前支持以下过滤方法:

过滤方法 描述
文本 (Text) 根据控制文本过滤控制项。
语义 根据语义相似性过滤控制项。
图标 根据控制图标图像过滤控制项。

配置

您可以通过在 config_dev.yaml 文件中设置 CONTROL_FILTER 来激活控制过滤。CONTROL_FILTER 是您要应用于控制项的过滤方法列表,可以是 TEXTSEMANTICICON

您可以在 CONTROL_FILTER 列表中配置多种过滤方法。

参考

控制过滤的实现基于 ufo/automator/ui_control/control_filter.py 文件中的 BasicControlFilter 类。具体的过滤类继承自 BasicControlFilter 类,并实现 control_filter 方法以根据特定的过滤方法过滤控制项。

BasicControlFilter 表示用于过滤控制项的模型。

__new__(model_path)

创建一个 BasicControlFilter 的新实例。

参数
  • model_path

    模型的路径。

返回
  • BasicControlFilter 实例。

源代码在 automator/ui_control/control_filter.py
72
73
74
75
76
77
78
79
80
81
82
def __new__(cls, model_path):
    """
    Creates a new instance of BasicControlFilter.
    :param model_path: The path to the model.
    :return: The BasicControlFilter instance.
    """
    if model_path not in cls._instances:
        instance = super(BasicControlFilter, cls).__new__(cls)
        instance.model = cls.load_model(model_path)
        cls._instances[model_path] = instance
    return cls._instances[model_path]

control_filter(control_dicts, plans, **kwargs) abstractmethod

计算给定关键字的嵌入与控制项之间的余弦相似度。

参数
  • control_dicts

    要与计划进行比较的控制项。

  • plans

    用于计算相似度的计划。

返回
  • 已过滤的控件项。

源代码在 automator/ui_control/control_filter.py
104
105
106
107
108
109
110
111
112
@abstractmethod
def control_filter(self, control_dicts, plans, **kwargs):
    """
    Calculates the cosine similarity between the embeddings of the given keywords and the control item.
    :param control_dicts: The control item to be compared with the plans.
    :param plans: The plans to be used for calculating the similarity.
    :return: The filtered control items.
    """
    pass

cos_sim(embedding1, embedding2) staticmethod

计算两个嵌入之间的余弦相似度。

参数
  • embedding1

    第一个嵌入。

  • embedding2

    第二个嵌入。

返回
  • float

    两个嵌入之间的余弦相似度。

源代码在 automator/ui_control/control_filter.py
153
154
155
156
157
158
159
160
161
162
163
@staticmethod
def cos_sim(embedding1, embedding2) -> float:
    """
    Computes the cosine similarity between two embeddings.
    :param embedding1: The first embedding.
    :param embedding2: The second embedding.
    :return: The cosine similarity between the two embeddings.
    """
    import sentence_transformers

    return sentence_transformers.util.cos_sim(embedding1, embedding2)

get_embedding(content)

将给定对象编码为嵌入。

参数
  • content

    要编码的内容。

返回
  • 对象的嵌入。

源代码在 automator/ui_control/control_filter.py
 95
 96
 97
 98
 99
100
101
102
def get_embedding(self, content):
    """
    Encodes the given object into an embedding.
    :param content: The content to encode.
    :return: The embedding of the object.
    """

    return self.model.encode(content)

load_model(model_path) staticmethod

从给定模型路径加载模型。

参数
  • model_path

    模型的路径。

返回
  • 加载的模型。

源代码在 automator/ui_control/control_filter.py
84
85
86
87
88
89
90
91
92
93
@staticmethod
def load_model(model_path):
    """
    Loads the model from the given model path.
    :param model_path: The path to the model.
    :return: The loaded model.
    """
    import sentence_transformers

    return sentence_transformers.SentenceTransformer(model_path)

plans_to_keywords(plans) staticmethod

从计划中获取关键字。我们只考虑计划中是字母或汉字的词语。

参数
  • plans (List[str]) –

    要解析的计划。

返回
  • List[str]

    从计划中提取的关键字列表。

源代码在 automator/ui_control/control_filter.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
@staticmethod
def plans_to_keywords(plans: List[str]) -> List[str]:
    """
    Gets keywords from the plan. We only consider the words in the plan that are alphabetic or Chinese characters.
    :param plans: The plan to be parsed.
    :return: A list of keywords extracted from the plan.
    """

    keywords = []
    for plan in plans:
        words = plan.replace("'", "").strip(".").split()
        words = [
            word
            for word in words
            if word.isalpha() or bool(re.fullmatch(r"[\u4e00-\u9fa5]+", word))
        ]
        keywords.extend(words)
    return keywords

remove_stopwords(keywords) staticmethod

从给定的关键字列表中删除停用词。如果您是第一次使用停用词,则需要使用 nltk.download('stopwords') 下载它们。

参数
  • keywords

    要过滤的关键字列表。

返回
  • 已移除停用词的关键字列表。

源代码在 automator/ui_control/control_filter.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@staticmethod
def remove_stopwords(keywords):
    """
    Removes stopwords from the given list of keywords. If you are using stopwords for the first time, you need to download them using nltk.download('stopwords').
    :param keywords: The list of keywords to be filtered.
    :return: The list of keywords with the stopwords removed.
    """

    try:
        from nltk.corpus import stopwords

        stopwords_list = stopwords.words("english")
    except LookupError as e:
        import nltk

        nltk.download("stopwords")
        stopwords_list = nltk.corpus.stopwords.words("english")

    return [keyword for keyword in keywords if keyword in stopwords_list]