概念#
Document 和 Node 对象是 LlamaIndex 中的核心抽象。
一个 Document 是围绕任何数据源的通用容器,例如 PDF、API 输出或从数据库检索的数据。它们可以手动构造,也可以通过我们的数据加载器自动创建。默认情况下,Document 存储文本以及一些其他属性。其中一些属性如下所示。
metadata
- 一个字典,包含可附加到文本的注解。
relationships
- 一个字典,包含与其他 Document/Node 的关系。- 注意:我们对 Document 存储图像提供了 Beta 支持,并正在积极努力改进其多模态能力。
一个 Node 表示源 Document 的一个“块”,可以是文本块、图像或其他内容。与 Document 类似,它们包含元数据以及与其他节点的关联信息。
Node 在 LlamaIndex 中是一等公民。您可以选择直接定义 Node 及其所有属性。您也可以选择通过我们的 NodeParser
类将源 Document“解析”成 Node。默认情况下,从 Document 派生的每个 Node 都将继承该 Document 的相同元数据(例如,Document 中的“file_name”字段会传播到每个 Node)。
使用模式#
以下是一些简单的代码片段,可帮助您开始使用 Document 和 Node。
文档#
节点#
from llama_index.core import Document, VectorStoreIndex
text_list = [text1, text2, ...]
documents = [Document(text=t) for t in text_list]
# build index
index = VectorStoreIndex.from_documents(documents)
文档/节点使用#
from llama_index.core.node_parser import SentenceSplitter
# load documents
...
# parse nodes
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)
# build index
index = VectorStoreIndex(nodes)
请参阅我们的深度指南,了解有关如何使用 Document/Node 的更多详细信息。
返回顶部