Elasticsearch:同义词在 RAG 中重要吗?

作者:来自 Elastic Jeffrey Rengifo 及 Tomás Murúa

探索 RAG 应用程序中 Elasticsearch 同义词的功能。

同义词允许我们使用具有相同含义的不同词语在文档中搜索,以确保用户无论使用什么确切的词语都能找到他们所寻找的内容。你可能会认为,由于 RAG 应用程序使用语义/向量搜索,同义词功能的一部分已经被同义词涵盖(因为根据定义,同义词是语义相关的词)。

这是真的吗?语义搜索真的能取代同义词吗?在本文中,我们将分析在 RAG 应用程序中使用同义词的影响。

步骤

  • 配置端点
  • 配置同义词
  • 索引文档
  • 语义搜索
  • 同义词和 RAG

配置推理端点

对于这个例子,我们将在 HR 环境中实现带有和不带有同义词的 RAG(Retrieval-Augmented Generation - 检索增强生成)系统。我们将使用术语 PTO(Paid Time Off - 带薪休假)的变体(如 “vacation” 或 “holiday”)为不同的文档编制索引。然后我们将配置同义词来展示这些关系如何提高搜索的相关性和准确性。

首先,让我们通过在 Kibana DevTools 中运行以下命令,使用带有推理 API(inference api) 的 ELSER 模型创建一个端点:

PUT _inference/sparse_embedding/code-wave_inference
{
  "service": "elasticsearch",
  "service_settings": {
    "num_allocations": 1,
    "num_threads": 1
  }
}

配置同义词

Elasticsearch 中的同义词是什么?

在 Elasticsearch 中,同义词(synonyms)是具有相同或相似含义的单词或短语,存储为同义词集,可以作为文件或通过 API 进行管理。它们允许用户找到相关信息,即使他们使用不同的术语来指代同一概念。

因此,例如,如果我们创建一组同义词,其中 “holiday” 和 “vacation” 是 “Paid Time Off” 的同义词,当员工搜索其中任何一个词时,他们就会找到与所有词相关的文档。

你可以在这篇文章中阅读有关它们的更多信息。

让我们使用同义词 API(synonyms API:) 创建一组同义词:

PUT _synonyms/code-wave_synonyms
{
  "synonyms_set": [
    {
      "synonyms": "holidays, paid time off"
    }
  ]
}

值得注意的是,同义词集必须先进行配置,然后才能应用于索引。

现在,让我们定义数据的设置和映射:

PUT /code-wave_index
{
  "settings": {
    "analysis": {
      "filter": {
        "synonyms_filter": {
          "type": "synonym_graph",
          "synonyms_set": "code-wave_synonyms",
          "updateable": true
        }
      },
      "analyzer": {
        "my_search_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "synonyms_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text_field": {
        "type": "text",
        "analyzer": "standard",
        "copy_to": "semantic_field",
        "fields": {
          "synonyms": {
            "type": "text",
            "analyzer": "standard",
            "search_analyzer": "my_search_analyzer"
          }
        }
      },
      "semantic_field": {
        "type": "semantic_text",
        "inference_id": "code-wave_inference"
      }
    }
  }
}

我们将使用 semantic_text 字段进行语义搜索,并使用 synonyms graph token filter 来处理多词同义词。

我们还创建了 text_field.synonym 版本和 text_field 版本的字段(可以针对这两种不同的类型进行搜索。请注意的是这两个类型都是 text 类型),以便更好地控制如何使用或不考虑同义词来查询字段。

最后,我们使用 copy_to 将 text_field 的值复制到该字段的 semantic_text 版本,以实现全文和语义查询。

索引文档

我们现在将使用批量 API 索引我们的文档:

