我们将会使用 MS MARCO Passage Ranking Task 提供的公共数据集进行演示。这个数据集包含了来自 Microsoft Bing 搜索引擎的真实问题和人工生成的答案,是测试矢量相似度搜索的绝佳资源;首先,因为回答问题是矢量搜索最常见的用例之一;其次,MS MARCO 排行榜中排名靠前的论文都以某种形式使用了矢量搜索。
第一步是安装文本嵌入模型。在我们的模型中,使用的是 Hugging Face 中的 msmarco-MiniLM-L-12-v3。这是一个句子转换程序模型,它会取一个句子或一个段落,并将其映射到一个 384 维的密集矢量。这个模型针对语义搜索进行了优化,并专门针对 MS MARCO Passage 数据集进行了训练,从而让它适合执行我们的任务。除了这个模型,Elasticsearch 还支持许多其他的文本嵌入模型。支持的完整列表可在此处查看。
我们使用在 NER 示例中构建的 Eland docker 代理来安装模型。运行下面的脚本,将我们的模型导入到本地集群中并进行部署:
我们使用 Kibana 的文件上传功能来上传这个数据集。通过 Kibana 文件上传,我们可以为字段提供定制名称。例如,我们将它们称为 ID 类型为 long 的段落 ID,以及 text 类型为 text 的段落内容。索引名称为 collection。上传完成后,我们可以看到一个名为 collection 的索引,其中包含 182,469 个文档。
在 Kibana 控制台中,我们创建了一个采集管道(操作方法请见上一篇博文),这次用于文本嵌入,故而称之为 text-embeddings。这些段落位于名为 text 的字段中。与之前一样,我们将会定义一个 field_map,以将文本映射到模型期望的字段 text_field。同样,将 on_failure 处理程序设为将故障索引到不同的索引中:
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":{"text":"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}}"}}]}
PUT collection-with-embeddings
{"mappings":{"properties":{"text_embedding.predicted_value":{"type":"dense_vector","dims":384,"index":true,"similarity":"cosine"},"text":{"type":"text"}}}}
终于,我们可以重建索引了。鉴于重建索引需要一些时间来处理所有文档并对它们进行推断,因此,我们会通过调用带有 wait_for_completion=false 标志的 API 在后台重建索引。
POST _reindex?wait_for_completion=false{"source":{"index":"collection"},"dest":{"index":"collection-with-embeddings","pipeline":"text-embeddings"}}
上述命令会返回一个任务 ID。我们可以通过以下方式来监控任务的进度:
GET _tasks/<task_id>
或者,也可以通过观察模型统计 API 或模型统计 UI 中 Inference count(推理计数)的增加来跟踪进度。
已重建索引的文档现在包含了推理结果 — 矢量嵌入。例如,其中一个文档如下图所示:
{"id":"G7PPtn8BjSkJO8zzChzT","text":"This is the definition of RNA along with examples of types of RNA molecules. This is the definition of RNA along with examples of types of RNA molecules. RNA Definition","text_embedding":{"predicted_value":[0.057356324046850204,0.1602816879749298,-0.18122544884681702,0.022277727723121643,....],"model_id":"sentence-transformers__msmarco-minilm-l-12-v3"}}
例如,给出一个文本查询“how is the weather in jamaica”(牙买加的天气怎么样),我们会首先运行 _infer API 以得到一个密集矢量的嵌入:
POST /_ml/trained_models/sentence-transformers__msmarco-minilm-l-12-v3/deployment/_infer
{"docs":{"text_field":"how is the weather in jamaica"}}
之后,我们将生成的密集矢量插入 _knn_search,如下图所示:
GET collection-with-embeddings/_knn_search
{"knn":{"field":"text_embedding.predicted_value","query_vector":[0.3345310091972351,-0.305600643157959,0.2592800557613373,…],"k":10,"num_candidates":100},"_source":["id","text"]}
结果,我们得到了最接近查询文档的前 10 个文档,按它们与查询的接近程度排序:
"hits":[{"_index":"collection-with-embeddings","_id":"47TPtn8BjSkJO8zzKq_o","_score":0.94591534,"_source":{"id":434125,"text":"The climate in Jamaica is tropical and humid with warm to hot temperatures all year round. The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit. Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year. Continue Reading."}},{"_index":"collection-with-embeddings","_id":"3LTPtn8BjSkJO8zzKJO1","_score":0.94536424,"_source":{"id":4498474,"text":"The climate in Jamaica is tropical and humid with warm to hot temperatures all year round. The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit. Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year"}},{"_index":"collection-with-embeddings","_id":"KrXPtn8BjSkJO8zzPbDW","_score":0.9432083,"_source":{"id":190804,"text":"Quick Answer. The climate in Jamaica is tropical and humid with warm to hot temperatures all year round. The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit. Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year. Continue Reading"}},...
6.快速验证
由于我们只使用了 MS MARCO 数据集的一个子集,因此我们无法进行全面评估。但是,我们可以对一些查询进行简单的验证,以确定我们确实得到了相关的结果,而不是一些随机的结果。从 TREC 2019 Deep Learning Track 对“段落排名任务”的判断中,我们选取最后 3 个查询,将它们提交到我们的矢量相似度搜索,获得前 10 个结果并参考 TREC 判断,看一看我们所收到结果的相关性如何。在段落排名任务中,段落的评分标准分为四个等级:不相关 (0)、相关(段落切题但没有回答问题)(1)、高度相关 (2) 和完全相关 (3)。
请注意,我们的验证不是严格的评估,验证结果仅用于快速演示。由于我们只对已知与查询相关的段落进行索引,因此这比原始段落检索任务要容易得多。未来我们会打算对 MS MARCO 数据集进行严格的评估。
将查询 #1124210“tracheids are part of _____”(管胞属于 _____)提交给我们的矢量搜索,返回了以下结果:
段落 ID
相关性评分
段落
2258591
2 - 高度相关
Tracheid of oak shows pits along the walls.It is longer than a vessel element and has no perforation plates.Tracheids are elongated cells in the xylem of vascular plants that serve in the transport of water and mineral salts.Tracheids are one of two types of tracheary elements, vessel elements being the other.Tracheids, unlike vessel elements, do not have perforation plates.racheids provide most of the structural support in softwoods, where they are the major cell type.Because tracheids have a much higher surface to volume ratio compared to vessel elements, they serve to hold water against gravity (by adhesion) when transpiration is not occurring.
2258592
3 - 完全相关
Tracheid. a dead lignified plant cell that functions in water conduction.Tracheids are found in the xylem of all higher plants except certain angiosperms, such as cereals and sedges, in which the water-conducting function is performed by vessels, or tracheae.Tracheids are usually polygonal in cross section; their walls have annular, spiral, or scalene thickenings or rimmed pores.racheids are found in the xylem of all higher plants except certain angiosperms, such as cereals and sedges, in which the water-conducting function is performed by vessels, or tracheae.Tracheids are usually polygonal in cross section; their walls have annular, spiral, or scalene thickenings or rimmed pores.
2258596
2 - 高度相关
Woody angiosperms have also vessels.The mature tracheids form a column of superposed, cylindrical dead cells whose end walls have been perforated, resulting in a continuous tube called vessel (trachea).Tracheids are found in all vascular plants and are the only conducting elements in gymnosperms and ferns.Tracheids have Pits on their end walls.Pits are not nearly as efficient for water translocation as Perforation Plates found in vessel elements.Woody angiosperms have also vessels.The mature tracheids form a column of superposed, cylindrical dead cells whose end walls have been perforated, resulting in a continuous tube called vessel (trachea).Tracheids are found in all vascular plants and are the only conducting elements in gymnosperms and ferns
2258595
2 - 高度相关
Summary:Vessels have perforations at the end plates while tracheids do not have end plates.Tracheids are derived from single individual cells while vessels are derived from a pile of cells.Tracheids are present in all vascular plants whereas vessels are confined to angiosperms.Tracheids are thin whereas vessel elements are wide.Tracheids have a much higher surface-to-volume ratio as compared to vessel elements.Vessels are broader than tracheids with which they are associated.Morphology of the perforation plate is different from that in tracheids.Tracheids are thin whereas vessel elements are wide.Tracheids have a much higher surface-to-volume ratio as compared to vessel elements.Vessels are broader than tracheids with which they are associated.Morphology of the perforation plate is different from that in tracheids.
131190
3 - 完全相关
Xylem tracheids are pointed, elongated xylem cells, the simplest of which have continuous primary cell walls and lignified secondary wall thickenings in the form of rings, hoops, or reticulate networks.
7443586
2 - 高度相关
1 The xylem tracheary elements consist of cells known as tracheids and vessel members, both of which are typically narrow, hollow, and elongated.Tracheids are less specialized than the vessel members and are the only type of water-conducting cells in most gymnosperms and seedless vascular plants.
181177
2 - 高度相关
In most plants, pitted tracheids function as the primary transport cells.The other type of tracheary element, besides the tracheid, is the vessel element.Vessel elements are joined by perforations into vessels.In vessels, water travels by bulk flow, as in a pipe, rather than by diffusion through cell membranes.
2947055
0 - 不相关
Cholesterol belongs to the groups of lipids called _______.holesterol belongs to the groups of lipids called _______.
6541866
2 - 高度相关
In most plants, pitted tracheids function as the primary transport cells.The other type of tracheary element, besides the tracheid, is the vessel element.Vessel elements are joined by perforations into vessels.In vessels, water travels by bulk flow, as in a pipe, rather than by diffusion through cell membranes.In most plants, pitted tracheids function as the primary transport cells.The other type of tracheary element, besides the tracheid, is the vessel element.Vessel elements are joined by perforations into vessels.In vessels, water travels by bulk flow, as in a pipe, rather than by diffusion through cell membranes.
查询 #1129237“hydrogen is a liquid below what temperature”返回了以下结果:
段落 ID
相关性评分
段落
8588222
0 - 不相关
回答:Hydrogen is a liquid below what temperature?By signing up, you'll get thousands of step-by-step solutions to your homework questions.... for Teachers for Schools for Companies
128984
3 - 完全相关
Hydrogen gas has the molecular formula H 2.At room temperature and under standard pressure conditions, hydrogen is a gas that is tasteless, odorless and colorless.Hydrogen can exist as a liquid under high pressure and an extremely low temperature of 20.28 kelvin (−252.87°C, −423.17 °F).Hydrogen is often stored in this way as liquid hydrogen takes up less space than hydrogen in its normal gas form.Liquid hydrogen is also used as a rocket fuel.
8588219
3 - 完全相关
User:Hydrogen is a liquid below what temperature? a.100°C;c. -183°C;b. -253°C;d.0°C Weegy:0 degrees C Weegy:Hydrogen is a liquid below 253 degrees C. User:What is the boiling point of oxygen? a.100 degrees C c. -57 degrees C b.8 degrees C d. -183 degrees C Weegy:The boiling point of oxygen is -183 degrees C.
3905057
3 - 完全相关
Hydrogen is a colorless, odorless, tasteless gas.Its density is the lowest of any chemical element, 0.08999 grams per liter.By comparison, a liter of air weighs 1.29 grams, 14 times as much as a liter of hydrogen.Hydrogen changes from a gas to a liquid at a temperature of -252.77°C (-422.99°F) and from a liquid to a solid at a temperature of -259.2°C (-434.6°F).It is slightly soluble in water, alcohol, and a few other common liquids.
4254811
3 - 完全相关
At STP (standard temperature and pressure) hydrogen is a gas.It cools to a liquid at -423 °F, which is only about 37 degrees above absolute zero.Eleven degrees cooler, at … -434 °F, it starts to solidify.
2697752
2 - 高度相关
Hydrogen's state of matter is gas at standard conditions of temperature and pressure.Hydrogen condenses into a liquid or freezes solid at extremely cold...Hydrogen's state of matter is gas at standard conditions of temperature and pressure.Hydrogen condenses into a liquid or freezes solid at extremely cold temperatures.Hydrogen's state of matter can change when the temperature changes, becoming a liquid at temperatures between minus 423.18 and minus 434.49 degrees Fahrenheit.It becomes a solid at temperatures below minus 434.49 F.Due to its high flammability, hydrogen gas is commonly used in combustion reactions, such as in rocket and automobile fuels.
6080460
3 - 完全相关
Hydrogen can exist as a liquid under high pressure and an extremely low temperature of 20.28 kelvin (−252.87°C, −423.17 °F).Hydrogen is often stored in this way as liquid hydrogen takes up less space than hydrogen in its normal gas form.Liquid hydrogen is also used as a rocket fuel.Hydrogen is found in large amounts in giant gas planets and stars, it plays a key role in powering stars through fusion reactions.Hydrogen is one of two important elements found in water (H 2 O).Each molecule of water is made up of two hydrogen atoms bonded to one oxygen atom.
128989
3 - 完全相关
Confidence votes 11.4K.At STP (standard temperature and pressure) hydrogen is a gas.It cools to a liquid at -423 °F, which is only about 37 degrees above absolute zero.Eleven degrees cooler, at -434 °F, it starts to solidify.
1959030
0 - 不相关
While below 4 °C the breakage of hydrogen bonds due to heating allows water molecules to pack closer despite the increase in the thermal motion (which tends to expand a liquid), above 4 °C water expands as the temperature increases.Water near the boiling point is about 4% less dense than water at 4 °C (39 °F)
3905800
0 - 不相关
Hydrogen is the lightest of the elements with an atomic weight of 1.0.Liquid hydrogen has a density of 0.07 grams per cubic centimeter, whereas water has a density of 1.0 g/cc and gasoline about 0.75 g/cc.These facts give hydrogen both advantages and disadvantages.
查询 #1133167“how is the weather in jamaica”返回了以下结果:
段落 ID
相关性评分
段落
434125
3 - 完全相关
The climate in Jamaica is tropical and humid with warm to hot temperatures all year round.The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit.Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year.
4498474
3 - 完全相关
The climate in Jamaica is tropical and humid with warm to hot temperatures all year round.The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit.Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year.
190804
3 - 完全相关
Quick Answer.The climate in Jamaica is tropical and humid with warm to hot temperatures all year round.The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit.Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year.继续阅读。Continue Reading.
1824479
3 - 完全相关
A:The climate in Jamaica is tropical and humid with warm to hot temperatures all year round.The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit.Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year.
1824480
3 - 完全相关
Quick Answer.The climate in Jamaica is tropical and humid with warm to hot temperatures all year round.The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit.Jamaican nights are considerably cooler than the days, and the mountain areas are cooler than the lower land throughout the year.
1824488
2 - 高度相关
Learn About the Weather of Jamaica The weather patterns you'll encounter in Jamaica can vary dramatically around the island Regardless of when you visit, the tropical climate and warm temperatures of Jamaica essentially guarantee beautiful weather during your vacation.Average temperatures in Jamaica range between 80 degrees Fahrenheit and 90 degrees Fahrenheit, with July and August being the hottest months and February the coolest.
4922619
2 - 高度相关
Weather.Jamaica averages about 80 degrees year-round, so climate is less a factor in booking travel than other destinations.The days are warm and the nights are cool.Rain usually falls for short periods in the late afternoon, with sunshine the rest of the day.
190806
2 - 高度相关
It is always important to know what the weather in Jamaica will be like before you plan and take your vacation.For the most part, the average temperature in Jamaica is between 80 °F and 90 °F (27 °FCelsius-29 °Celsius).Luckily, the weather in Jamaica is always vacation friendly.You will hardly experience long periods of rain fall, and you will become accustomed to weeks upon weeks of sunny weather.
2613296
2 - 高度相关
Average temperatures in Jamaica range between 80 degrees Fahrenheit and 90 degrees Fahrenheit, with July and August being the hottest months and February the coolest.Temperatures in Jamaica generally vary approximately 10 degrees from summer to winter
1824486
2 - 高度相关
The climate in Jamaica is tropical and humid with warm to hot temperatures all year round.The average temperature in Jamaica is between 80 and 90 degrees Fahrenheit.Jamaican nights are considerably...