当前位置: 首页 > news >正文

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

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


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

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chromawith 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的列表(以防我们需要在下游使用它们)。

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/

http://www.lryc.cn/news/135910.html

相关文章:

  • 【C++练习】普通方法+利用this 设置一个矩形类(Rectangle), 包含私有成员长(length)、 宽(width), 定义一下成员函数
  • 电子电路学习笔记之SA1117BH-1.2TR——LDO低压差线性稳压器
  • 【LeetCode-面试经典150题-day7】
  • 00-音视频-概述
  • SOFARPC(笔记)
  • 无线上网连接及配置
  • Webpack减少打包数量和体积(Umi 3.*中)
  • python Crypto 包安装
  • 时序预测 | MATLAB实现SO-CNN-LSTM蛇群算法优化卷积长短期记忆神经网络时间序列预测
  • 前端开发,怎么解决浏览器兼容性问题? - 易智编译EaseEditing
  • 树莓派3B安装64位操作系统
  • Mysql系列 - 第2天:详解mysql数据类型(重点)
  • Linux常用的运维命令
  • 【从零学习python 】50.面向对象编程中的多态应用
  • 实现Token刷新机制
  • FlaUi输入账号密码
  • ModStartBlog v8.0.0 博客归档页面,部分组件升级
  • 使用 PyTorch 进行高效图像分割:第 4 部分
  • 西班牙卡瓦起泡酒的风味搭配
  • Java项目-苍穹外卖-Day05
  • 取模运算符在数组下标的应用
  • Firefox(火狐),使用技巧汇总,问题处理
  • 耐腐蚀高速数控针阀和多功能PID控制器在流量比率控制中的应用
  • C语言:选择+编程(每日一练Day6)
  • 微信小程序教学系列(8)
  • 情人节定制:HTML5 Canvas全屏七夕爱心表白特效
  • 操作系统-笔记-第五章-输入输出管理
  • 感觉自己效率不高吗?学习实现目标的六个关键步骤,让你做任何事都事半功倍!
  • 【高级IO】- 五种 IO 模型 | 多路转接 - select
  • 在Linux搭建GitLab私有仓库配置实现远程访问私有仓库Gitlab ——【内网穿透】