Cohere reranker 一致的排序器

这本notebook展示了如何在检索器中使用 Cohere 的重排端点。这是在 ContextualCompressionRetriever 的想法基础上构建的。

%pip install --upgrade --quiet  cohere
%pip install --upgrade --quiet  faiss# OR  (depending on Python version)%pip install --upgrade --quiet  faiss-cpu
# get a new token: https://dashboard.cohere.ai/import getpass
import osos.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
# get a new token: https://dashboard.cohere.ai/import getpass
import osos.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")

建立基础矢量存储检索器

让我们首先初始化一个简单的向量存储检索器,并存储 2023 年(分块)。我们可以设置检索器以检索大量(20)的文档。

from langchain_community.document_loaders import TextLoader
from langchain_community.embeddings import CohereEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitterdocuments = TextLoader("../../how_to/state_of_the_union.txt").load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
texts = text_splitter.split_documents(documents)
retriever = FAISS.from_documents(texts, CohereEmbeddings()).as_retriever(search_kwargs={"k": 20}
)query = "What did the president say about Ketanji Brown Jackson"
docs = retriever.invoke(query)
pretty_print_docs(docs)
API 参考:TextLoader | CohereEmbeddings | FAISS | RecursiveCharacterTextSplitter
Document 1: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.
----------------------------------------------------------------------------------------------------
Document 2: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.
----------------------------------------------------------------------------------------------------
Document 3:As I said last year, especially to our younger transgender Americans, I will always have your back as your President, so you can be yourself and reach your God-given potential. While it often appears that we never agree, that isn’t true. I signed 80 bipartisan bills into law last year. From preventing government shutdowns to protecting Asian-Americans from still-too-common hate crimes to reforming military justice.
----------------------------------------------------------------------------------------------------

使用 CohereRerank 进行重新排名

现在让我们用一个 ContextualCompressionRetriever 包装我们的基础检索器。我们会添加一个 CohereRerank ,使用 Cohere 重新排名端点来重新排列返回的结果。

from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
from langchain_cohere import CohereRerank
from langchain_community.llms import Coherellm = Cohere(temperature=0)
compressor = CohereRerank()
compression_retriever = ContextualCompressionRetriever(base_compressor=compressor, base_retriever=retriever
)compressed_docs = compression_retriever.invoke("What did the president say about Ketanji Jackson Brown"
)
pretty_print_docs(compressed_docs)
API 参考: ContextualCompressionRetriever | CohereRerank | Cohere

您当然可以在问答管道中使用这个检索器

from langchain.chains import RetrievalQA
API 参考:RetrievalQA
chain = RetrievalQA.from_chain_type(llm=Cohere(temperature=0), retriever=compression_retriever
)chain({"query": query})
{'query': 'What did the president say about Ketanji Brown Jackson','result': " The president speaks highly of Ketanji Brown Jackson, stating that she is one of the nation's top legal minds, and will continue the legacy of excellence of Justice Breyer. The president also mentions that he worked with her family and that she comes from a family of public school educators and police officers. Since her nomination, she has received support from various groups, including the Fraternal Order of Police and judges from both major political parties. \n\nWould you like me to extract another sentence from the provided text? "}
Edit this page

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/26851.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue.js有哪几种甘特图库?Vue.js的5大甘特图库分享!

vue.js有哪几种甘特图库?Vue.js的5大甘特图库分享! 如今,软件市场为任何复杂程度的项目提供了各种现成的计划和调度工具,但这些解决方案可能包含过多的功能或缺乏一些必要的功能。这就是为什么许多公司更愿意投资开发基于网络的定制解决方案…

下载elasticsearch-7.10.2教程

1、ES官网下载地址 Elasticsearch:官方分布式搜索和分析引擎 | Elastic 2、点击下载Elasticsearch 3、点击 View past releases,查看过去的版本 4、选择版本 Elasticsearch 7.10.2,点击 Download,进入下载详情 5、点击 LINUX X8…

基于jeecgboot-vue3的Flowable流程-流程处理(二)

因为这个项目license问题无法开源&#xff0c;更多技术支持与服务请加入我的知识星球。 对应VForm3&#xff0c;原先的后端解析也要做调整 1、获取历史任务的表单信息 // 获取历史任务节点表单数据值List<HistoricVariableInstance> listHistoricVariableInstance his…

Flask快速入门(路由、CBV、请求和响应、session)

Flask快速入门&#xff08;路由、CBV、请求和响应、session&#xff09; 目录 Flask快速入门&#xff08;路由、CBV、请求和响应、session&#xff09;安装创建页面Debug模式快速使用Werkzeug介绍watchdog介绍快速体验 路由系统源码分析手动配置路由动态路由-转换器 Flask的CBV…

meilisearch的索引(index)的最佳实践

官网的第一手资料学新技术&#xff1a;meilisearch官方文档 安装的官网地址&#xff1a;meilisearch安装的官网 部署在生产环境的指导&#xff1a;meilisearch部署在生产环境的指导 Elasticsearch 做为老牌搜索引擎&#xff0c;功能基本满足&#xff0c;但复杂&#xff0c;重…

CentOS系统自带Python2无法使用pip命令

Linux运维工具-ywtool 目录 一. 系统环境二.解决三.验证四.备注(1)输入"yum install -y python-pip",提示没有可用 python-pip包(2)安装完pip后进行升级 一. 系统环境 centos7系统自带的python2.7无法使用pip命令 二.解决 yum install python-pip -y三.验证 pip…

供应链初学者手册——第五部分:信息技术在供应链中的应用

