Node 和 Document 有什么关系?
查看完整题解
参考回答
直接回答
Document 通常保留接入时的一份资料,Node 是后续索引、检索处理的单位。解析器可以把长文档拆成多个 TextNode,让查询命中其中一小段。
讲清原理
由解析器创建的节点通常继承文档元数据,也能通过来源关系追到原文。检索返回节点后,还可以按需读周围内容或上级段落,找回刚好被切开的条件。
例子与易错点
示例用单词计数来固定分块预算,检查能否生成多个节点,且 ref_doc_id 与部门标记都指回原手册。生产时应使用适合实际模型的 tokenizer,并给来源字段留出预算。
from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter
manual = Document(
id_="camera-guide",
text=("A green light means the camera is ready. "
"A flashing light means the battery is low. "
"Press the button once to take a picture."),
metadata={"department": "support"},
)
splitter = SentenceSplitter(chunk_size=15, chunk_overlap=2, tokenizer=str.split)
nodes = splitter.get_nodes_from_documents([manual])
assert len(nodes) > 1 and len({n.node_id for n in nodes}) == len(nodes)
assert all(n.ref_doc_id == "camera-guide" for n in nodes)
assert all(n.metadata["department"] == "support" for n in nodes)
print("each node keeps the source ID and department")
这道题,你掌握得怎么样?
掌握程度由你主动标记,阅读和收藏不会自动改变状态。