在本篇指南中,我们将学习如何利用LlamaIndex构建一个多文档的AI代理。在这个过程中,我们将使用以下架构:
- 在每个文档上建立一个"文档代理":每个文档代理可以在其文档中进行问答或概括。
- 在这一组文档代理上建立一个顶级代理。进行工具检索,然后在一组工具中进行CoT(自下而上的构树)以回答问题。
如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex。
# 安装LlamaIndex
!pip install llama-index-core
!pip install llama-index-agent-openai
!pip install llama-index-readers-file
!pip install llama-index-postprocessor-cohere-rerank
!pip install llama-index-llms-openai
!pip install llama-index-embeddings-openai
!pip install unstructured[html]
//中转API
然后我们需要加载LlamaIndex文档。
# 加载LlamaIndex文档
domain = "docs.llamaindex.ai"
docs_url = "https://docs.llamaindex.ai/en/latest/"
!wget -e robots=off --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains {domain} --no-parent {docs_url}
//中转API
我们接下来构建一个多文档代理,首先为每个文档构建一个文档代理,然后定义顶级的父代理以及一个对象索引。
# 构建代理
agents_dict, extra_info_dict = await build_agents(docs)
//中转API
我们构建一个顶级代理,可以在不同的文档代理中进行协调,以回答任何用户查询。
# 构建顶级代理
top_agent = OpenAIAgent.from_tools(
tool_retriever=custom_obj_retriever,
system_prompt="You are an agent designed to answer queries about the documentation. Please always use the tools provided to answer a question. Do not rely on prior knowledge.",
llm=llm,
verbose=True,
)
//中转API
最后,我们可以运行一些示例查询,范围从单个文档的QA / 摘要到多个文档的QA / 摘要。
# 运行查询
response = top_agent.query("What types of agents are available in LlamaIndex?")
print(response)
//中转API
本文主要向您展示了如何使用LlamaIndex来建立一个多文档代理。在实际使用中,可能会出现的问题包括:
- 在构建索引时可能会遇到性能问题。解决方案是适当地调整批处理大小和线程数,以便在保持索引质量的同时提供合理的效率。
- 在提取文本摘要时可能会发现结果不准确。解决方案是调整摘要的参数,比如长度限制,或者更换合适的摘要模型。
如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!
参考资料:
1516

被折叠的 条评论
为什么被折叠?



