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

Elasticsearch:使用 inference API 进行语义搜索

本教程中的说明向您展示了如何将 inference API 工作流与各种服务结合使用来对你的数据执行语义搜索。为了说明问题的方便,我将使用 Cohere 服务来进行详细说明。更多其它服务,请详细参阅链接。

重要:有关在 Elastic Stack 中执行语义搜索的最简单方法,请参阅 semantic_text 端到端教程。

在本次练习中,我们将使用 Cohere 的 embed-english-v3.0 模型。事实上,你可以使用任何 Cohere 和 OpenAI 模型,它们均受 inference API 支持。有关 HuggingFace 上可用的推荐模型列表,请参阅支持的模型列表。 你可以尝试如下的模型:

  • Cohere 的 embed-english-v3.0 模型
  • HuggingFace 的 all-mpnet-base-v2 模型
  • OpenAI 的 text-embedding-ada-002 第二代嵌入模型
  • 可通过 Azure AI Studio 或 Azure OpenAI 获得的模型
  • Google Vertex AI 的 text-embedding-004 模型
  • Mistral 的 mistral-embed 模型
  • Amazon Bedrock 的 amazon.titan-embed-text-v1 模型

要求

你需要 Cohere 帐户才能将 inference API 与 Cohere 服务一起使用。

创建推理端点

使用 Create inference API 创建推理端点:

PUT _inference/text_embedding/cohere_embeddings  /* 1 */
{"service": "cohere","service_settings": {"api_key": "<api_key>", /* 2 */ "model_id": "embed-english-v3.0", /* 3 */ "embedding_type": "byte"}
}
  1. 任务类型为路径中的 text_embedding,推理端点的唯一标识符 inference_id 为 cohere_embeddings。
  2. 你的 Cohere 帐户的 API 密钥。你可以在 Cohere 仪表板的 API 密钥部分下找到你的 API 密钥。你只需提供一次 API 密钥。Get inference API 不会返回你的 API 密钥。
  3. 要使用的嵌入模型的名称。你可以在此处找到 Cohere 嵌入模型的列表。

注意:使用此模型时,建议在密集向量字段映射中使用的相似度度量是点积。对于 Cohere 模型,嵌入被归一化为单位长度,在这种情况下点积和余弦度量是等效的。

从上面的请求上,我们可以看出来有一个限制 requests_per_minute 为 10000。这是因为我使用的是测试账号。

创建索引映射

必须创建目标索引的映射 - 包含模型将根据你的输入文本创建的嵌入的索引。目标索引必须具有一个字段,该字段对于大多数模型具有 dense_vector 字段类型,对于稀疏向量模型具有 sparse_vector 字段类型,就像 elser 服务的情况一样,用于索引所用模型的输出。

PUT cohere-embeddings
{"mappings": {"properties": {"content_embedding": {  /* 1 */"type": "dense_vector",  /* 2 */"dims": 1024,  /* 3 */"element_type": "byte"},"content": { /* 4 */"type": "text" }}}
}
  1. 包含生成的标记的字段的名称。下一步必须在推理管道配置中引用它。
  2. 包含标记的字段是 dense_vector 字段。
  3. 模型的输出维度。在您使用的模型的 Cohere 文档中找到此值。
  4. 创建密集向量表示的字段的名称。在此示例中,字段的名称为 content。下一步必须在推理管道配置中引用它。
  5. 字段类型,在此示例中为 text。

使用推理处理器创建摄取管道

使用 inference processor 创建 ingest pipeline,并使用上面创建的模型对管道中摄取的数据进行推理。

PUT _ingest/pipeline/cohere_embeddings
{"processors": [{"inference": {"model_id": "cohere_embeddings", /* 1 */"input_output": {  /* 2 */"input_field": "content","output_field": "content_embedding"}}}]
}
  1. 你使用 Create inference API 创建的推理端点的名称,在该步骤中称为 inference_id。
  2. 定义推理过程的 input_field 和将包含推理结果的 output_field 的配置对象。

加载数据

在此步骤中,你将加载稍后在推理提取管道中用于从中创建嵌入的数据。

使用 msmarco-passagetest2019-top1000 数据集,它是 MS MARCO Passage Ranking 数据集的子集。它包含 200 个查询,每个查询都附有相关文本段落列表。所有唯一段落及其 ID 都已从该数据集中提取并编译为 tsv 文件。

下载文件并使用机器学习 UI 中的 Data Visualizer 将其上传到你的集群。将名称 id 分配给第一列,将内容分配给第二列。索引名称为 test-data。上传完成后,你可以看到一个名为 test-data 的索引,其中包含 182469 个文档。

具体的步骤可以参考文章 “Elasticsearch:使用 ELSER 进行语义搜索 - sparse_vector”。

通过 inference ingest pipeline 采集数据

通过使用你选择的模型的推理管道重新索引数据,从文本中创建嵌入。此步骤使用重新索引 API 模拟通过管道采集数据。

POST _reindex?wait_for_completion=false
{"source": {"index": "test-data","size": 50  /* 1 */},"dest": {"index": "cohere-embeddings","pipeline": "cohere_embeddings"}
}
  1. 重新索引的默认批次大小为 1000。将大小减小到较小的数字可以加快重新索引过程的更新速度,从而使您能够密切跟踪进度并尽早发现错误。

注意:你的 Cohere 账户的速率限制可能会影响重新索引过程的吞吐量。

注意:由于我采用的是测试账号,有速率限制,在上面,我设置 max_docs 为10,也即只针对前 10个 文档进行向量化。你可以根据自己的账号来具体操作。

该调用返回一个任务 ID 来监控进度:

GET _tasks/<task_id>

上面显示我们的10个文档已经完成了向量的生成。我们可以通过如下的命令来进行查看:

