AI菜鸟向前飞 — LangChain系列之十二 - RAG(下篇):Index和Retriever

AI菜鸟向前飞 --- LangChain系列之十 - RAG(上篇)

AI菜鸟向前飞 --- LangChain系列之十一 - RAG(中篇)

先分享个问题的解法

python 复制代码
# 在使用Chroma实例化过程中,可能会出现如下报错
AttributeError: type object 'hnswlib.Index' has no attribute 'file_handle_count'

当使用代码遇到如上问题时,你可以这么去解决:

pip uninstall hnswlib
pip uninstall chroma-hnswlib
pip install chroma-hnswlib

原因:chroma-hnswlib的一个bug导致,回到正题。

今天主要给大家介绍Indexing(索引),在正式介绍之前,先介绍个重要概念:持久化存储

向量化存储方式

在内存中

上一篇的例子,给大家演示的是非持久化存储

bash 复制代码
vector_store = Chroma(embedding_function=embeddings)

# 因存储在内存中造成每次启动程序时,都要add_documents存储,否则无法查询到
print("看看有值吗:\n", vector_store.get(where={"source": {"$eq": "test.txt"}}))

vector_store.add_documents(txt_docs)

print("再看看有值吗:\n", vector_store.get(where={"source": {"$eq": "test.txt"}}))

结果

rust 复制代码
看看有值吗:
 {'ids': [], 'embeddings': None, 'metadatas': [], 'documents': [], 'uris': None, 'data': None}
