Elasticsearch:使用向量搜索来查询及比较文字 - NLP text embedding

这篇具有很好参考价值的文章主要介绍了Elasticsearch:使用向量搜索来查询及比较文字 - NLP text embedding。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Elastic Stack 机器学习功能可以生成嵌入(embeddings),你可以使用它在非结构化文本中搜索或比较不同的文本片段。传统上,我们在搜索文本的时候,更加倾向于把文字进行分词,然后再对 token 进行比对:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

在上面,我们在文字中完全或部分匹配分词后的 token,从而完成我们的文字搜索。随着机器学习的出现,我们甚至可以直接在文字中直接使用 “问-答” 这样的方式进行搜索,比如:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

在这种情况下,它不仅限于对文字的 token 匹配,它可以对语义进行匹配,比如,在上面,我们可以查询问题 “How fast should my internet be?”。我们可以使用 Elasticsearch 所提供的 vector search 来实现。

在我之前的文章中,我已经写了几篇关于 NLP 的文章。如果你想了解更多,请参阅文章 “Elastic:开发者上手指南” 中的 “NLP - 自然语言处理”。有关目前被支持的三方 NLP 模型,你可以在地址找到。在那里,你可以找到一个比较完整的被支持的 NLP 模型的列表。

在今天的展示中,我将使用最新的 Elastic Stack 8.3 来进行展示。

什么是 text embedding?

文本嵌入(text embedding)是一项产生称为嵌入的文本数学表示的任务。 机器学习模型将文本转换为数值数组(也称为向量)。 具有相似含义的内容片段具有相似的表示。 这意味着可以通过使用数学相似性函数来确定不同的文本片段在语义上是否相似、不同甚至相反。

此任务仅负责生成嵌入。 创建嵌入时,可以将其存储在 dense_vector 字段中并在搜索时使用。 例如,你可以在 k-nearest (kNN) 搜索中使用这些向量来实现语义搜索功能。

以下是生成文本嵌入的示例:

{
    docs: [{"text_field": "The quick brown fox jumps over the lazy dog."}]
}
...

该任务返回以下结果:

...
{
    "predicted_value": [0.293478, -0.23845, ..., 1.34589e2, 0.119376]
    ...
}
...

安装

Elasticsearch 及 Kibana

如果你还没有安装好自己的 Elastic Stack,请参考如下的文章来安装 Elasticsearch 及 Kibana:

  • 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch
  • Elastic:使用 Docker 安装 Elastic Stack 8.0 并开始使用

由于我们需要使用到机器学习,我们在本地安装时,必须启动白金版试用功能:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

Eland

可以使用 Pip 从 PyPI 安装 Eland:

python -m pip install eland

也可以使用 Conda 从 Conda Forge 安装 Eland:

conda install -c conda-forge eland

希望在不安装 Eland 的情况下使用它的用户,为了只运行可用的脚本,可以构建 Docker 容器:

git clone https://github.com/elastic/eland
cd eland
docker build -t elastic/eland .

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

 Eland 将 Hugging Face 转换器模型到其 TorchScript 表示的转换和分块过程封装在一个 Python 方法中; 因此,这是推荐的导入方法。

  1. 安装 Eland Python 客户端。
  2. 运行 eland_import_hub_model 脚本。 例如:
eland_import_hub_model --url <clusterUrl> \ 
--hub-model-id elastic/distilbert-base-cased-finetuned-conll03-english \ 
--task-type ner 
  • 指定 URL 以访问你的集群。 例如,https://<user>:<password>@<hostname>:<port>。
  • 在 Hugging Face 模型中心中指定模型的标识符。
  • 指定 NLP 任务的类型。 支持的值为 fill_mask、ner、text_classification、text_embedding, question_answering 和 zero_shot_classification。

写入数据到 Elasticsearch

我们首先到如下的地址克隆如下的项目:

git clone https://github.com/liu-xiao-guo/nlp-webinar

等我们下载完上面的仓库文件后,我们可以发现有如下的几个文件:

