neural-cherche

neural-cherche

专注于神经搜索模型微调和快速推理的工具库

Neural-Cherche 是一个专为微调和推理神经搜索模型(如 Splade、ColBERT 和 SparseEmbed)设计的库,兼容多种设备。通过该库,用户可以高效地进行模型微调,并在离线和在线环境中执行推理。此外,Neural-Cherche 提供多种检索器和排序器,支持保存嵌入以避免重复计算,适用于多种信息检索任务,并附有便捷的安装步骤和详细文档。

Neural-ChercheColBERTSpladeSparseEmbedBM25Github开源项目

Neural-Cherche 项目介绍

Neural-Cherche 是一个专为精调神经搜索模型而设计的库,主要用于模型如 Splade、ColBERT 和 SparseEmbed。在处理特定数据集时,它能够提供高效的推理方法,并支持对精调后的检索器或排序器进行操作。Neural-Cherche 的设计目标是提供一种简便有效的方法,让用户能够在离线和在线环境中利用神经搜索模型。此外,它还支持保存所有计算的嵌入,以避免冗余计算。

兼容性

Neural-Cherche 可以在多种设备上运行,包括 CPU、GPU 和 MPS 等。在精调过程中,用户可以用任何预训练的 Sentence Transformer 检查点来精调 ColBERT,而对于 Splade 和 SparseEmbed 的精调则需要一个基于语言模型 (MLM) 的预训练模型。

安装指南

要安装 Neural-Cherche,可以使用以下命令:

pip install neural-cherche

如果您计划在训练期间对模型进行评价,请使用:

pip install "neural-cherche[eval]"

快速开始

训练数据集需要由三元组 (anchor, positive, negative) 组成,其中 anchor 是查询,positive 是与 anchor 直接相关的文档,negative 是与 anchor 无关的文档。例如:

X = [ ("anchor 1", "positive 1", "negative 1"), ("anchor 2", "positive 2", "negative 2"), ("anchor 3", "positive 3", "negative 3"), ]

以下是使用 Neural-Cherche 从 Sentence Transformer 预训练检查点对 ColBERT 模型进行精调的示例:

import torch from neural_cherche import models, utils, train model = models.ColBERT( model_name_or_path="raphaelsty/neural-cherche-colbert", device="cuda" if torch.cuda.is_available() else "cpu" # 或 mps ) optimizer = torch.optim.AdamW(model.parameters(), lr=3e-6) X = [ ("query", "positive document", "negative document"), ("query", "positive document", "negative document"), ("query", "positive document", "negative document"), ] for step, (anchor, positive, negative) in enumerate(utils.iter( X, epochs=1, # 训练轮数 batch_size=8, # 每批次三元组数量 shuffle=True )): loss = train.train_colbert( model=model, optimizer=optimizer, anchor=anchor, positive=positive, negative=negative, step=step, gradient_accumulation_steps=50, ) if (step + 1) % 1000 == 0: model.save_pretrained("checkpoint")

文档检索

使用精调后的 ColBERT 模型可以对文档进行重新排序。例如:

import torch from lenlp import sparse from neural_cherche import models, rank, retrieve documents = [ {"id": "doc1", "title": "Paris", "text": "Paris is the capital of France."}, {"id": "doc2", "title": "Montreal", "text": "Montreal is the largest city in Quebec."}, {"id": "doc3", "title": "Bordeaux", "text": "Bordeaux in Southwestern France."}, ] retriever = retrieve.BM25( key="id", on=["title", "text"], count_vectorizer=sparse.CountVectorizer( normalize=True, ngram_range=(3, 5), analyzer="char_wb", stop_words=[] ), k1=1.5, b=0.75, epsilon=0.0, ) model = models.ColBERT( model_name_or_path="raphaelsty/neural-cherche-colbert", device="cuda" if torch.cuda.is_available() else "cpu" # 或 mps ) ranker = rank.ColBERT( key="id", on=["title", "text"], model=model, ) documents_embeddings = retriever.encode_documents(documents=documents) retriever.add(documents_embeddings=documents_embeddings) queries = ["Paris", "Montreal", "Bordeaux"] queries_embeddings = retriever.encode_queries(queries=queries) ranker_queries_embeddings = ranker.encode_queries(queries=queries) candidates = retriever( queries_embeddings=queries_embeddings, batch_size=32, k=100, # 检索文档数量 ) ranker_documents_embeddings = ranker.encode_candidates_documents( candidates=candidates, documents=documents, batch_size=32, ) scores = ranker( queries_embeddings=ranker_queries_embeddings, documents_embeddings=ranker_documents_embeddings, documents=candidates, batch_size=32, ) scores

预训练模型

Neural-Cherche 提供了专门为其设计的预训练检查点,例如 [raphaelsty/neural-cherche-sparse-embed] 和 [raphaelsty/neural-cherche-colbert]。这些检查点在 MS-MARCO 数据集的一个子集上进行了精调,可以进一步在特定数据集上进行精调以更好地适应特定语言。

项目贡献者

  • Benjamin Clavié
  • Arthur Satouf

参考文献

Neural-Cherche 项目使用了一些知名的学术论文作为其理论基础,包括 SPLADE、SparseEmbed、ColBERT 等项目。

许可证

本项目基于 MIT 开源许可证发布,SPLADE 模型仅可用于非商业目的,而 SparseEmbed 和 ColBERT 完全开源,包括商业用途。

编辑推荐精选

音述AI

音述AI

全球首个AI音乐社区

音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。

lynote.ai

lynote.ai

一站式搞定所有学习需求

不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。

AniShort

AniShort

为AI短剧协作而生

专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。

seedancetwo2.0

seedancetwo2.0

能听懂你表达的视频模型

Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。

nano-banana纳米香蕉中文站

nano-banana纳米香蕉中文站

国内直接访问,限时3折

输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动

扣子-AI办公

扣子-AI办公

职场AI,就用扣子

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

下拉加载更多