POST _bulk
{"index":{"_index":"code-wave_index","_id":"1"}}
{"semantic_field":"Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones.","text_field":"Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones."}
{"index":{"_index":"code-wave_index","_id":"2"}}
{"semantic_field":"Holidays: Paid public holidays recognized each calendar year.","text_field":"Holidays: Paid public holidays recognized each calendar year."}
{"index":{"_index":"code-wave_index","_id":"3"}}
{"semantic_field":"Sick leave: Paid sick leave of up to 15 days per year.","text_field":"Sick leave: Paid sick leave of up to 15 days per year."}
{"index":{"_index":"code-wave_index","_id":"4"}}
{"semantic_field":"Holidays sale: Enjoy discounts up to 50% during our exclusive holidays sale event!","text_field":"Holidays sale: Enjoy discounts up to 50% during our exclusive holidays sale event!"}
{"index":{"_index":"code-wave_index","_id":"5"}}
{"semantic_field":"Holidays recipes: Try our top 10 holidays dessert recipes, perfect for family gatherings and celebrations.","text_field":"Holidays recipes: Try our top 10 holidays dessert recipes, perfect for family gatherings and celebrations."}
{"index":{"_index":"code-wave_index","_id":"6"}}
{"semantic_field":"Holidays travel: Find the best deals for your holidays flights and accommodations this season.","text_field":"Holidays travel: Find the best deals for your holidays flights and accommodations this season."}
{"index":{"_index":"code-wave_index","_id":"7"}}
{"semantic_field":"Holidays music: Stream your favorite holidays classics and discover new seasonal hits.","text_field":"Holidays music: Stream your favorite holidays classics and discover new seasonal hits."}
{"index":{"_index":"code-wave_index","_id":"8"}}
{"semantic_field":"Holidays decorations: Our store offers a wide range of holidays decorations to make your home festive.","text_field":"Holidays decorations: Our store offers a wide range of holidays decorations to make your home festive."}
{"index":{"_index":"code-wave_index","_id":"9"}}
{"semantic_field":"Holidays movies: Check out our list of must-watch holidays movies for cozy winter nights.","text_field":"Holidays movies: Check out our list of must-watch holidays movies for cozy winter nights."}
{"index":{"_index":"code-wave_index","_id":"10"}}
{"semantic_field":"Holidays festival: Join us at the city's annual holidays festival featuring lights, music, and local food.","text_field":"Holidays festival: Join us at the city's annual holidays festival featuring lights, music, and local food."}
{"index":{"_index":"code-wave_index","_id":"11"}}
{"semantic_field":"Holidays weather: Stay updated with our holidays weather forecast to plan your activities.","text_field":"Holidays weather: Stay updated with our holidays weather forecast to plan your activities."}
{"index":{"_index":"code-wave_index","_id":"12"}}
{"semantic_field":"Holidays gift guide: Browse our ultimate holidays gift guide for everyone on your list.","text_field":"Holidays gift guide: Browse our ultimate holidays gift guide for everyone on your list."}
{"index":{"_index":"code-wave_index","_id":"13"}}
{"semantic_field":"Holidays traditions: Explore unique holidays traditions celebrated around the world.","text_field":"Holidays traditions: Explore unique holidays traditions celebrated around the world."}

我们现在就可以开始搜索了!但首先,让我们通过搜索 holidays 来确保同义词有效:

GET code-wave_index/_search
{
  "_source": {
    "excludes": [
      "*embeddings",
      "*chunks"
    ]
  },
  "query": {
    "multi_match": {
      "query": "holidays",
      "fields": [
        "text_field^10",
        "text_field.synonyms^0.6"
      ]
    }
  }
}

我们对 boost 进行调整,使同义词的得分低于原始单词。

检查响应:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 12,
      "relation": "eq"
    },
    "max_score": 5.2014494,
    "hits": [
      {
        "_index": "code-wave_index",
        "_id": "2",
        "_score": 3.0596757,
        "_source": {
          "text_field": "Holidays: Paid public holidays recognized each calendar year.",
          "semantic_field": {
            "inference": {
              "inference_id": "code-wave_inference",
              "model_settings": {
                "task_type": "sparse_embedding"
              }
            },
            "text": "Holidays: Paid public holidays recognized each calendar year."
          }
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "1",
        "_score": 3.023004,
        "_source": {
          "text_field": "Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones.",
          "semantic_field": {
            "inference": {
              "inference_id": "code-wave_inference",
              "model_settings": {
                "task_type": "sparse_embedding"
              }
            },
            "text": "Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones."
          }
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "13",
        "_score": 2.9230676,
        "_source": {
          "text_field": "Holidays traditions: Explore unique holidays traditions celebrated around the world.",
          "semantic_field": {
            "inference": {
              "inference_id": "code-wave_inference",
              "model_settings": {
                "task_type": "sparse_embedding"
              }
            },
            "text": "Holidays traditions: Explore unique holidays traditions celebrated around the world."
          }
        }
      },
      ...
    ]
  }
}