$ pwd
/Users/liuxg/data/nlp-webinar
$ ls
README.md                 console-commands.txt
blogs.json                eland-docker-commands.txt

其中的 blogs.json 是我们想要写入到 Elasticsearch 中的文档。这个文档包含 Elastic Blog: Stories, Tutorials, Releases | Elastic Blog 的博客网站里的文档。我们使用如下的方法来摄入:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据 text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

从上面,我们可以看到共有326篇博客文章。点击上面的扩展图标:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

我们可以在 body_content_window 里查看到该文章的所有内容。

上传 NLP 模型 

接下来我们来上传模型。我们使用如下的命令:

docker run -it --rm elastic/eland \
    eland_import_hub_model \
      --url https://elastic:QfpoidDGfAfixQEikZW_@192.168.0.3:9200/ \
      --hub-model-id sentence-transformers/msmarco-MiniLM-L-12-v3 \
      --task-type text_embedding \
      --insecure \
      —-start 

说明:

  • https://elastic:QfpoidDGfAfixQEikZW_@192.168.0.3:9200/ 是我们的 Elasticsearch 的访问地址。其中 QfpoidDGfAfixQEikZW_ 是超级用户 elastic 的密码。在你的部署中,你需要根据自己的安装来进行修改
  • sentence-transformers/msmarco-MiniLM-L-12-v3 是在 huggingface.co 上可以找到的模型。

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

我们甚至可以在网页上进行一些测试。

  • task-type 是 text_embedding
  • --insecure:由于我们使用的是自签名的 Elasticsearch 部署,所以,我们可以使用这个选项来进行上传

如果你不想使用 --insecure 来上传,或者你有自己的证书,那么你可以使用如下的方式来进行上传:

docker run -it \
     -v /Users/liuxg/elastic/elasticsearch-8.3.3/config/certs/http_ca.crt:/usr/share/http_ca.crt \
     --rm elastic/eland \
     eland_import_hub_model \
        --url https://elastic:QfpoidDGfAfixQEikZW_@192.168.0.3:9200/ \
        --hub-model-id sentence-transformers/msmarco-MiniLM-L-12-v3 \
        --task-type text_embedding \
        --ca-cert /usr/share/http_ca.crt
        --start    

你也可以使用 API key 来进行上传。我们可以使用如下的步骤:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

docker run -it --rm elastic/eland \
    eland_import_hub_model \
      --url https://192.168.0.3:9200/ \
      --es-api-key N2N5Znc0SUJQVTcyQ3I2bmxid3U6WkpYR1JxX3JTdlNUajlXWk5vX2xZUQ== \
      --hub-model-id sentence-transformers/msmarco-MiniLM-L-12-v3 \
      --task-type text_embedding \
      --insecure \
      --start

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

从上面的输出中,我们可以看到模型已经成功地上传到 Elasticsearch 中了。

我们可以打开 Kibana 并进行查看:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

 按照同样的步骤,我们来上传另外一个模型 question_answering:

docker run -it --rm elastic/eland \
    eland_import_hub_model \
      --url https://192.168.0.3:9200/ \
      --es-api-key N2N5Znc0SUJQVTcyQ3I2bmxid3U6WkpYR1JxX3JTdlNUajlXWk5vX2xZUQ== \
      --hub-model-id deepset/tinyroberta-squad2 \
      --task-type question_answering \
      --insecure \
      --start

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据 text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

 至此,我们成功地上传了需要的模型。

使用模型

我们接下来创建 ingest pipeline 并使用模型。我们先创建一个 ingest pipeline:

# text embedding ingest  pipeline with inference model
PUT _ingest/pipeline/text-embeddings
{
  "description": "Text embedding pipeline",
  "processors": [
    {
      "inference": {
        "model_id": "sentence-transformers__msmarco-minilm-l-12-v3",
        "target_field": "text_embedding",
        "field_map": {
          "body_content_window": "text_field"
        }
      }
    }
  ],
  "on_failure": [
    {
      "set": {
        "description": "Index document to 'failed-<index>'",
        "field": "_index",
        "value": "failed-{{{_index}}}"
      }
    },
    {
      "set": {
        "description": "Set error message",
        "field": "ingest.failure",
        "value": "{{_ingest.on_failure_message}}"
      }
    }
  ]
}

