LlamaIndex系列:RAG应用开发中的数据连接器和核心概念

释放双眼,带上耳机,听听看~!
学习LlamaIndex系列中的RAG应用开发过程中的数据连接器和核心概念,包括Document和Node的使用方法和定制化操作。了解如何处理各式数据源并进行文档处理,以及自定义Document的方法。

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

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

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

前言

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

Document

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

LlamaIndex系列:RAG应用开发中的数据连接器和核心概念

  • Document内容

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

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

  首先,我们从llama_index中引入Document,接着申明了一个数组,每一项为文本,最后,我们设置了Document的text属性,返回了一个documents数组。
LlamaIndex系列:RAG应用开发中的数据连接器和核心概念

  • 自定义Document

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

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

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

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

document.metadata = {'filename': 'hello_world_v2.pdf'}

  我们也可以设置文档id

from llama_index import Document
document = Document(text='Hello World')
document.doc_id = "xxxx-yyyy"
  • SimpleDirectoryReader中设置
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。

LlamaIndex系列:RAG应用开发中的数据连接器和核心概念

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

Node

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

from llama_index.schema import TextNode
node = TextNode(text="hello world", id_="1234-5678")
print(node)

LlamaIndex系列:RAG应用开发中的数据连接器和核心概念

  • 从Document中拿到Nodes
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方法得到了文档的所有结点。

[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}nn{content}', metadata_template='{key}: {value}', metadata_seperator='n')...]

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

  • 自定义Node

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

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教程

大语言模型角色扮演:深度学习科学家在Nature发表论文

2023-12-19 17:00:00

AI教程

Llama 2: 开源的LLMs模型及微调聊天模型详解

2023-12-19 17:03:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索