主题
概念
Document 与 Node 对象是 LlamaIndex 内部的核心抽象。
Document 是围绕任意数据源的通用容器,比如一个 PDF、一次 API 输出,或从数据库取回的数据。它们既可以手工构造,也可以由数据加载器自动创建。默认情况下,Document 存放文本以及其他若干属性,其中一些列在下面。
metadata—— 可以附在文本上的一组标注字典。relationships—— 记录与其他 Documents/Nodes 之间关系的字典。
注:我们已提供 beta 能力让 Document 存储图像,并正在持续改进其多模态能力。
Node 表示源 Document 的一个“块”,可以是文本块、图像或其他。与 Document 类似,节点也包含元数据以及与其他节点的关联信息。
在 LlamaIndex 里 Node 是一等公民。你可以选择直接定义 Node 及其全部属性,也可以选择用我们的 NodeParser 类把源 Document “解析”成 Node。默认情况下,由某个 Document 派生出的每个 Node 都会继承该 Document 的元数据(例如 Document 里的 “file_name” 字段会传播到每个 Node)。
使用方式
下面几个小片段演示如何开始使用 Document 与 Node。
Documents
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)Nodes
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 的使用
想深入了解如何使用 Documents/Nodes,请看我们的详解指南。