我们接下来创建如下的一个索引:

# template mapping for dense vector field 
PUT blogs-with-embeddings
{
  "mappings": {
    "properties": {
      "text_embedding.predicted_value": {
        "type": "dense_vector",
        "dims": 384,
        "index": true,
        "similarity": "l2_norm"
      }
    }
  }
}

 请注意这里的 dims 为384。它是和 sentence-transformers/msmarco-MiniLM-L-12-v3 · Hugging Face 中定义的维度大小是一致的。

接下来,我们使用 reindex 把之前的 blogs 写入到另外一个索引中:

# Reindex to use text embedding pipeline - returns <task_id>
POST _reindex?wait_for_completion=false
{
  "source": {
    "index": "blogs"
  },
  "dest": {
    "index": "blogs-with-embeddings",
    "pipeline": "text-embeddings"
  }
}

在上面,我们定义了 pipeline 从而在 reindex 的过程中,它可以使用上面定义的 infererence 处理器。上面的命令将返回一个 taskid:

{
  "task" : "ViiAKJJMSEmig9qtGfb34g:635573"
}

我们可以通过如下的方式来查看 task 的进度:

GET _tasks/ViiAKJJMSEmig9qtGfb34g:635573

我们可以到机器学习的界面去查看进度:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

接下来,我们为新的索引 blogs-with-embeddings 创建一个 data view:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

我们接下来去 Discover 来查看这个索引:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

我们接下来使用这个最新创建的索引来进行一些搜索:

POST _ml/trained_models/sentence-transformers__msmarco-minilm-l-12-v3/deployment/_infer
{
  "docs": [{"text_field": "Find anomalies in location data"}]
}

上面的命令将返回如下的数据:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

上面的 predicted_value 是一个384 维的向量。之后,我们将生成的密集向量(dense vector)插入到 _knn_search 中,如下所示: 

GET blogs-with-embeddings/_knn_search
{
  "_source": ["body_content_window", "url"],
  "knn": {
    "field": "text_embedding.predicted_value",
    "k": 5,
    "num_candidates": 10,
    "query_vector": [
    0.0745367705821991,
    -0.2075425535440445,
    0.0939224511384964,
    -0.16857969760894775,
    0.1657467484474182,
       …
    ]
  }
}

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

