llama_index 我想构建一个多层次的工具或多层次的代理结构,但不知道如何实现?

smdnsysy  于 4个月前  发布在  其他
关注(0)|答案(4)|浏览(78)

问题验证

  • 我已在文档和Discord中寻找答案。

问题

tools1 = tools.from_tools(
tools = [tool1,tool2,tool3]
)
tools2 = tools.from_tools(
tools = [tool4,tool5,tool6]
)
ReActAgent.from_tools(
tools=[tools1,tools2]
)

是否有任何方法可以实现类似于上述的内容?

vwhgwdsa

vwhgwdsa1#

要构建多层次的工具或多层次的代理结构,可以遵循以下步骤:

  1. 设置文档代理:为每个文档创建一个文档代理。每个文档代理都可以在其文档内执行诸如QA和摘要等任务。
  2. 设置顶层代理:创建一个可以协调不同文档代理以回答任何用户查询的顶层代理。
    以下是相关的代码片段:

为每个文档构建文档代理

from llama_index.agent.openai import OpenAIAgent
from llama_index.core import load_index_from_storage, StorageContext, VectorStoreIndex, SummaryIndex
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.tools import QueryEngineTool, ToolMetadata
import os

node_parser = SentenceSplitter()

# Build agents dictionary
agents = {}
query_engines = {}
# this is for the baseline
all_nodes = []

for idx, wiki_title in enumerate(wiki_titles):
    nodes = node_parser.get_nodes_from_documents(city_docs[wiki_title])
    all_nodes.extend(nodes)

    if not os.path.exists(f"./data/{wiki_title}"):
        # build vector index
        vector_index = VectorStoreIndex(nodes)
        vector_index.storage_context.persist(
            persist_dir=f"./data/{wiki_title}"
        )
    else:
        vector_index = load_index_from_storage(
            StorageContext.from_defaults(persist_dir=f"./data/{wiki_title}"),
        )

    # build summary index
    summary_index = SummaryIndex(nodes)
    # define query engines
    vector_query_engine = vector_index.as_query_engine(llm=Settings.llm)
    summary_query_engine = summary_index.as_query_engine(llm=Settings.llm)

    # define tools
    query_engine_tools = [
        QueryEngineTool(
            query_engine=vector_query_engine,
            metadata=ToolMetadata(
                name="vector_tool",
                description=(
                    "Useful for questions related to specific aspects of"
                    f" {wiki_title} (e.g. the history, arts and culture,"
                    " sports, demographics, or more)."
                ),
            ),
        ),
        QueryEngineTool(
            query_engine=summary_query_engine,
            metadata=ToolMetadata(
                name="summary_tool",
                description=(
                    "Useful for any requests that require a holistic summary"
                    f" of EVERYTHING about {wiki_title}. For questions about"
                    " more specific sections, please use the vector_tool."
                ),
            ),
        ),
    ]
# build agent
    function_llm = OpenAI(model="gpt-4")
    agent = OpenAIAgent.from_tools(
        query_engine_tools,
        llm=function_llm,
        verbose=True,
        system_prompt=f"""\
You are a specialized agent designed to answer queries about {wiki_title}.
You must ALWAYS use at least one of the tools provided when answering a question; do NOT rely on prior knowledge.\
""",
    )

    agents[wiki_title] = agent
    query_engines[wiki_title] = vector_index.as_query_engine(
        similarity_top_k=2
    )

构建启用检索功能的OpenAI代理

# define tool for each document agent
all_tools = []
for wiki_title in wiki_titles:
    wiki_summary = (
        f"This content contains Wikipedia articles about {wiki_title}. Use"
        f" this tool if you want to answer any questions about {wiki_title}.\n"
    )
    doc_tool = QueryEngineTool(
        query_engine=agents[wiki_title],
        metadata=ToolMetadata(
            name=f"tool_{wiki_title}",
            description=wiki_summary,
        ),
    )
    all_tools.append(doc_tool)
# define an "object" index and retriever over these tools
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex

obj_index = ObjectIndex.from_objects(
    all_tools,
    index_cls=VectorStoreIndex,
)
from llama_index.agent.openai import OpenAIAgent

top_agent = OpenAIAgent.from_tools(
    tool_retriever=obj_index.as_retriever(similarity_top_k=3),
    system_prompt=""" \
You are an agent designed to answer queries about a set of given cities.
Please always use the tools provided to answer a question. Do not rely on prior knowledge.\

""",
    verbose=True,
)

这种设置允许顶层代理有效地检索和使用适当的文档代理来回答查询。

使用MultiDocumentAgentsPack

另外,您可以使用 MultiDocumentAgentsPack 类来简化设置过程:

from llama_index.core.schema import Document
from llama_index.packs.multi_document_agents.base import MultiDocumentAgentsPack

# Initialize documents, titles, and descriptions
docs = [Document(content="..."), ...]
doc_titles = ["Title1", "Title2", ...]
doc_descriptions = ["Description1", "Description2", ...]

# Create the multi-document agents pack
multi_doc_agents_pack = MultiDocumentAgentsPack(docs, doc_titles, doc_descriptions)

# Access the top-level agent
top_agent = multi_doc_agents_pack.get_modules()["top_agent"]

# Use the top-level agent to answer queries
response = top_agent.query("Your query here")
print(response)