供应链初学者手册 文章目录 供应链初学者手册第五部分&#xff1a;信息技术在供应链中的应用10. 供应链信息系统10.1 SCM 系统的介绍10.2 ERP 系统在供应链中的应用 11. 物联网和区块链技术11.1 物联网在供应链中的应用11.2 区块链技术的潜在应用 总结 第五部分&#xff1a;信息…

华为鸿蒙HarmonyOS应用开发者高级认证题库

判断题 1、云函数打包完成后&#xff0c;需要到AppGallery Connect创建对应函数的触发器才可以在端侧中调用 错 2、在column和Row容器组件中&#xff0c;aligntems用于设置子组件在主轴方向上的对齐格式&#xff0c;justifycontent用于设置子组件在交叉轴方向上的对齐格式 错…

Roboflow 图片分类打标

今天准备找个图片标注工具&#xff0c;在网上搜了一下&#xff0c;看 Yolo 的视频中都是用 Roboflow 工具去尝试了一下&#xff0c;标注确实挺好用的&#xff0c;可以先用一些图片训练一个模型&#xff0c;随后用模型进行智能标注。我主要是做标注然后到处到本地进行模型的训练…

RK3588开发笔记-100M网口自协商成1000M网口

目录 前言 一、问题描述 二、原理图连接 三、解决方法 总结 前言 在进行RK3588开发过程中,遇到一个令人困惑的问题:在使用RTL8211F-CG phy芯片出来的100M网口在自协商后连接速率变成了1000M。这篇博客将详细记录这个问题的产生、排查过程以及最终的解决方案,希望能对遇到…

java 中动态数组向下转型

介绍 接的上面的&#xff0c;现在我们在两个子类student 和teacher中分别添加study 和teacher方法&#xff0c;但父类中没有该方法。那么就需要用到我们的向下转型了 代码 student package hansunping;public class student extends person {private String name;public in…

上心师傅的思路分享(三)--Nacos渗透

目录 1. 前言 2. Nacos 2.1 Nacos介绍 2.2 鹰图语法 2.3 fofa语法 2.3 漏洞列表 未授权API接口漏洞 3 环境搭建 3.1 方式一: 3.2 方式二: 3.3 访问方式 4. 工具监测 5. 漏洞复现 5.1 弱口令 5.2 未授权接口 5.3.1 用户信息 API 5.3.2 集群信息 API 5.3.3 配置…

Android基础-运行时权限

一、引言 随着智能手机和移动互联网的普及&#xff0c;Android操作系统作为其中的佼佼者&#xff0c;其安全性问题日益受到关注。为了保障用户数据的安全和隐私&#xff0c;Android系统引入了权限机制来管理和控制应用程序对系统资源和用户数据的访问。特别是在Android 6.0&am…

借力AI,助力网络钓鱼(邮件)检测

引言 互联网时代&#xff0c;邮件系统依然是人们工作、生活中的很重要的一部分&#xff0c;与此同时&#xff0c;邮件系统的发展带来的钓鱼邮件问题也成为网络中的最大的安全隐患之一。本文将为大家解开网络钓鱼&#xff08;邮件&#xff09;的神秘面纱&#xff0c;一探究竟&a…

Unity3D Shader数据传递语法详解

在Unity3D中&#xff0c;Shader是用于渲染图形的一种程序&#xff0c;它定义了物体在屏幕上的外观。Shader通过接收输入数据&#xff08;如顶点位置、纹理坐标、光照信息等&#xff09;并计算像素颜色来工作。为了使得Shader能够正确运行并产生期望的视觉效果&#xff0c;我们需…

和利时DCS数据采集对接安监平台

在工业互联网日益繁荣的今天&#xff0c;工业数据的采集、传输与利用变得至关重要。特别是在工业自动化领域&#xff0c;数据的实时性和准确性直接关系到生产效率和安全性。和利时DCS&#xff08;分布式控制系统&#xff09;以其卓越的稳定性和可靠性&#xff0c;在工业自动化领…

Ubuntu 24.04 屏蔽snap包

Ubuntu 24.04 屏蔽snap包 屏蔽 这里所说的屏蔽指的是&#xff1a;禁止sudo apt install firefox时安装snap版本的包。 如需卸载snap&#xff0c;请使用关键词搜索。 命令行 cat <<EOF | sudo tee /etc/apt/preferences.d/snap-apps-disable Package: chromium* firef…

chat gpt基本原理解读

chat gpt基本原理解读 ChatGPT是一种基于生成式预训练变换器&#xff08;Generative Pre-trained Transformer, GPT&#xff09;的对话模型&#xff0c;主要通过大量的文本数据训练生成自然语言回复。以下是ChatGPT的基本原理解读&#xff1a; 1. 基本架构 ChatGPT 是基于 GPT…

AIGC底层技术介绍

1.AIGC概述 AIGC&#xff0c;全称Artificial Intelligence Generated Content&#xff0c;即人工智能生成内容。这是一种新兴的人工智能技术&#xff0c;其核心思想是利用人工智能模型&#xff0c;根据给定的主题、关键词、格式、风格等条件&#xff0c;自动生成各种类型的文本…

MySQL安全性管理

用户权限管理 创建和管理用户&#xff1a; 使用 CREATE USER 和 GRANT 语句创建和管理用户。例如&#xff1a; CREATE USER usernamehost IDENTIFIED BY password; GRANT SELECT, INSERT, UPDATE, DELETE ON database.* TO usernamehost;最小权限原则&#xff1a; 只赋予用户…