我们可以查看一下上面搜索到的第一个文章的内容:

        "_source": {
          "body_content_window": " About the data This data is a collection of approximately 2 months of gtfs-realtime data streamed from San Antonio, TX. This data consists solely of the vehicle position updates with each bus position updated approximately every 5 min. The data is sparse during off hours (between 00:00-05:00) each day. Each individual bus follows deterministic, daily patterns. These patterns, for certain buses, follow deterministic seasonality that can vary day-to-day. The data consists of 2,882,527 unique events from 24-Apr-2019 to 13-Jun-2019. Data provided by VIA Metropolitan Transit . Creating the anomaly detection job To create an anomaly detection job in Kibana, click Create job on the Machine learning > Anomaly detection page and select the advanced job wizard. Alternatively, use the create anomaly detection jobs API . Use the lat_long function in a detector in your anomaly detection job for the pos field (a geo-point field) and set the by field to vehicle.id. This detects anomalies where the geographic location (pos) of a vehicle (a bus, in this case) is unusual for that particular bus (vehicle.id) . An anomaly might indicate a problem or unforeseen delay. You should then also select vehicle.id as an influencer for this job. For more information regarding influencers, see the documentation . Once you’ve created the job and it has produced some results, you can navigate to Elastic Maps to view them. Mapping anomalies by location in Elastic Maps Navigate to Elastic Maps and select Create Map, then select Add Layer. You should see the ML Anomalies layer card option which you can now select. You can now select from a list of jobs - only jobs with geo location data in the results will be in this list. Once you have a job selected, notice - in the screenshots below - that you can choose between a few layer options: ‘Actual’ , ‘Typical’ , and ‘Actual to typical’ . The ‘Actual’ layer is displayed by default until another is selected. This layer displays the actual geographical position of the associated entity on the map. As shown in the expanded legend in the above screenshot, the points are colored according to the severity level of the anomaly - this is determined by the record score of the anomaly.",

 从上面的内容中,我们可以看到 anomaly, Maps, Elastic Maps, geo 等字眼:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

 显然这个搜索不是基于我们常见的 token 匹配的那种搜索,而是基于语义的的搜索。

我们可以对搜索添加一个过滤器:

GET blogs-with-embeddings/_knn_search
{
  "_source": ["body_content_window", "url", "byline"],
  "knn": {
    "field": "text_embedding.predicted_value",
    "k": 10,
    "num_candidates": 10,
    "query_vector": [
    0.0745367705821991,
    -0.2075425535440445,
    0.0939224511384964,
    -0.16857969760894775,
    ...
    ]
  },
  "filter": {
    "term": {
      "byline.keyword": "Melissa Alvarez"
    }
  }
}

上面的搜索结果为:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

接下来,我们使用 question-answer 模型来对我们的文字进行提问。我们把上图中的 body_content_window 的内容拷贝下来并粘贴到如下的命令中:

#question/answering example
POST _ml/trained_models/deepset__tinyroberta-squad2/_infer
{
  "docs":[{"text_field": "TEXT"}],
  "inference_config": {
    "question_answering": {
        "question": "what type of anomaly detection job?",
        "max_answer_length": 30
     }
  }
}

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

 在上面的右边显示了问题的回答:

Machine learning > Anomaly detection page and select the advanced job wizard

 text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

 我们也可以使用如下的一个简单的例子来进行展示:

POST _ml/trained_models/deepset__tinyroberta-squad2/_infer
{
  "docs":[{ "text_field": "My name is Xiaoguo, Liu and I live in Beijing"}],
  "inference_config": {
    "question_answering": {
      "question": "Where do I live?"
    }
  }
}

你可以看到如下的结果:

{
  "inference_results": [
    {
      "predicted_value": "in Beijing",
      "start_offset": 35,
      "end_offset": 45,
      "prediction_probability": 0.0027769953663940796
    }
  ]
}

如果你想创建一个有 Web 界面的应用来进行测试,你可以参考链接 https://github.com/liu-xiao-guo/flask-elastic-nlp。请详细参考其中的 README.md 文件。最终我们将看到如下的 UI:

text embedding,Elasticsearch,Elastic,AI,自然语言处理,人工智能,elasticsearch,大数据

 这里就不再累述了。文章来源地址https://www.toymoban.com/news/detail-698739.html

到了这里,关于Elasticsearch:使用向量搜索来查询及比较文字 - NLP text embedding的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 向量数据库:使用Elasticsearch实现向量数据存储与搜索

    Here’s the table of contents:   Elasticsearch在7.x的版本中支持 向量检索 。在向量函数的计算过程中,会对所有匹配的文档进行线性扫描。因此,查询预计时间会随着匹配文档的数量线性增长。出于这个原因,建议使用查询参数来限制匹配文档的数量(类似二次查找的逻辑,先使

    2024年02月07日
    浏览(42)
  • 快速入门:使用 Gemini Embeddings 和 Elasticsearch 进行向量搜索

    Gemini 是 Google DeepMind 开发的多模态大语言模型家族,作为 LaMDA 和 PaLM 2 的后继者。由 Gemini Ultra、Gemini Pro 和 Gemini Nano 组成,于 2023 年 12 月 6 日发布,定位为 OpenAI 的竞争者 GPT-4。 本教程演示如何使用 Gemini API 创建嵌入并将其存储在 Elasticsearch 中。 Elasticsearch 将使我们能够执

    2024年01月21日
    浏览(31)
  • 使用 ElasticSearch 作为知识库,存储向量及相似性搜索

    在当今大数据时代,快速有效地搜索和分析海量数据成为了许多企业和组织的重要需求。 Elasticsearch 作为一款功能强大的分布式搜索和分析引擎,为我们提供了一种优秀的解决方案。除了传统的文本搜索, Elasticsearch 还引入了向量存储的概念,以实现更精确、更高效的相似性

    2024年02月10日
    浏览(35)
  • 使用 ElasticSearch 作为知识库,存储向量及相似性搜索_elasticsearch cosinesimilarity(1)

    下面基于上篇文章使用到的 Chinese-medical-dialogue-data 中文医疗对话数据作为知识内容进行实验。 本篇实验使用 ES 版本为: 7.14.0 二、Chinese-medical-dialogue-data 数据集 GitHub 地址如下: https://github.com/Toyhom/Chinese-medical-dialogue-data 数据分了 6 个科目类型: 数据格式如下所示: 其中

    2024年04月11日
    浏览(37)
  • 搜索中关于稀疏检索和稠密向量检索的召回效果比较

    不同检索方式说明 最近在做搜索召回提升相关的研究工作。对比了稀疏检索和稠密向量检索的效果。其中使用的搜索引擎为elasticsearch8.x版本。稀疏检索包括BM25的检索方式,以及es官方在8.8之后版本提供的稀疏向量模型的方式。稠密向量检索,是指借助机器学习的模型做文本

    2024年02月21日
    浏览(30)
  • Elasticsearch:使用查询规则(query rules)进行搜索

    在之前的文章 “Elasticsearch 8.10 中引入查询规则 - query rules”,我们详述了如何使用 query rules 来进行搜索。这个交互式笔记本将向你介绍如何使用官方 Elasticsearch Python 客户端来使用查询规则。 你将使用 query rules API 将查询规则存储在 Elasticsearch 中,并使用 rule_query 查询它们。

    2024年02月21日
    浏览(34)
  • Elasticsearch 向量相似搜索

    Elasticsearch 向量相似搜索的原理涉及使用密集向量(dense vector)来表示文档,并通过余弦相似性度量来计算文档之间的相似性。以下是 Elasticsearch 向量相似搜索的基本原理: 向量表示文档 : 文档的文本内容经过嵌入模型(如BERT、Word2Vec等)处理,得到一个密集向量(dense v

    2024年02月04日
    浏览(42)
  • Elasticsearch:运用向量搜索通过图像搜索找到你的小狗

    作者:ALEX SALGADO 你是否曾经遇到过这样的情况:你在街上发现了一只丢失的小狗,但不知道它是否有主人? 了解如何使用向量搜索或图像搜索来做到这一点。 您是否曾经遇到过这样的情况:你在街上发现了一只丢失的小狗,但不知道它是否有主人? 在 Elasticsearch 中通过图像

    2024年02月03日
    浏览(33)
  • Java开发者的Python快速实战指南:探索向量数据库之图像相似搜索-文字版

    首先,我要向大家道个歉。原本我计划今天向大家展示如何将图片和视频等形式转换为向量并存储在向量数据库中,但是当我查看文档时才发现,腾讯的向量数据库尚未完全开发完成。因此,今天我将用文本形式来演示相似图片搜索。如果您对腾讯的产品动态不太了解,可以

    2024年02月05日
    浏览(57)
  • 🔥🔥Java开发者的Python快速实战指南:探索向量数据库之图像相似搜索-文字版

    首先,我要向大家道个歉。原本我计划今天向大家展示如何将图片和视频等形式转换为向量并存储在向量数据库中,但是当我查看文档时才发现,腾讯的向量数据库尚未完全开发完成。因此,今天我将用文本形式来演示相似图片搜索。如果您对腾讯的产品动态不太了解,可以

    2024年02月05日
    浏览(55)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包