强化 AppAgent

UFO 提供多种机制,通过 RAG(检索增强生成)和其他技术来增强 AppAgent 的能力。这些机制提高了 AppAgent 对任务的理解,改善了生成计划的质量,并提高了 AppAgent 与应用程序交互的效率。

我们目前支持以下强化方法:

强化方法 描述
从帮助文档中学习 通过从帮助文档中检索知识来强化 AppAgent。
从 Bing 搜索学习 通过在 Bing 上搜索信息以获取最新知识来强化 AppAgent。
从自身经验中学习 通过从 AppAgent 自身的成功经验中学习来强化它。
从用户演示中学习 通过从用户演示的操作轨迹中学习来强化 AppAgent。

知识提供

UFO 通过在 AppAgent 类中定义的 context_provision 方法向 AppAgent 提供知识。

def context_provision(self, request: str = "") -> None:
    """
    Provision the context for the app agent.
    :param request: The Bing search query.
    """

    # Load the offline document indexer for the app agent if available.
    if configs["RAG_OFFLINE_DOCS"]:
        utils.print_with_color(
            "Loading offline help document indexer for {app}...".format(
                app=self._process_name
            ),
            "magenta",
        )
        self.build_offline_docs_retriever()

    # Load the online search indexer for the app agent if available.

    if configs["RAG_ONLINE_SEARCH"] and request:
        utils.print_with_color("Creating a Bing search indexer...", "magenta")
        self.build_online_search_retriever(
            request, configs["RAG_ONLINE_SEARCH_TOPK"]
        )

    # Load the experience indexer for the app agent if available.
    if configs["RAG_EXPERIENCE"]:
        utils.print_with_color("Creating an experience indexer...", "magenta")
        experience_path = configs["EXPERIENCE_SAVED_PATH"]
        db_path = os.path.join(experience_path, "experience_db")
        self.build_experience_retriever(db_path)

    # Load the demonstration indexer for the app agent if available.
    if configs["RAG_DEMONSTRATION"]:
        utils.print_with_color("Creating an demonstration indexer...", "magenta")
        demonstration_path = configs["DEMONSTRATION_SAVED_PATH"]
        db_path = os.path.join(demonstration_path, "demonstration_db")
        self.build_human_demonstration_retriever(db_path)

context_provision 方法根据 config_dev.yaml 文件中的配置设置加载 AppAgent 的离线文档索引器、在线搜索索引器、经验索引器和演示索引器。

参考

UFO 使用位于 ufo/rag/retriever.py 文件中的 Retriever 类从各种来源检索知识。Retriever 类提供以下方法来检索知识:

基类:ABC

用于检索文档的类。

创建一个新的检索器。

源代码位于 rag/retriever.py
42
43
44
45
46
47
48
49
def __init__(self) -> None:
    """
    Create a new Retriever.
    """

    self.indexer = self.get_indexer()

    pass

get_indexer() 抽象方法

获取索引器。

返回
  • 索引器。

源代码位于 rag/retriever.py
51
52
53
54
55
56
57
@abstractmethod
def get_indexer(self):
    """
    Get the indexer.
    :return: The indexer.
    """
    pass

retrieve(query, top_k, filter=None)

从给定查询中检索文档。:filter: 要应用于检索到的文档的过滤器。

参数
  • query (str) –

    用于检索文档的查询。

  • top_k (int) –

    要检索的文档数量。

返回
  • 从给定查询中检索到的文档。

源代码位于 rag/retriever.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def retrieve(self, query: str, top_k: int, filter=None):
    """
    Retrieve the document from the given query.
    :param query: The query to retrieve the document from.
    :param top_k: The number of documents to retrieve.
    :filter: The filter to apply to the retrieved documents.
    :return: The document from the given query.
    """
    if not self.indexer:
        return []

    results = self.indexer.similarity_search(query, top_k, filter=filter)

    if not results:
        return []
    else:
        return results