我们可以看到,当我们搜索 “holidays” 时,第二个文档有同义词:“Paid Time Off”。

混合搜索

混合搜索使我们能够将全文和语义搜索查询的结果组合成一个规范化的结果集,方法是使用 RRF(Reciprocal Rank Fusion - 倒述排序融合)来平衡来自不同检索器的分数。

GET code-wave_index/_search
{
  "_source": "text_field",
  "retriever": {
    "rrf": {
      "retrievers": [
        {
          "standard": {
            "query": {
              "nested": {
                "path": "semantic_field.inference.chunks",
                "query": {
                  "sparse_vector": {
                    "inference_id": "code-wave_inference",
                    "field": "semantic_field.inference.chunks.embeddings",
                    "query": "holidays"
                  }
                }
              }
            }
          }
        },
        {
          "standard": {
            "query": {
              "multi_match": {
                "query": "holidays",
                "fields": [
                  "text_field.synonyms"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

回复:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 13,
      "relation": "eq"
    },
    "max_score": 0.03175403,
    "hits": [
      {
        "_index": "code-wave_index",
        "_id": "7",
        "_score": 0.03175403,
        "_source": {
          "text_field": "Holidays music: Stream your favorite holidays classics and discover new seasonal hits."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "13",
        "_score": 0.031257633,
        "_source": {
          "text_field": "Holidays traditions: Explore unique holidays traditions celebrated around the world."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "4",
        "_score": 0.031009614,
        "_source": {
          "text_field": "Holidays sale: Enjoy discounts up to 50% during our exclusive holidays sale event!"
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "2",
        "_score": 0.030834913,
        "_source": {
          "text_field": "Holidays: Paid public holidays recognized each calendar year."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "6",
        "_score": 0.03079839,
        "_source": {
          "text_field": "Holidays travel: Find the best deals for your holidays flights and accommodations this season."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "11",
        "_score": 0.02964427,
        "_source": {
          "text_field": "Holidays weather: Stay updated with our holidays weather forecast to plan your activities."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "5",
        "_score": 0.029418126,
        "_source": {
          "text_field": "Holidays recipes: Try our top 10 holidays dessert recipes, perfect for family gatherings and celebrations."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "12",
        "_score": 0.028991597,
        "_source": {
          "text_field": "Holidays gift guide: Browse our ultimate holidays gift guide for everyone on your list."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "1",
        "_score": 0.016393442,
        "_source": {
          "text_field": "Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones."
        }
      },
      {
        "_index": "code-wave_index",
        "_id": "10",
        "_score": 0.016393442,
        "_source": {
          "text_field": "Holidays festival: Join us at the city's annual holidays festival featuring lights, music, and local food."
        }
      }
    ]
  }
}

该查询将返回语义和文本相关的文档。

同义词和 RAG

在本节中,我们将评估同义词和语义搜索如何改进 RAG 系统中的查询。我们将使用一个关于休息日的常见问题作为此示例:

How many vacation days are provided for holidays?

对于这个问题,我们对文档 1 中的信息感兴趣。文档 2 更接近我们想要的结果,但并不精确。当我们不使用同义词进行搜索时,我们将得到此结果。我们来看看它们的内容:

  • [1] Paid time off: All employees receive 20 days of paid vacation annually, with additional days earned for tenure milestones.
  • [2] Holidays: Paid public holidays recognized each calendar year.

这两个文档都包含与休息日(days off)相关的信息,但只有文档 2 特别使用了术语 “holidays”,因此我们可以测试同义词和语义搜索在 Playground 中的工作方式。

你可以从 Search>Playground 访问 Playground。从那里,你需要配置你想要使用的 LLM 并选择我们已经创建的索引作为上下文发送。你可以在此处阅读有关 Playground 及其配置的更多信息

配置完 Playground 后,如果我们点击查询按钮,我们可以看到同义词已被停用:

对于每个问题,我们会将前一个查询的前三个结果发送给 LLM,作为上下文:

现在,让我们向 Playground 提出问题并检查停用同义词后的结果:

由于前三个搜索结果中没有列出说明员工每年可享受多少假期的文件,因此 LLM 无法回答这个问题。在这种情况下,最接近的结果在文档 [2] 中。

注意:通过点击 “Snippet”,我们可以看到答案在 Elasticsearch 中的具体内容。

让我们清理聊天记录,激活同义词并再次提出同样的问题:

请注意,当你启用 semantic_text 字段和 text 字段时,Playground 将自动生成混合搜索查询:

让我们重复一下这个问题,现在激活同义词:

现在,答案确实包含了我们正在搜索的文档,因为同义词允许将文档 [1] 发送到 LLM。

结论

在本文中,我们发现同义词是搜索系统的基本组成部分,即使在使用语义搜索时也不一定涵盖同义词功能。

同义词允许我们根据用例控制要提升的文档,并通过调整相关性来提高准确性。另一方面,语义搜索对于 recall 很有用,这意味着它可以引入潜在的相关结果,而无需我们为每个相关术语添加同义词。

通过混合搜索,我们可以同时进行同义词和语义搜索,实现两全其美的效果。使用 Playground,如果我们选择语义和文本字段的组合作为搜索字段,它将自动为我们构建混合查询。

想要获得 Elastic 认证吗?了解下一期 Elasticsearch 工程师培训何时举行!

Elasticsearch 包含许多新功能,可帮助你为你的用例构建最佳的搜索解决方案。深入了解我们的示例笔记本以了解更多信息,开始免费云试用,或立即在本地机器上试用 Elastic。

原文:Are synonyms important in RAG? - Elasticsearch Labs

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

0基础学LabVIEW

对于零基础的朋友来说,学习LabVIEW需要一个科学的学习路径和方法。通过观看优质的B站教程打好基础,再结合实际项目进行实践操作,能够快速提升LabVIEW的应用能力。以下是从入门到进阶的学习建议。 ​ 一、利用B站入门教程打基础 筛选优质教程…

7. Docker 容器数据卷的使用(超详细的讲解说明)

7. Docker 容器数据卷的使用(超详细的讲解说明) 文章目录 7. Docker 容器数据卷的使用(超详细的讲解说明)1. Docker容器数据卷概述2. Docker 容器数据卷的使用演示:2.1 宿主 和 容器之间映射添加容器卷2.2 容器数据卷 读写规则映射添加说明2.3 容器数据卷的继承和共…

零售行业线下门店的AI知识库应用实践

在竞争激烈的零售行业,线下门店的运营效率和服务质量是企业成功的关键因素之一。随着AI技术的不断发展,越来越多的零售企业开始探索如何利用AI知识库提升线下门店的运营效率和员工服务水平。 1. AI知识库在零售行业的应用背景 零售行业面临着诸多挑战&…

Unity Behavior Designer - Behavior Trees for Everyone(行为树)为什么称作AI 的“基石”之一

Behavior Designer 是 Unity 中一个用于创建行为树的插件,主要用于 AI 和 NPC(非玩家角色)的行为管理。行为树(Behavior Trees)是一种基于树形结构的决策树模型,广泛应用于游戏开发中,特别是在控…

八大排序——简单选择排序

目录 1.1基本操作: 1.2动态图: 1.3代码: 代码解释 1. main 方法 2. selectSort 方法 示例运行过程 初始数组 每轮排序后的数组 最终排序结果 代码总结 1.1基本操作: 选择排序(select sorting)也…

Jenkins 新建配置 Freestyle project 任务 六

Jenkins 新建配置 Freestyle project 任务 六 一、新建任务 在 Jenkins 界面 点击 New Item 点击 Apply 点击 Save 回到任务主界面 二、General 点击左侧 Configure Description:任务描述 勾选 Discard old builds Discard old builds:控制何时…

使用 Dockerfile 构建自定义 Nginx 镜像并集成 nginx_upstream_check_module

目录 1. 为什么需要自定义 Nginx 镜像? 2. Dockerfile 解析 2.1 基础镜像选择 2.2 安装依赖 2.3 下载并解压 Nginx 源码 2.4 应用补丁并编译 Nginx 2.5 暴露端口并设置启动命令 3. 构建并运行自定义 Nginx 镜像 3.1 构建镜像 3.2 运行容器 3.3 健康检测配…

【论文笔记】Are Self-Attentions Effective for Time Series Forecasting? (NeurIPS 2024)

官方代码https://github.com/dongbeank/CATS Abstract 时间序列预测在多领域极为关键,Transformer 虽推进了该领域发展,但有效性尚存争议,有研究表明简单线性模型有时表现更优。本文聚焦于自注意力机制在时间序列预测中的作用,提…

【MQ】Spring3 中 RabbitMQ 的使用与常见场景

一、初识 MQ 传统的单体架构,分布式架构的同步调用里,无论是方法调用,还是 OpenFeign 难免会有以下问题: 扩展性差(高耦合,需要依赖对应的服务,同样的事件,不断有新需求&#xff0…

LabVIEW与USB设备开发

开发一台USB设备并使用LabVIEW进行上位机开发,涉及底层驱动的编写、USB通信协议的实现以及LabVIEW与设备的接口设计。本文将详细介绍如何开发USB设备驱动、实现LabVIEW与USB设备的通信以及优化数据传输,帮助用户顺利完成项目开发。下面是一个详细的说明&…

kali连接xshell

1.先保证宿主机:以太网适配器 VMware Network Adapter VMnet8 和kali(net 模式)在同一个网段 windows VMnet8开启 查看是否是自动获取ip ipv4 和ipv6一样的 查看 windows VMnet8的IPv4的地址 查看 kali 的IP地址 window ping的结果&#xf…

557. 反转字符串中的单词 III 简单

557. 反转字符串中的单词 IIIhttps://leetcode.cn/problems/reverse-words-in-a-string-iii/ 给定一个字符串 s ,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例 1: 输入:s "Lets take LeetC…

多语言订货系统的语言适配与本地化开发策略

在全球化浪潮的席卷下,商业世界的边界日益模糊,企业纷纷踏上国际化征程,与世界各地的客户展开紧密合作。在这一背景下,多语言订货系统成为企业开拓全球市场的关键基础设施,其语言适配能力与本地化开发策略,…

OpenWRT中常说的LuCI是什么——LuCI介绍(一)

我相信每个玩openwrt的小伙伴都或多或少看到过luci这个东西,但luci到底是什么东西,可能还不够清楚,今天就趁机来介绍下,openwrt中的luci,到底是个什么东西。 什么是LuCI? 首先,LuCI是OpenWRT中…

第39周:猫狗识别 2(Tensorflow实战第九周)

目录 前言 一、前期工作 1.1 设置GPU 1.2 导入数据 输出 二、数据预处理 2.1 加载数据 2.2 再次检查数据 2.3 配置数据集 2.4 可视化数据 三、构建VGG-16网络 3.1 VGG-16网络介绍 3.2 搭建VGG-16模型 四、编译 五、训练模型 5.1 上次程序的主要Bug 5.2 修改版…

vue3 描边加载动画

效果&#xff1a; 组件代码&#xff1a; <template><divclass"loading-wrap"ref"loadingWrap":style"[{ borderRadius: styles.borderRadius || 4px },{ borderColor: styles.borderColor || #409eff },{ border: loading ? 1px solid #40…

20240911 光迅科技 笔试

文章目录 1、选择题1.11.21.31.41.51.61.71.81.91.101.111.121.131.141.152、编程题2.1岗位:嵌入式软件工程师 题型:15 道选择题,1 道编程题 注意:本文章暂无解析,谨慎分辨答案对错 1、选择题 1.1 若某图有 100 个顶点、90 条边,则该图一定是 (C) 有向图连通图非连…

C++软件开发常见面试题(二)

struct和class的区别 指针和引用的区别&#xff1f;c为什么提供了引用这个东西&#xff1f; 说const 指针和指针 const的区别&#xff1f;例如const A*是什么意思&#xff1f;了解const 函数吗&#xff1f;具体是不修改哪些数据成员呢&#xff1f; 多态。追问&#xff1a;动态…

[生信云问题分析] 为什么医院/单位/校园网络,无法通过ssh协议访问服务器

使用生信云,生信分析更省心轻松&#xff1b;欢迎访问生信圆桌 www.tebteb.cc了解 背景 许多科研人员在日常工作中需要使用单位的网络&#xff0c;但有时会遇到一个奇怪的现象&#xff1a;虽然网页可以正常打开&#xff0c;却无法通过SSH协议访问科研服务器。SSH&#xff08;Se…

java项目之基于推荐算法的图书购物网站源码(ssm+mybatis+mysql)

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于ssm的基于推荐算法的图书购物网站项目。项目源码以及部署相关请联系风歌&#xff0c;文末附上联系信息 。 项目简介&#xff1a; 基于推荐算法的…