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

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

相关文章:

  • 【论文复现|智能算法改进】基于改进鲸鱼优化算法的移动机器人多目标点路径规划
  • 1.Vue2使用ElementUI-初识及环境搭建
  • ViewModel、Lifecycles、LiveData基本使用
  • TK防关联引流系统:全球多账号运营,一“键”掌控!
  • Typora 破解、激活!亲测有效!2024 最新激活方法
  • 排序-快排算法对数组进行排序
  • 6-11 函数题:某范围中的最小值
  • 源代码防泄密经验分享之安全上网篇
  • 联邦学习的基本流程,联邦学习权重聚合,联邦学习权重更新
  • Serverless 使用OOS将http文件转存到对象存储
  • Spring的循环依赖
  • Vite - 项目打包从 0 到 1(完美解决打包后访问白屏问题)
  • Python第二语言(八、Python包)
  • 解决富文本中抖音视频无法播放的问题——403
  • HTML静态网页成品作业(HTML+CSS)—— 非遗皮影戏介绍网页(6个页面)
  • 230. Kth Smallest Element in a BST
  • Centos6.8 使用rpm安装mysql5.7
  • gops —— Go 程序诊断分析工具
  • laravel5.5 视图共享数据
  • mysql_config not found
  • Python_OOP
  • Shell编程
  • vue 配置sass、scss全局变量
  • 阿里云应用高可用服务公测发布
  • 分布式事物理论与实践
  • 基于 Babel 的 npm 包最小化设置
  • 讲清楚之javascript作用域
  • 普通函数和构造函数的区别
  • 使用SAX解析XML
  • 【运维趟坑回忆录】vpc迁移 - 吃螃蟹之路
  • FaaS 的简单实践
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #QT(QCharts绘制曲线)
  • (2.2w字)前端单元测试之Jest详解篇
  • (2024,Flag-DiT,文本引导的多模态生成,SR,统一的标记化,RoPE、RMSNorm 和流匹配)Lumina-T2X
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (备忘)Java Map 遍历
  • (笔试题)分解质因式
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (一)kafka实战——kafka源码编译启动
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • (状压dp)uva 10817 Headmaster's Headache
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .gitignore文件忽略的内容不生效问题解决
  • .NET Micro Framework初体验(二)
  • .net mvc 获取url中controller和action
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .NET导入Excel数据