如果你不想等到重新索引过程完全完成(对于大型数据集而言,这可能需要数小时),你也可以取消重新索引过程:

POST _tasks/<task_id>/_cancel

语义搜索

使用嵌入丰富数据集后,你可以使用 semantic search 查询数据。对于密集向量模型,将 query_vector_builder 传递给 k-最近邻 (kNN) 向量搜索 API,并提供查询文本和您用于创建嵌入的模型。对于像 ELSER 这样的稀疏向量模型,请使用 sparse_vector 查询,并提供查询文本和您用于创建嵌入的模型。

注意:如果你取消了重新索引过程,则你只会运行查询部分数据,这会影响结果的质量。

GET cohere-embeddings/_search
{"knn": {"field": "content_embedding","query_vector_builder": {"text_embedding": {"model_id": "cohere_embeddings","model_text": "Muscles in human body"}},"k": 10,"num_candidates": 100},"_source": ["id","content"]
}

结果,你将从 cohere-embeddings 索引中收到与查询含义最接近的前 10 个文档,这些文档按与查询的接近程度排序:

"hits": [{"_index": "cohere-embeddings","_id": "-eFWCY4BECzWLnMZuI78","_score": 0.737484,"_source": {"id": 1690948,"content": "Oxygen is supplied to the muscles via red blood cells. Red blood cells carry hemoglobin which oxygen bonds with as the hemoglobin rich blood cells pass through the blood vessels of the lungs.The now oxygen rich blood cells carry that oxygen to the cells that are demanding it, in this case skeletal muscle cells.ther ways in which muscles are supplied with oxygen include: 1  Blood flow from the heart is increased. 2  Blood flow to your muscles in increased. 3  Blood flow from nonessential organs is transported to working muscles."}},{"_index": "cohere-embeddings","_id": "HuFWCY4BECzWLnMZuI_8","_score": 0.7176013,"_source": {"id": 1692482,"content": "The thoracic cavity is separated from the abdominal cavity by the  diaphragm. This is a broad flat muscle.    (muscular) diaphragm The diaphragm is a muscle that separat…e the thoracic from the abdominal cavity. The pelvis is the lowest part of the abdominal cavity and it has no physical separation from it    Diaphragm."}},{"_index": "cohere-embeddings","_id": "IOFWCY4BECzWLnMZuI_8","_score": 0.7154432,"_source": {"id": 1692489,"content": "Muscular Wall Separating the Abdominal and Thoracic Cavities; Thoracic Cavity of a Fetal Pig; In Mammals the Diaphragm Separates the Abdominal Cavity from the"}},{"_index": "cohere-embeddings","_id": "C-FWCY4BECzWLnMZuI_8","_score": 0.695313,"_source": {"id": 1691493,"content": "Burning, aching, tenderness and stiffness are just some descriptors of the discomfort you may feel in the muscles you exercised one to two days ago.For the most part, these sensations you experience after exercise are collectively known as delayed onset muscle soreness.urning, aching, tenderness and stiffness are just some descriptors of the discomfort you may feel in the muscles you exercised one to two days ago."}},(...)]

更多阅读 :将 Cohere 与 Elasticsearch 结合使用

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 智能对决:提示词攻防中的AI安全博弈
  • [数据集][目标检测]玉米病害检测数据集VOC+YOLO格式6000张4类别
  • 搭建线上虚拟展厅,需要哪些技术?
  • 如何使用useMemo来优化React组件的性能?
  • SpringBoot整合第三方技术
  • PowerBi 柱形图,数据标签无法显示在端外
  • 基于STM32设计的防盗书包(华为云IOT)(216)
  • 大数据Flink(一百一十三):Flink Python写DataStreamAPI作业快速入门
  • PySpark
  • 面向Data+AI时代的数据湖创新与优化(附Iceberg案例)
  • 电脑错误mfc140.dll丢失怎么办?mfc140.dll丢失如何修复?
  • MySQL数据库安装(详细)—>Mariadb的安装(day21)
  • 印度再现超级大片,豪华阵容加顶级特效
  • 【GC】垃圾回收原理分析
  • 股票锁仓是唯一的盈利方法吗,不如试试程序交易做T
  • [nginx文档翻译系列] 控制nginx
  • 《剑指offer》分解让复杂问题更简单
  • Computed property XXX was assigned to but it has no setter
  • echarts的各种常用效果展示
  • echarts花样作死的坑
  • go语言学习初探(一)
  • HTTP--网络协议分层,http历史(二)
  • Leetcode 27 Remove Element
  • MySQL几个简单SQL的优化
  • nfs客户端进程变D,延伸linux的lock
  • nginx 负载服务器优化
  • oldjun 检测网站的经验
  • PermissionScope Swift4 兼容问题
  • Python进阶细节
  • python学习笔记-类对象的信息
  • Spring核心 Bean的高级装配
  • SQLServer之索引简介
  • 警报:线上事故之CountDownLatch的威力
  • 数据仓库的几种建模方法
  • 算法-插入排序
  • 小李飞刀:SQL题目刷起来!
  • 终端用户监控:真实用户监控还是模拟监控?
  • 国内开源镜像站点
  • #{}和${}的区别?
  • #Datawhale X 李宏毅苹果书 AI夏令营#3.13.2局部极小值与鞍点批量和动量
  • #nginx配置案例
  • #stm32驱动外设模块总结w5500模块
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (十六)串口UART
  • (五)Python 垃圾回收机制
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • **PHP二维数组遍历时同时赋值
  • ..回顾17,展望18
  • .htaccess 强制https 单独排除某个目录
  • .NET 5种线程安全集合
  • .NET Micro Framework 4.2 beta 源码探析