再看看有值吗:
 {'ids': ['5558fd95-9935-4070-861a-b9da7bfa8b92'], 'embeddings': None, 'metadatas': [{'source': 'test.txt'}], 'documents': ['南方将迎长达10天密集降雨热,\n苹果在中国1年收400亿苹果税,\n欢迎晚宴上他们都来了,\n湖北一村上百村民莫名有了营业执照, ............], 'uris': None, 'data': None}

持久化存储

bash 复制代码
vector_store_persist = Chroma(embedding_function=embeddings, persist_directory="./db")

# vector_store_persist.add_documents(txt_docs)

print("看看有值吗:\n", vector_store_persist.get(where={"source": {"$eq": "test.txt"}}))

结果

rust 复制代码
看看有值吗:
 {'ids': ['425c92f3-7d61-44a4-b208-964586cc0e55'], 'embeddings': None, 'metadatas': [{'source': 'test.txt'}], 'documents': ['南方将迎长达10天密集降雨热,\n苹果在中国1年收400亿苹果税,\n欢迎晚宴上他们都来了,\n湖北一村上百村民莫名有了营业执照, ............], 'uris': None, 'data': None}

看磁盘里有什么 ![[Pasted image 20240513133207.png]]

问题来了,如果每次繁琐的调用add_documents或者是add_texts的话,遇到同一份文档,岂不是要存多次?LangChain提供了一个特别好用的API,它提供了三种索引维护(清理)模式:)

Indexing:接收的文档参数为Document对象,若对Document对象不熟悉的朋友,可参考

AI菜鸟向前飞 --- LangChain系列之十一 - RAG(中篇)

清理模式 原文档被修改 向量存储是否删除 原文档被删除 向量存储是否删除 是否 支持并发
None(无)
Incremental(增量)
Full(全量)

Document中metadata中有一个默认字段叫"source",index也要用到它(source的含义是把文档块与原文档关联起来),用它与index的文档内容做关联。

为了更易于理解,让我们边看代码边"学习"吧

首先,让我们准备好持久化存储的方式,你猜猜为什么必须用这种呢?为什么不用内存方式的存储?

准备工作

向量化数据库

ini 复制代码
from langchain.indexes import SQLRecordManager, index
vectorstore = Chroma(embedding_function=embeddings, persist_directory="./db")

# 这个是第一次见,它的作用是把持久化的数据库作为记录管理...
# 简单来说,index会需要往这里存数据,可以找到它
record_manager = SQLRecordManager(namespace="Chroma/langchain-index", db_url="sqlite://./db/chroma.sqlite3")

Document数据(注意source)

ini 复制代码
doc1 = Document(page_content="欢迎晚宴上他们都来了", metadata={"source": "dinner.txt"})
doc2 = Document(page_content="三组数据看实体经济发展质效提升", metadata={"source": "economy.txt"})
doc3 = Document(page_content="苹果在中国1年收400亿苹果税", metadata={"source": "apple.txt"})

索引函数的粗糙封装

ini 复制代码
def index_docs(doc: List[Document], record_manager=record_manager, vectorstore=vectorstore, cleanup=None, source_id_key="source", force_update=False):
    return index(doc, record_manager, vectorstore, cleanup=cleanup, source_id_key=source_id_key, force_update=force_update)

(试验)正式开始

None模式

程序

ini 复制代码
# 清除向量存储,这行代码的含义,看完本章后,你就懂了
index([], record_manager, vectorstore, cleanup="full", source_id_key="source")

# line-one
index_docs(doc=[doc1, doc1, doc1 ......(此处省略一万个], cleanup=None)
# line-two
index_docs(doc=[doc1, doc2], cleanup=None)
# line-three
index_docs(doc=[doc1, doc2, doc3], cleanup=None)
# line-four
doc3.page_content = "Apple 发布 M4芯片的iPad Pro"
print("修改doc2之后,再次索引", 
      index_docs([doc1, doc2, doc3], cleanup=None,source_id_key="source"))


print("当前存储的数量", vectorstore._collection.count())
# line-five
index_docs(doc=[], cleanup=None)
# line-six
print("源文档被删除,再次索引后,当前存储的数量", vectorstore._collection.count())

输出结果

arduino 复制代码
# line-one
{'num_added': 1, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}
# line-two
{'num_added': 1, 'num_updated': 0, 'num_skipped': 1, 'num_deleted': 0}
# line-three
{'num_added': 1, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 0}
# line-four
当前存储的数量 3
修改doc2之后,再次索引 {'num_added': 1, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 0}
当前存储的数量 4
# line-five
{'num_added': 0, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}
# line-six
源文档被删除,再次索引后,当前存储的数量 4

重点解析:

  • line-one:无论传入多少个一样的文档,都会索引入向量数据库一次(num_added)
  • line-two:若遇到相同的文件,会跳过(num_skipped);不同的,会添加(num_added)
  • line-four:文档内容(page_content)被修改后,再次索引时会继续添加已修改后的文档内容到向量数据库(num_added),原来的所有数据都不会改变,虽然是跳过(num_skipped)两个,但已经存储的不会被删除或修改,存储的数量变成了4个
  • line-six:若再次索引时,原文档被删除(传入为空),所有已存储的数据无变化

Incremental 增量模式

程序

ini 复制代码
# line-one
index_docs([doc1, doc2], cleanup="incremental")
# line-two
index_docs([doc1, doc2, doc3], cleanup="incremental", source_id_key="source")
# line-three
doc3.page_content = "Apple 发布 M4芯片13英寸平板电脑iPad Pro"
print("修改doc2之后,再次索引", index_docs([doc1, doc2, doc3], cleanup="incremental", source_id_key="source"))

# line-four
print("当前存储的数量", vectorstore._collection.count())
# line-five
index_docs([], cleanup="incremental", source_id_key="source")
# line-six
print("源文档被删除,再次索引后,当前存储的数量", vectorstore._collection.count())

输出结果

arduino 复制代码
# line-one
{'num_added': 2, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}
# line-two
{'num_added': 1, 'num_updated': 2, 'num_skipped': 0, 'num_deleted': 0}
# line-three
修改doc2之后,再次索引 {'num_added': 1, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 1}
# line-four
当前存储的数量 3
# line-five
{'num_added': 0, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}
# line-six
源文档被删除,再次索引后,当前存储的数量 3

重点解析

  • line-two:若遇到相同的文件,会跳过(num_skipped);不同的,会添加(num_added)
  • line-three:文档内容(page_content)被修改后,再次索引时会删除已存储的原文档内容(num_deleted),添加已修改后的文档内容到向量数据库(num_added)
  • line-six:若再次索引时,原文档被删除(传入为空),所有已存储的数据无变化

Full全量模式

程序

ini 复制代码
# line-one
index_docs([doc1, doc2], cleanup="full", source_id_key="source")
# line-two
index_docs([doc1, doc2, doc3], cleanup="full", source_id_key="source")
# line-three
doc3.page_content = "Apple 发布 M4芯片11英寸平板电脑iPad Pro"
print("修改doc2之后,再次索引", index_docs([doc1, doc2, doc3], cleanup="full", source_id_key="source"))

# line-four
print("当前存储的数量", vectorstore._collection.count())
# line-five
index_docs([], cleanup="full", source_id_key="source")
# line-six
print("源文档被删除,再次索引后,当前存储的数量", vectorstore._collection.count())

输出结果

arduino 复制代码
# line-one
{'num_added': 2, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}
# line-two
{'num_added': 1, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 0}
# line-three
修改doc2之后,再次索引 {'num_added': 1, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 1}
# line-four
当前存储的数量 3
# line-fix
{'num_added': 0, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 3}
# line-six
源文档被删除,再次索引后,当前存储的数量 0

重点解析

  • line-three:文档内容(page_content)被修改后,再次索引时会删除已存储的原文档内容(num_deleted),添加已修改后的文档内容到向量数据库(num_added)
  • line-six:若再次索引时,原文档被删除(传入为空),所有已存储的数据都将被删除(num_deleted)

这三种就介绍完了,但大家会发现有一个"num_updated",从前到后都没有遇到过,是因为,要加一个参数 程序

ini 复制代码
index_docs(doc=[doc1, doc2])
index_docs(doc=[doc1, doc2, doc3], force_update=True)

输出结果

arduino 复制代码
{'num_added': 2, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}
{'num_added': 1, 'num_updated': 2, 'num_skipped': 0, 'num_deleted': 0}

这次"num_updated"有值了吧 : )

具体要用哪种模式,根据场景选择不同的,另外还要注意的有:

当内容发生变化时,在索引期间将有一段时间新旧版本都可能返回给用户------这是因为在新内容写入时,旧版本还没被删除之前造成的

需要用好不同的模式:

  • Incremental模式:可以最大限度地减少这段时间,因为它能够在写入时连续进行清理
  • Full模式:在所有的都写入后进行清理

关于Retriever

最后让我们看下 对于RAG中一个特别重要的Retriever,先来讲常用参数:

Top K 前K个结果

程序

ini 复制代码
texts = [
    "南方将迎长达10天密集降雨热",
    "苹果在中国1年收400亿苹果税",
    "欢迎晚宴上他们都来了",
    "湖北一村上百村民莫名有了营业执照",
    "三组数据看全国实体经济发展质效提升",
    "南方即将迎来密集降雨热天气,长达10多天",
    "台湾地震把福州乌塔震歪?假的",
    "在中国1年苹果被收400亿苹果税",
    "上海外滩特警回应因为太帅走红",
    "北京",
    "我在上海东方明珠广场"
]
dbstore = Chroma.from_texts(texts=texts, embedding=DashScopeEmbeddings(model="text-embedding-v2"))
retriver = dbstore.as_retriever(search_kwargs={'k': 3})
res = retriver.invoke("请根据文中的信息内容只回答出关于城市地点的内容")
print(res)

输出结果

css 复制代码
[Document(page_content='北京'), Document(page_content='南方将迎长达10天密集降雨热') Document(page_content='南方即将迎来密集降雨热天气,长达10多天')]

MMR 最大边界相似性

程序

ini 复制代码
retriver = dbstore.as_retriever(search_type="mmr", search_kwargs={'k': 3})
res = retriver.invoke("请根据文中的信息内容只回答出关于城市地点的内容")
print(res)

输出结果

css 复制代码
[Document(page_content='北京'), Document(page_content='南方将迎长达10天密集降雨热') Document(page_content='台湾地震把福州乌塔震歪?假的')]

与Top K比较一下:),你就能看到MMR的妙处了

Similarity score threshold 相似度阈值

程序

ini 复制代码
retriver = dbstore.as_retriever(search_type="similarity_score_threshold", search_kwargs={"score_threshold": 0.7})
res = retriver.invoke("请根据文中的信息内容只回答出北京的内容")

输出结果

css 复制代码
[Document(page_content='北京')]

总结

对RAG的介绍暂时告一段落,涉及到一些高级的Retriever,等后面有空单独开个专题再给大家介绍,实在是太重磅、内容相当多,到时与agent一起介绍。

相关推荐
龙的爹23338 小时前
论文翻译 | LLaMA-Adapter :具有零初始化注意的语言模型的有效微调
人工智能·gpt·语言模型·自然语言处理·nlp·prompt·llama
罗曼蒂克在消亡11 小时前
github项目——gpt-pilot自动创建应用
gpt·github·github项目
学习前端的小z13 小时前
【AIGC】ChatGPT提示词解析:如何打造个人IP、CSDN爆款技术文案与高效教案设计
人工智能·chatgpt·aigc
wgggfiy1 天前
chatgpt学术科研prompt模板有哪些?chatgpt的学术prompt有哪些?学术gpt,学术科研
论文阅读·人工智能·gpt·chatgpt·prompt·aigc
⊙月1 天前
CMU 10423 Generative AI:lec15(Scaling Laws 大规模语言模型的扩展法则)
人工智能·aigc
三桥君1 天前
我为什么决定关闭ChatGPT的记忆功能?
人工智能·ai·自然语言处理·chatgpt·prompt·openai·ai产品经理
Ephemeroptera1 天前
通过python-api使用openai的gpt
人工智能·python·gpt
DC10201 天前
GPT 的工作原理:深入解析
java·开发语言·gpt
Dlimeng1 天前
2024年OpenAI DevDay发布实时 API、提示缓存等新功能
人工智能·深度学习·ai·chatgpt·openai·sam altman
贪玩懒悦1 天前
用langchain+streamlit应用RAG实现个人知识库助手搭建
人工智能·ai·语言模型·langchain·aigc