自然语言处理从入门到应用——LangChain:索引(Indexes)-[向量存储器(Vectorstores)]

分类目录:《自然语言处理从入门到应用》总目录


Vectorstores是构建索引的最重要组件之一。本文展示了与VectorStores相关的基本功能。在使用VectorStores时,创建要放入其中的向量是一个关键部分,通常通过嵌入来创建。

csharp 复制代码
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma

with open('../../state_of_the_union.txt') as f:
    state_of_the_union = f.read()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_text(state_of_the_union)

embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_texts(texts, embeddings)

query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query)

日志输出:

复制代码
Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.

输入:

复制代码
print(docs[0].page_content)

输出:

复制代码
In state after state, new laws have been passed, not only to suppress the vote, but to subvert entire elections. 

We cannot let this happen. 

Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you're at it, pass the Disclose Act so Americans can know who is funding our elections. 

Tonight, I'd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer---an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. 

One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. 

And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence.

添加文本

我们可以使用add_texts方法轻松地将文本添加到VectorStore中。它将返回一个文档ID的列表(以防我们需要在下游使用它们)。

csharp 复制代码
docsearch.add_texts(["Ankush went to Princeton"])

输出:

复制代码
['a05e3d0c-ab40-11ed-a853-e65801318981']

输入:

复制代码
query = "Where did Ankush go to college?"
docs = docsearch.similarity_search(query)
docs[0]
Document(page_content='Ankush went to Princeton', lookup_str='', metadata={}, lookup_index=0)

从文档初始化

我们还可以直接从文档初始化一个Vectorstore。当我们在文本分割器上使用该方法直接获取文档时,这非常有用(当原始文档具有相关联的元数据时非常方便)。

复制代码
documents = text_splitter.create_documents([state_of_the_union], metadatas=[{"source": "State of the Union"}])
docsearch = Chroma.from_documents(documents, embeddings)

query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query)

日志输出:

复制代码
Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.

输入:

复制代码
print(docs[0].page_content)

输出:

复制代码
In state after state, new laws have been passed, not only to suppress the vote, but to subvert entire elections. 

We cannot let this happen. 

Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you're at it, pass the Disclose Act so Americans can know who is funding our elections. 

Tonight, I'd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer---an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. 

One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. 

And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence.

参考文献:

1\] LangChain官方网站:https://www.langchain.com/ \[2\] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/ \[3\] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

相关推荐
汽车仪器仪表相关领域4 分钟前
AI赋能智能检测,引领灯光检测新高度——NHD-6109智能全自动远近光检测仪项目实战分享
大数据·人工智能·功能测试·机器学习·汽车·可用性测试·安全性测试
brave and determined6 分钟前
工程设计类学习(DAY4):硬件可靠性测试全攻略:标准到实战
人工智能·嵌入式硬件·测试·硬件设计·可靠性测试·嵌入式设计·可靠性方法
Stuomasi_xiaoxin7 分钟前
ROS2介绍,及ubuntu22.04 安装ROS 2部署使用!
linux·人工智能·深度学习·ubuntu
李泽辉_8 分钟前
深度学习算法学习(五):手动实现梯度计算、反向传播、优化器Adam
深度学习·学习·算法
lovingsoft9 分钟前
AI+敏捷时代,专项测试人员是否还有存在的必要?
人工智能
木头程序员15 分钟前
大模型边缘部署突破:动态推理技术与精度-延迟-能耗帕累托优化
大数据·人工智能·计算机视觉·自然语言处理·智能手机·数据挖掘
DX_水位流量监测17 分钟前
无人机测流之雷达流速仪监测技术分析
大数据·网络·人工智能·数据分析·自动化·无人机
昨日之日200617 分钟前
LTX-2 - 一键生成音视频,创作更轻松 支持50系显卡 ComfyUI工作流 一键整合包
人工智能·音视频·视频
imbackneverdie21 分钟前
Science最新文章:大型语言模型时代的科学生产
人工智能·语言模型·自然语言处理·aigc·ai写作
李泽辉_24 分钟前
深度学习算法学习(一):梯度下降法和最简单的深度学习核心原理代码
深度学习·学习·算法