LlamaIndex 四 Documents与Nodes

欢迎来到我的LlamaIndex系列,如果您也和我一样,在搭建RAG应用时,了解到了LlamaIndex, 那就请一起来学习它的各个功能模块和demo实例。

LlamaIndex 一 简单文档查询 - 掘金 (juejin.cn)

LlamIndex二 RAG应用开发 - 掘金 (juejin.cn)

LlamaIndex三 配置 - 掘金 (juejin.cn)

LlamaIndex 四 数据连接器 - 掘金 (juejin.cn)

LlamaIndex在开发RAG应用过程中,一路电光带火石,上篇应对不同格式数据的各种数据连接器,仿佛十八般兵器,让我们应接不暇。本篇,我们就继连接器之后,来到LlamaIndex处理各式数据的核心概念,Document和Nodes,它是索引前的重要步骤。

前言

RAG应用开发,就像摆酒。要处理各式数据,犹如乾隆老爷子当年摆下的千叟宴。数据连接器把吃席的请进来了,接下来怎么张罗呢?在LlamaIndex中,提供了DocumentNode两个数据抽象概念。

Document

Document是各式数据源的容器:数据连接器把数据加载后,得到的是一个抽象的Document对象。不管是PDF,还是API响应,或是数据库等,皆是由一个Document对象来托管。

  • Document内容

Document包含文本数据和在头部包含一些文件的属性,如元数据、关系数据。让我们来看个例子。

ini 复制代码
from llama_index import Document
text_list = ["hello", "world"]
documents = [Document(text=t) for t in text_list]

首先,我们从llama_index中引入Document,接着申明了一个数组,每一项为文本,最后,我们设置了Document的text属性,返回了一个documents数组。

  • 自定义Document

上面的例子我们通过Document的text定制了内容,接下来我们来定义元数据。

javascript 复制代码
from llama_index import Document
document = Document(
    text='Hello World',
    metadata={
        'filename': 'hello_world.pdf',
        'category': 'science'
    }
)

在设置内容为text的同时,我们还在创建这个文档时指定元数据中文件名为hello_world.pdf,分类为science

我们也可以修改document对象的meta属性,这里做了重命名。

ini 复制代码
document.metadata = {'filename': 'hello_world_v2.pdf'}

我们也可以设置文档id

ini 复制代码
from llama_index import Document
document = Document(text='Hello World')
document.doc_id = "xxxx-yyyy"
  • SimpleDirectoryReader中设置
ini 复制代码
from llama_index import SimpleDirectoryReader
filenama_hook = lambda filename: {'file_name': filename}
documents = SimpleDirectoryReader('./data', file_metadata=filenama_hook).load_data()

filename_hook 返回的是一个lambda 的匿名函数(<function <lambda> at 0x000001F2829AE160>)。该函数接受一个filename的参数,并返回一个Dic。其中包含一个键值对:'filename':filename。

看上图打印结果,SimpleDirectoryReader的file_metadata参数,设置了在目录中每次加载文件时的回调。

Node

Document作为数据窗口,对数据分割解析,Node就是相应的抽象。Node是LlamaIndex中的一等公民,也包含了和Document一样的数据和属性。Document由Nodes构成。

python 复制代码
from llama_index.schema import TextNode
node = TextNode(text="hello world", id_="1234-5678")
print(node)
  • 从Document中拿到Nodes
ini 复制代码
from llama_index import Document
from llama_index.node_parser import SimpleNodeParser

text_list = ["hello", "world"]
documents = [Document(text=t) for t in text_list]

parser = SimpleNodeParser.from_defaults()
nodes = parser.get_nodes_from_documents(documents)

我们从llama_index的node_parser模块中引入了SimpleNodeParser, 接下来和之前的例子一样,循环字符串数组生成了两个Document,最后调用parser的get_nodes_from_document方法得到了文档的所有结点。

php 复制代码
[TextNode(id_='9d03ae8a-5b2d-4cc4-9c72-b25891050224', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='c6896cee-18b0-45e2-927f-bc9b497dfe21', node_type=<ObjectType.DOCUMENT: '4'>, metadata={}, hash='7debe8d278fe6c55c45f979269ab268102d75f8d48644d244cd0050dae0846ac'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='16da9220-0f7e-4713-b923-1a426ea9064e', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='12d52a924ac6bc76ec4101c5d1f55bb5b5365d5f4c524a21d6175a4b049a1962')}, hash='7debe8d278fe6c55c45f979269ab268102d75f8d48644d244cd0050dae0846ac', text='hello', start_char_idx=0, end_char_idx=5, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n')...]

大家可以通过打印看到Node和Document类似,有text和metadata等属性。

  • 自定义Node

我们还可以像前端node结点一样来拼装nodes 到Document

ini 复制代码
from llama_index.schema import TextNode, NodeRelationship, RelatedNodeInfo

hello_node = TextNode(text="Hello", id_="1111-1111")
world_node = TextNode(text="World", id_="2222-2222")

hello_node.relationships[NodeRelationship.NEXT] = RelatedNodeInfo(node_id=world_node.node_id, metadata={"created_by": "VerySmallWoods"})
world_node.relationships[NodeRelationship.PREVIOUS] = RelatedNodeInfo(node_id=hello_node.node_id)
nodes = [hello_node, world_node]

有这么细的手工活,我们可以慢慢组装结点了。这对之后的一些高级活是很重要的。

总结

  • LlamaIndex这只"八爪鱼"在连接完各式各样的数据后,使用DocumentNode的抽象概念,进一步处理数据。
  • 通过LlamaInex提供的Document和Node对象,我们可以对数据文件进行一些业务相关的处理。

参考资料

相关推荐
半吊子全栈工匠21 小时前
如何接手一个数据团队?
大数据·人工智能
后端研发Marion21 小时前
【JoyAgent-JDGenie 全栈多智能体系统技术文档】
人工智能·大模型·智能体·langflow·joyagent
多则惑少则明21 小时前
AI测试、大模型测试(一)
人工智能·ai测试·大模型测试
灰灰勇闯IT21 小时前
飞桨平台实战:从零训练中文文本分类模型,附完整开发流程
人工智能·分类·paddlepaddle
新智元21 小时前
GPT-5.2 提前泄露?今夜,OpenAI 要拿 Gemini 3 祭天!
人工智能·openai
catchadmin21 小时前
用 Laravel 官方 AI 工具提升开发效率 效率提示数倍
人工智能·php·laravel
李小星同志21 小时前
DPO,PPO,GRPO的学习
人工智能·深度学习·学习
维构lbs智能定位21 小时前
室内定位无线技术的分类和原理全解析(一)
人工智能·分类·数据挖掘·室内定位无线技术
范男21 小时前
Qwen3-VL + LLama-Factory进行针对Grounding任务LoRA微调
人工智能·深度学习·计算机视觉·transformer·llama
用户3778330434921 小时前
( 教学 )Agent 构建 Prompt(提示词)4. 提示词模板 (初级到高级的应用提示词)
人工智能·langchain