此类处理文档代理和顶层代理的创建,使其更容易管理和使用多层次的代理结构 1(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/agent/multi_document_agents.ipynb)2(https://github.com/run-llama/llama_index/blob/main/llama-index-packs/llama-index-packs-multi-document-agents/llama_index/packs/multi_document_agents/base.py)3(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/agent/multi_document_agents-v1.ipynb)。

  • 要继续与 Dosu 交谈,请提及 @dosu。*
kupeojn6

kupeojn63#

如果我想要更多的层级怎么办?@dosu

vzgqcmou

vzgqcmou4#

要将设置扩展到不使用OpenAI的更多代理级别,您可以遵循以下步骤:

此外,您还可以使用LlamaIndex框架中的 MultiDocumentAgentsPack 类来构建多级代理架构。该类允许您为每个文档设置一个文档代理,具有QA和摘要功能,然后在这些文档代理上设置一个顶级代理。以下是其工作原理的简要概述:

class MultiDocumentAgentsPack(BaseLlamaPack):
    """Multi-document Agents pack.

Given a set of documents, build our multi-document agents architecture.
- setup a document agent over agent doc (capable of QA and summarization)
- setup a top-level agent over doc agents
"""

    def __init__(self, docs: List[Document], doc_titles: List[str], doc_descriptions: List[str], **kwargs: Any) -> None:
        """Init params."""
        self.node_parser = SentenceSplitter()
        self.llm = YourLLM(temperature=0)  # Replace with your LLM
        self.service_context = ServiceContext.from_defaults(llm=self.llm)

        # Build agents dictionary
        self.agents = {}

        # build agent for each document
        for idx, doc in enumerate(docs):
            doc_title = doc_titles[idx]
            doc_description = doc_descriptions[idx]
            nodes = self.node_parser.get_nodes_from_documents([doc])

            # build vector index
            vector_index = VectorStoreIndex(nodes, service_context=self.service_context)

            # build summary index
            summary_index = SummaryIndex(nodes, service_context=self.service_context)

            # define query engines
            vector_query_engine = vector_index.as_query_engine()
            summary_query_engine = summary_index.as_query_engine()

            # define tools
            query_engine_tools = [
                QueryEngineTool(
                    query_engine=vector_query_engine,
                    metadata=ToolMetadata(
                        name="vector_tool",
                        description=f"Useful for questions related to specific aspects of {doc_title}.",
                    ),
                ),
                QueryEngineTool(
                    query_engine=summary_query_engine,
                    metadata=ToolMetadata(
                        name="summary_tool",
                        description=f"Useful for any requests that require a holistic summary of EVERYTHING about {doc_title}.",
                    ),
                ),
            ]

            # build agent
            function_llm = YourLLM()  # Replace with your LLM
            agent = YourAgent.from_tools(
                query_engine_tools,
                llm=function_llm,
                verbose=True,
                system_prompt=f"""\
You are a specialized agent designed to answer queries about {doc_title}.
You must ALWAYS use at least one of the tools provided when answering a question; do NOT rely on prior knowledge.\
""",
            )

            self.agents[doc_title] = agent

        # build top-level, retrieval-enabled Agent
        all_tools = []
        for idx, doc in enumerate(docs):
            doc_title = doc_titles[idx]
            doc_description = doc_descriptions[idx]
            wiki_summary = f"Use this tool if you want to answer any questions about {doc_title}.\nDoc description: {doc_description}\n"
            doc_tool = QueryEngineTool(
                query_engine=self.agents[doc_title],
                metadata=ToolMetadata(
                    name=f"tool_{doc_title}",
                    description=wiki_summary,
                ),
            )
            all_tools.append(doc_tool)

        tool_mapping = SimpleToolNodeMapping.from_objects(all_tools)
        self.obj_index = ObjectIndex.from_objects(
            all_tools,
            tool_mapping,
            VectorStoreIndex,
        )
        self.top_agent = FnRetrieverAgent.from_retriever(
            self.obj_index.as_retriever(similarity_top_k=3),
            system_prompt=""" \
You are an agent designed to answer queries about a set of given cities.
Please always use the tools provided to answer a question. Do not rely on prior knowledge.\
""",
            verbose=True,
        )

    def get_modules(self) -> Dict[str, Any]:
        """Get modules."""
        return {
            "top_agent": self.top_agent,
            "obj_index": self.obj_index,
            "doc_agents": self.agents,
        }

    def run(self, *args: Any, **kwargs: Any) -> Any:
        """Run the pipeline."""
        return self.top_agent.query(*args, **kwargs)

这些示例和 MultiDocumentAgentsPack 类提供了关于如何创建和管理更复杂代理设置的详细指南,包括递归检索、多文档处理、自定义代理创建以及使用较低级别的API进行更高级别的控制 1(https://github.com/run-llama/llama_index/blob/main/docs/docs/module_guides/deploying/agents/modules.md)2(https://github.com/run-llama/llama_index/blob/main/llama-index-packs/llama-index-packs-multi-document-agents/llama_index/packs/multi_document_agents/base.py)3(https://github.com/run-llama/llama_index/blob/main/docs/docs/presentations/materials/2024-06-22-genai-philippines.ipynb)4(https://github.com/run-llama/llama_index/blob/main/docs/docs/presentations/materials/2024-06-19-georgian-genai-bootcamp.ipynb)。

  • 要继续与 Dosu 交谈,请提及 @dosu。*

相关问题