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对象,我们可以对数据文件进行一些业务相关的处理。

参考资料

相关推荐
AI王也4 分钟前
ChatGPT 4o 使用指南 (9月更新)
人工智能·chatgpt·prompt·aigc
望繁信科技6 分钟前
望繁信科技受邀出席ACS2023,为汽车行业数智化护航添翼
人工智能·企业数字化转型·流程挖掘·流程智能·数字北极星
木凳子a10 分钟前
给儿童掏耳朵用哪个好?儿童耳勺最建议买的五个牌子
人工智能·安全·信息可视化·智能家居·健康医疗
秋919 分钟前
教师心理学能力研判:多维度视角下的分析,判断教师心理学知识能力强弱,并提出针对性意见
人工智能·心理学研判·教师心理学研判·心理学知识研判
中科微星19 分钟前
相位型SLM硬件产品面型性能提升
图像处理·人工智能·深度学习
AI2024081422 分钟前
众数信科AI智能体政务服务解决方案——寻知智能笔录系统
人工智能·政务
敲上瘾1 小时前
多态的使用和原理(c++详解)
开发语言·数据结构·c++·单片机·aigc·多态·模拟
生信宝典1 小时前
ROC和AUC也不是评估机器学习性能的金标准
人工智能·qt·机器学习
ShuQiHere1 小时前
【ShuQiHere】 探索计算机视觉的世界:从基础到应用
人工智能·计算机视觉
毕小宝1 小时前
TensorFlow 的基本概念和使用场景
人工智能·python·tensorflow