多文档 Agent (V1)¶
在本指南中,您将学习如何为 LlamaIndex 文档设置多文档 Agent。
这是 V0 多文档 Agent 的扩展,增加了以下功能:
- 文档(工具)检索期间的重排序
- Agent 可以用来规划的查询规划工具
我们采用以下架构来实现:
- 为每个文档设置一个“文档 Agent”:每个文档 Agent 可以在其文档内部进行问答/摘要
- 在这组文档 Agent 之上设置一个顶层 Agent。进行工具检索,然后在工具集上执行 CoT(思维链)来回答问题。
如果您在 colab 上打开此 Notebook,您可能需要安装 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]
%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]
输入 [ ]
已复制!
%load_ext autoreload
%autoreload 2
%load_ext autoreload %autoreload 2
输入 [ ]
已复制!
domain = "docs.llamaindex.ai"
docs_url = "https://docs.llamaindex.org.cn/en/stable/"
!wget -e robots=off --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains {domain} --no-parent {docs_url}
domain = "docs.llamaindex.ai" docs_url = "https://docs.llamaindex.org.cn/en/stable/" !wget -e robots=off --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains {domain} --no-parent {docs_url}
输入 [ ]
已复制!
from llama_index.readers.file import UnstructuredReader
reader = UnstructuredReader()
from llama_index.readers.file import UnstructuredReader reader = UnstructuredReader()
输入 [ ]
已复制!
from pathlib import Path
all_files_gen = Path("./docs.llamaindex.ai/").rglob("*")
all_files = [f.resolve() for f in all_files_gen]
from pathlib import Path all_files_gen = Path("./docs.llamaindex.ai/").rglob("*") all_files = [f.resolve() for f in all_files_gen]
输入 [ ]
已复制!
all_html_files = [f for f in all_files if f.suffix.lower() == ".html"]
all_html_files = [f for f in all_files if f.suffix.lower() == ".html"]
输入 [ ]
已复制!
len(all_html_files)
len(all_html_files)
输出 [ ]
1656
输入 [ ]
已复制!
useful_files = [
x
for x in all_html_files
if "understanding" in str(x).split(".")[-2]
or "examples" in str(x).split(".")[-2]
]
print(len(useful_files))
useful_files = [ x for x in all_html_files if "understanding" in str(x).split(".")[-2] or "examples" in str(x).split(".")[-2] ] print(len(useful_files))
680
输入 [ ]
已复制!
from llama_index.core import Document
# TODO: set to higher value if you want more docs to be indexed
doc_limit = 100
docs = []
for idx, f in enumerate(useful_files):
if idx > doc_limit:
break
print(f"Idx {idx}/{len(useful_files)}")
loaded_docs = reader.load_data(file=f, split_documents=True)
loaded_doc = Document(
text="\n\n".join([d.get_content() for d in loaded_docs]),
metadata={"path": str(f)},
)
print(loaded_doc.metadata["path"])
docs.append(loaded_doc)
from llama_index.core import Document # TODO: 如果您想索引更多文档,请将此值设置得更高 doc_limit = 100 docs = [] for idx, f in enumerate(tqdm(docs)): if idx > doc_limit: break print(f"Idx {idx}/{len(useful_files)}") loaded_docs = reader.load_data(file=f, split_documents=True) loaded_doc = Document( text="\n\n".join([d.get_content() for d in loaded_docs]), metadata={"path": str(f)}, ) print(loaded_doc.metadata["path"]) docs.append(loaded_doc)
输入 [ ]
已复制!
print(len(docs))
print(len(docs))
101
定义全局 LLM + Embeddings
输入 [ ]
已复制!
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
import os os.environ["OPENAI_API_KEY"] = "sk-..."
输入 [ ]
已复制!
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core import Settings
llm = OpenAI(model="gpt-4o")
Settings.llm = llm
Settings.embed_model = OpenAIEmbedding(
model="text-embedding-3-small", embed_batch_size=256
)
from llama_index.llms.openai import OpenAI from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.core import Settings llm = OpenAI(model="gpt-4o") Settings.llm = llm Settings.embed_model = OpenAIEmbedding( model="text-embedding-3-small", embed_batch_size=256 )
构建多文档 Agent¶
在本节中,我们将向您展示如何构建多文档 Agent。我们首先为每个文档构建一个文档 Agent,然后使用对象索引定义顶层父 Agent。
为每个文档构建文档 Agent¶
在本节中,我们为每个文档定义“文档 Agent”。
我们为每个文档定义了向量索引(用于语义搜索)和摘要索引(用于摘要)。然后将这两个查询引擎转换为工具,并传递给 OpenAI 函数调用 Agent。
这个文档 Agent 可以动态选择在给定文档内执行语义搜索或摘要。
我们为每个城市创建了一个单独的文档 Agent。
输入 [ ]
已复制!
from llama_index.core.agent.workflow import FunctionAgent, ReActAgent
from llama_index.core import (
load_index_from_storage,
StorageContext,
VectorStoreIndex,
)
from llama_index.core import SummaryIndex
from llama_index.core.tools import QueryEngineTool
from llama_index.core.node_parser import SentenceSplitter
import os
from tqdm.notebook import tqdm
import pickle
async def build_agent_per_doc(nodes, file_base):
vi_out_path = f"./data/llamaindex_docs/{file_base}"
summary_out_path = f"./data/llamaindex_docs/{file_base}_summary.pkl"
if not os.path.exists(vi_out_path):
Path("./data/llamaindex_docs/").mkdir(parents=True, exist_ok=True)
# build vector index
vector_index = VectorStoreIndex(nodes)
vector_index.storage_context.persist(persist_dir=vi_out_path)
else:
vector_index = load_index_from_storage(
StorageContext.from_defaults(persist_dir=vi_out_path),
)
# build summary index
summary_index = SummaryIndex(nodes)
# define query engines
vector_query_engine = vector_index.as_query_engine(llm=llm)
summary_query_engine = summary_index.as_query_engine(
response_mode="tree_summarize", llm=llm
)
# extract a summary
if not os.path.exists(summary_out_path):
Path(summary_out_path).parent.mkdir(parents=True, exist_ok=True)
summary = str(
await summary_query_engine.aquery(
"Extract a concise 1-2 line summary of this document"
)
)
pickle.dump(summary, open(summary_out_path, "wb"))
else:
summary = pickle.load(open(summary_out_path, "rb"))
# define tools
query_engine_tools = [
QueryEngineTool.from_defaults(
query_engine=vector_query_engine,
name=f"vector_tool_{file_base}",
description=f"Useful for questions related to specific facts",
),
QueryEngineTool.from_defaults(
query_engine=summary_query_engine,
name=f"summary_tool_{file_base}",
description=f"Useful for summarization questions",
),
]
# build agent
function_llm = OpenAI(model="gpt-4")
agent = FunctionAgent(
tools=query_engine_tools,
llm=function_llm,
system_prompt=f"""\
You are a specialized agent designed to answer queries about the `{file_base}.html` part of the LlamaIndex docs.
You must ALWAYS use at least one of the tools provided when answering a question; do NOT rely on prior knowledge.\
""",
)
return agent, summary
async def build_agents(docs):
node_parser = SentenceSplitter()
# Build agents dictionary
agents_dict = {}
extra_info_dict = {}
# # this is for the baseline
# all_nodes = []
for idx, doc in enumerate(tqdm(docs)):
nodes = node_parser.get_nodes_from_documents([doc])
# all_nodes.extend(nodes)
# ID will be base + parent
file_path = Path(doc.metadata["path"])
file_base = str(file_path.parent.stem) + "_" + str(file_path.stem)
agent, summary = await build_agent_per_doc(nodes, file_base)
agents_dict[file_base] = agent
extra_info_dict[file_base] = {"summary": summary, "nodes": nodes}
return agents_dict, extra_info_dict
from llama_index.core.agent.workflow import FunctionAgent, ReActAgent from llama_index.core import ( load_index_from_storage, StorageContext, VectorStoreIndex, ) from llama_index.core import SummaryIndex from llama_index.core.tools import QueryEngineTool from llama_index.core.node_parser import SentenceSplitter import os from tqdm.notebook import tqdm import pickle async def build_agent_per_doc(nodes, file_base): vi_out_path = f"./data/llamaindex_docs/{file_base}" summary_out_path = f"./data/llamaindex_docs/{file_base}_summary.pkl" if not os.path.exists(vi_out_path): Path("./data/llamaindex_docs/").mkdir(parents=True, exist_ok=True) # 构建向量索引 vector_index = VectorStoreIndex(nodes) vector_index.storage_context.persist(persist_dir=vi_out_path) else: vector_index = load_index_from_storage( StorageContext.from_defaults(persist_dir=vi_out_path), ) # 构建摘要索引 summary_index = SummaryIndex(nodes) # 定义查询引擎 vector_query_engine = vector_index.as_query_engine(llm=llm) summary_query_engine = summary_index.as_query_engine( response_mode="tree_summarize", llm=llm ) # 提取摘要 if not os.path.exists(summary_out_path): Path(summary_out_path).parent.mkdir(parents=True, exist_ok=True) summary = str( await summary_query_engine.aquery( "Extract a concise 1-2 line summary of this document" ) ) pickle.dump(summary, open(summary_out_path, "wb")) else: summary = pickle.load(open(summary_out_path, "rb")) # 定义工具 query_engine_tools = [ QueryEngineTool.from_defaults( query_engine=vector_query_engine, name=f"vector_tool_{file_base}", description=f"Useful for questions related to specific facts", ), QueryEngineTool.from_defaults( query_engine=summary_query_engine, name=f"summary_tool_{file_base}", description=f"Useful for summarization questions", ), ] # 构建 agent function_llm = OpenAI(model="gpt-4") agent = FunctionAgent( tools=query_engine_tools, llm=function_llm, system_prompt=f"""\ 您是专门设计用于回答关于 LlamaIndex 文档 `{file_base}.html` 部分的查询的 Agent。回答问题时,您必须总是使用至少一个提供的工具;不要依赖先前的知识。\ """, ) return agent, summary async def build_agents(docs): node_parser = SentenceSplitter() # 构建 agent 字典 agents_dict = {} extra_info_dict = {} # # 这是用于基线 # all_nodes = [] for idx, doc in enumerate(tqdm(docs)): nodes = node_parser.get_nodes_from_documents([doc]) # all_nodes.extend(nodes) # ID 将是 base + parent file_path = Path(doc.metadata["path"]) file_base = str(file_path.parent.stem) + "_" + str(file_path.stem) agent, summary = await build_agent_per_doc(nodes, file_base) agents_dict[file_base] = agent extra_info_dict[file_base] = {"summary": summary, "nodes": nodes} return agents_dict, extra_info_dict
输入 [ ]
已复制!
agents_dict, extra_info_dict = await build_agents(docs)
agents_dict, extra_info_dict = await build_agents(docs)
构建启用检索功能的 OpenAI Agent¶
我们构建了一个顶层 Agent,它可以协调不同的文档 Agent 来回答用户的任何查询。
这个 Agent 将使用工具检索器来检索与查询最相关的工具。
V0 的改进:与 V0 的“基础”版本相比,我们做了以下改进。
- 增加重排序:我们使用 Cohere 重排序器来更好地过滤候选文档集。
- 增加查询规划工具:我们增加了一个明确的查询规划工具,该工具根据检索到的工具集动态创建。
输入 [ ]
已复制!
from typing import Callable
from llama_index.core.tools import FunctionTool
def get_agent_tool_callable(agent: FunctionAgent) -> Callable:
async def query_agent(query: str) -> str:
response = await agent.run(query)
return str(response)
return query_agent
# define tool for each document agent
all_tools = []
for file_base, agent in agents_dict.items():
summary = extra_info_dict[file_base]["summary"]
async_fn = get_agent_tool_callable(agent)
doc_tool = FunctionTool.from_defaults(
async_fn,
name=f"tool_{file_base}",
description=summary,
)
all_tools.append(doc_tool)
from typing import Callable from llama_index.core.tools import FunctionTool def get_agent_tool_callable(agent: FunctionAgent) -> Callable: async def query_agent(query: str) -> str: response = await agent.run(query) return str(response) return query_agent # 为每个文档 Agent 定义工具 all_tools = [] for file_base, agent in agents_dict.items(): summary = extra_info_dict[file_base]["summary"] async_fn = get_agent_tool_callable(agent) doc_tool = FunctionTool.from_defaults( async_fn, name=f"tool_{file_base}", description=summary, ) all_tools.append(doc_tool)
输入 [ ]
已复制!
print(all_tools[0].metadata)
print(all_tools[0].metadata)
ToolMetadata(description='The document provides a series of tutorials on building agentic LLM applications using LlamaIndex, covering key steps such as building RAG pipelines, agents, and workflows, along with techniques for data ingestion, indexing, querying, and application evaluation.', name='tool_understanding_index', fn_schema=<class 'llama_index.core.tools.utils.tool_understanding_index'>, return_direct=False)
输入 [ ]
已复制!
# define an "object" index and retriever over these tools
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import (
ObjectIndex,
ObjectRetriever,
)
from llama_index.postprocessor.cohere_rerank import CohereRerank
from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.schema import QueryBundle
from llama_index.llms.openai import OpenAI
llm = OpenAI(model_name="gpt-4o")
obj_index = ObjectIndex.from_objects(
all_tools,
index_cls=VectorStoreIndex,
)
vector_node_retriever = obj_index.as_node_retriever(
similarity_top_k=10,
)
# define a custom object retriever that adds in a query planning tool
class CustomObjectRetriever(ObjectRetriever):
def __init__(
self,
retriever,
object_node_mapping,
node_postprocessors=None,
llm=None,
):
self._retriever = retriever
self._object_node_mapping = object_node_mapping
self._llm = llm or OpenAI("gpt-4o")
self._node_postprocessors = node_postprocessors or []
def retrieve(self, query_bundle):
if isinstance(query_bundle, str):
query_bundle = QueryBundle(query_str=query_bundle)
nodes = self._retriever.retrieve(query_bundle)
for processor in self._node_postprocessors:
nodes = processor.postprocess_nodes(
nodes, query_bundle=query_bundle
)
tools = [self._object_node_mapping.from_node(n.node) for n in nodes]
sub_agent = FunctionAgent(
name="compare_tool",
description=f"""\
Useful for any queries that involve comparing multiple documents. ALWAYS use this tool for comparison queries - make sure to call this \
tool with the original query. Do NOT use the other tools for any queries involving multiple documents.
""",
tools=tools,
llm=self._llm,
system_prompt="""You are an expert at comparing documents. Given a query, use the tools provided to compare the documents and return a summary of the results.""",
)
async def query_sub_agent(query: str) -> str:
response = await sub_agent.run(query)
return str(response)
sub_question_tool = FunctionTool.from_defaults(
query_sub_agent,
name=sub_agent.name,
description=sub_agent.description,
)
return tools + [sub_question_tool]
# 在这些工具上定义一个“对象”索引和检索器 from llama_index.core import VectorStoreIndex from llama_index.core.objects import ( ObjectIndex, ObjectRetriever, ) from llama_index.postprocessor.cohere_rerank import CohereRerank from llama_index.core.query_engine import SubQuestionQueryEngine from llama_index.core.schema import QueryBundle from llama_index.llms.openai import OpenAI llm = OpenAI(model_name="gpt-4o") obj_index = ObjectIndex.from_objects( all_tools, index_cls=VectorStoreIndex, ) vector_node_retriever = obj_index.as_node_retriever( similarity_top_k=10, ) # 定义一个添加查询规划工具的自定义对象检索器 class CustomObjectRetriever(ObjectRetriever): def __init__( self, retriever, object_node_mapping, node_postprocessors=None, llm=None, ): self._retriever = retriever self._object_node_mapping = object_node_mapping self._llm = llm or OpenAI("gpt-4o") self._node_postprocessors = node_postprocessors or [] def retrieve(self, query_bundle): if isinstance(query_bundle, str): query_bundle = QueryBundle(query_str=query_bundle) nodes = self._retriever.retrieve(query_bundle) for processor in self._node_postprocessors: nodes = processor.postprocess_nodes( nodes, query_bundle=query_bundle ) tools = [self._object_node_mapping.from_node(n.node) for n in nodes] sub_agent = FunctionAgent( name="compare_tool", description=f"""\ 对于涉及比较多个文档的任何查询很有用。进行比较查询时务必始终使用此工具 - 确保使用原始查询调用此工具。不要对涉及多个文档的任何查询使用其他工具。\ """, tools=tools, llm=self._llm, system_prompt="""您是比较文档的专家。给定查询,使用提供的工具比较文档并返回结果摘要。""", ) async def query_sub_agent(query: str) -> str: response = await sub_agent.run(query) return str(response) sub_question_tool = FunctionTool.from_defaults( query_sub_agent, name=sub_agent.name, description=sub_agent.description, ) return tools + [sub_question_tool]
输入 [ ]
已复制!
# wrap it with ObjectRetriever to return objects
custom_obj_retriever = CustomObjectRetriever(
vector_node_retriever,
obj_index.object_node_mapping,
node_postprocessors=[CohereRerank(top_n=5, model="rerank-v3.5")],
llm=llm,
)
# 使用 ObjectRetriever 包装它以返回对象 custom_obj_retriever = CustomObjectRetriever( vector_node_retriever, obj_index.object_node_mapping, node_postprocessors=[CohereRerank(top_n=5, model="rerank-v3.5")], llm=llm, )
输入 [ ]
已复制!
tmps = custom_obj_retriever.retrieve("hello")
# should be 5 + 1 -- 5 from reranker, 1 from subquestion
print(len(tmps))
tmps = custom_obj_retriever.retrieve("hello") # 应该是 5 + 1 -- 5 来自重排序器,1 来自子问题 print(len(tmps))
6
输入 [ ]
已复制!
from llama_index.core.agent.workflow import ReActAgent, FunctionAgent
top_agent = FunctionAgent(
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,
)
# top_agent = ReActAgent(
# 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,
# )
from llama_index.core.agent.workflow import ReActAgent, FunctionAgent top_agent = FunctionAgent( tool_retriever=custom_obj_retriever, system_prompt=""" \ 您是专门设计用于回答关于文档查询的 Agent。请务必始终使用提供的工具来回答问题。不要依赖先前的知识。\ """, llm=llm, ) # top_agent = ReActAgent( # tool_retriever=custom_obj_retriever, # system_prompt=""" \ # 您是专门设计用于回答关于文档查询的 Agent。 # 请务必始终使用提供的工具来回答问题。不要依赖先前的知识。\ # """, # llm=llm, # )
输入 [ ]
已复制!
all_nodes = [
n for extra_info in extra_info_dict.values() for n in extra_info["nodes"]
]
all_nodes = [ n for extra_info in extra_info_dict.values() for n in extra_info["nodes"] ]
输入 [ ]
已复制!
base_index = VectorStoreIndex(all_nodes)
base_query_engine = base_index.as_query_engine(similarity_top_k=4)
base_index = VectorStoreIndex(all_nodes) base_query_engine = base_index.as_query_engine(similarity_top_k=4)
运行示例查询¶
让我们运行一些示例查询,范围从单个文档的问答/摘要到多个文档的问答/摘要。
输入 [ ]
已复制!
from llama_index.core.agent.workflow import (
AgentStream,
ToolCall,
ToolCallResult,
)
handler = top_agent.run(
"What can you build with LlamaIndex?",
)
async for ev in handler.stream_events():
if isinstance(ev, ToolCallResult):
print(
f"\nCalling tool {ev.tool_name} with args {ev.tool_kwargs}\n Got response: {str(ev.tool_output)[:200]}"
)
elif isinstance(ev, ToolCall):
print(f"\nTool call: {ev.tool_name} with args {ev.tool_kwargs}")
# Print the stream of the agent
# elif isinstance(ev, AgentStream):
# print(ev.delta, end="", flush=True)
response = await handler
from llama_index.core.agent.workflow import ( AgentStream, ToolCall, ToolCallResult, ) handler = top_agent.run( "What can you build with LlamaIndex?", ) async for ev in handler.stream_events(): if isinstance(ev, ToolCallResult): print( f"\n调用工具 {ev.tool_name},参数 {ev.tool_kwargs}\n 收到响应:{str(ev.tool_output)[:200]}" ) elif isinstance(ev, ToolCall): print(f"\n工具调用:{ev.tool_name},参数 {ev.tool_kwargs}") # 打印 Agent 的流 # elif isinstance(ev, AgentStream): # print(ev.delta, end="", flush=True) response = await handler
Tool call: tool_SimpleIndexDemoLlama2_index with args {'query': 'What can you build with LlamaIndex?'} Tool call: tool_apps_index with args {'query': 'What can you build with LlamaIndex?'} Tool call: tool_putting_it_all_together_index with args {'query': 'What can you build with LlamaIndex?'} Tool call: tool_llamacloud_index with args {'query': 'What can you build with LlamaIndex?'} Calling tool tool_SimpleIndexDemoLlama2_index with args {'query': 'What can you build with LlamaIndex?'} Got response: With LlamaIndex, you can build a VectorStoreIndex. This involves setting up the necessary environment, loading documents into the index, and then querying the index for information. You need to instal Tool call: tool_using_llms_index with args {'query': 'What can you build with LlamaIndex?'} Calling tool tool_llamacloud_index with args {'query': 'What can you build with LlamaIndex?'} Got response: With LlamaIndex, you can build a system that connects to your data stores, automatically indexes them, and then queries the data. This is done by integrating LlamaCloud into your project. The system a Calling tool tool_apps_index with args {'query': 'What can you build with LlamaIndex?'} Got response: With LlamaIndex, you can build a full-stack web application. You can integrate it into a backend server like Flask, package it into a Docker container, or use it directly in a framework such as Stream Calling tool tool_putting_it_all_together_index with args {'query': 'What can you build with LlamaIndex?'} Got response: With LlamaIndex, you can build a variety of applications and tools. This includes: 1. Chatbots: You can use LlamaIndex to create interactive chatbots. 2. Agents: LlamaIndex can be used to build intel Calling tool tool_using_llms_index with args {'query': 'What can you build with LlamaIndex?'} Got response: With LlamaIndex, you can build a variety of applications by leveraging the various Language Model (LLM) integrations it supports. These include OpenAI, Anthropic, Mistral, DeepSeek, Hugging Face, and
输入 [ ]
已复制!
# print the final response string
print(str(response))
# 打印最终的响应字符串 print(str(response))
With LlamaIndex, you can build various applications and tools, including: 1. **VectorStoreIndex**: Set up and query a VectorStoreIndex by loading documents and configuring the environment as per the documentation. 2. **Full-Stack Web Applications**: Integrate LlamaIndex into backend servers like Flask, Docker containers, or frameworks like Streamlit. Resources include guides for TypeScript+React, Delphic starter template, and Flask, Streamlit, and Docker integration examples. 3. **Chatbots, Agents, and Unified Query Framework**: Create interactive chatbots, intelligent agents, and a unified query framework for handling different query types. LlamaIndex also supports property graphs and full-stack web applications. 4. **Data Management with LlamaCloud**: Build systems that connect to data stores, automatically index data, and efficiently query it by integrating LlamaCloud into your project. 5. **LLM Integrations**: Utilize various Language Model (LLM) integrations such as OpenAI, Anthropic, Mistral, DeepSeek, and Hugging Face. LlamaIndex provides a unified interface to access different LLMs, enabling you to select models based on their strengths and price points. You can use multi-modal LLMs for chat messages with text, images, and audio inputs, and even call tools and functions directly through API calls. These capabilities make LlamaIndex a versatile tool for building a wide range of applications and systems.
输入 [ ]
已复制!
# access the tool calls
# print(response.tool_calls)
# 访问工具调用 # print(response.tool_calls)
输入 [ ]
已复制!
# baseline
response = base_query_engine.query(
"What can you build with LlamaIndex?",
)
print(str(response))
# 基线 response = base_query_engine.query( "What can you build with LlamaIndex?", ) print(str(response))
With LlamaIndex, you can build a variety of applications and systems, including a full-stack web application, a chatbot, and a unified query framework over multiple indexes. You can also perform semantic searches, summarization queries, and queries over structured data like SQL or Pandas DataFrames. Additionally, LlamaIndex supports routing over heterogeneous data sources and compare/contrast queries. It provides tools and templates to help you integrate these capabilities into production-ready applications.
输入 [ ]
已复制!
response = await top_agent.run("Compare workflows to query engines")
print(str(response))
response = await top_agent.run("比较工作流程与查询引擎") print(str(response))
Workflows and query engines serve different purposes in an application context: 1. Workflows: - Workflows are designed to manage the execution flow of an application by dividing it into sections triggered by events. - They are event-driven and step-based, allowing for the management of application complexity by breaking it into smaller, more manageable pieces. - Workflows focus on controlling the flow of application execution through steps and events. 2. Query Engines: - Query engines are tools used to process queries against a database or data source to retrieve specific information. - They are primarily used for querying and retrieving data from databases. - Query engines are focused on the retrieval, postprocessing, and response synthesis stages of querying. In summary, workflows are more about controlling the flow of application execution, while query engines are specifically designed for querying and retrieving data from databases.
输入 [ ]
已复制!
response = await top_agent.run(
"Can you compare the compact and tree_summarize response synthesizer response modes at a very high-level?"
)
print(str(response))
response = await top_agent.run( "您能否从非常高的层面比较一下 compact 和 tree_summarize 响应合成器响应模式?" ) print(str(response))
The compact response synthesizer mode aims to produce concise and condensed responses, focusing on delivering the most relevant information in a brief format. On the other hand, the tree_summarize response synthesizer mode is designed to create structured and summarized responses, organizing information in a comprehensive manner. In summary, the compact mode provides brief and straightforward responses, while the tree_summarize mode offers more detailed and organized output for a comprehensive summary.