Elastic 提供了一个强大的 ELSER 供我们进行语义搜索。ELSER 是一种稀疏向量的搜索方法。我们无需对它做任何的微调及训练。它是一种 out-of-domain 的模型。目前它仅对英文 进行支持。希望将来它能对其它的语言支持的更好。更多关于 ELSER 的知识,请参阅文章 "Elasticsearch:使用 ELSER 释放语义搜索的力量:Elastic Learned Sparse EncoderR"。在本文中,我们将使用第二版的 ELSER 来进行语义搜索。我将使用 Jupyter notebook 演示如何使用 ELSER 模型 .elser_model_2 模型,该模型提供了更高的检索精度。
如果你已使用 ELSER 模型 .elser_model_1 设置索引,并且想要升级到 ELSER v2 模型 - .elser_model_2,请按照文章升级索引以使用 elser 模型的说明进行操作 来进行升级。
安装
如果你还没有安装好自己的 Elasticsearch 及 Kibana,请参考文章:
安装 Elasticsearch 及 Kibana
如果你还没有安装好自己的 Elasticsearch 及 Kibana,那么请参考一下的文章来进行安装:
在安装的时候,请选择 Elastic Stack 8.x 进行安装。在安装的时候,我们可以看到如下的安装信息:
为了能够上传向量模型,我们必须订阅白金版或试用。
Python
我们需要安装相应的 Elasticsearch 包:
markdown
1. $ pwd
2. /Users/liuxg/python/elser
3. $ pip3 install elasticsearch -qU
4. $ pip3 list | grep elasticseach
5. elasticsearch 8.11.1
6. rag-elasticsearch 0.0.1 /Users/liuxg/python/rag-elasticsearch/my-app/packages/rag-elasticsearch
环境变量
在启动 Jupyter 之前,我们设置如下的环境变量:
ini
1. export ES_USER="elastic"
2. export ES_PASSWORD="yarOjyX5CLqTsKVE3v*d"
3. export ES_ENDPOINT="localhost"
拷贝 Elasticsearch 证书
我们把 Elasticsearch 的证书拷贝到当前的目录下:
markdown
1. $ pwd
2. /Users/liuxg/python/elser
3. $ cp ~/elastic/elasticsearch-8.11.0/config/certs/http_ca.crt .
4. $ ls
5. find_books_about_christmas_without_searching_for_christmas.ipynb
6. Chatbot with LangChain conversational chain and OpenAI.ipynb
7. ElasticKnnSearch.ipynb
8. ElasticVectorSearch.ipynb
9. ElasticsearchStore.ipynb
10. Mental Health FAQ.ipynb
11. Multilingual semantic search.ipynb
12. NLP text search using hugging face transformer model.ipynb
13. Question Answering with Langchain and OpenAI.ipynb
14. RAG-langchain-elasticsearch.ipynb
15. Semantic search - ELSER.ipynb
16. Semantic search quick start.ipynb
17. book_summaries_1000_chunked.json
18. books.json
19. data.json
20. http_ca.crt
21. lib
22. sample_data.json
23. upgrading-index-to-use-elser.ipynb
24. vector_search_implementation_guide_api.ipynb
25. workplace-docs.json
在上面,我们把 Elasticsearch 的证书 http_ca.crt 拷贝到当前的目录下。
运行应用
连接到 Elasticsearch
ini
1. from elasticsearch import Elasticsearch
2. import os
4. elastic_user=os.getenv('ES_USER')
5. elastic_password=os.getenv('ES_PASSWORD')
6. elastic_endpoint=os.getenv("ES_ENDPOINT")
8. url = f"https://{elastic_user}:{elastic_password}@{elastic_endpoint}:9200"
9. es = Elasticsearch(url, ca_certs = "./http_ca.crt", verify_certs = True)
11. print(es.info())
从上面的输出中,我们可以看到,连接到 Elasticsearch 是成功的。
如果你对如何连接到 Elasticsearch 还不是很熟悉的话,那么请阅读文章 "Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x"。
下载及部署 ELSER 模型
下面,我们来尝试通过软件的方式来针对 ELSER 进行手动部署。在此示例中,我们将下载 ELSER 模型并将其部署到 ML 节点中。 确保你有一个 ML 节点才能运行 ELSER 模型。如果你之前已经下载过,我们通过软件的方式来进行删除,并安装最新的模型:
python
1. # delete model if already downloaded and deployed
2. try:
3. es.ml.delete_trained_model(model_id=".elser_model_2",force=True)
4. print("Model deleted successfully, We will proceed with creating one")
5. except exceptions.NotFoundError:
6. print("Model doesn't exist, but We will proceed with creating one")
8. # Creates the ELSER model configuration. Automatically downloads the model if it doesn't exist.
9. es.ml.put_trained_model(
10. model_id=".elser_model_2",
11. input={
12. "field_names": ["text_field"]
13. }
14. )
我们回到 Kibana 的界面中进行查看:
上面显示, .elser_model_2 正在被下载。我们需要等一段时间才能下载完毕。这个依赖于你自己的网路速度。使用以下命令检查模型下载的状态。
lua
1. while True:
2. status = es.ml.get_trained_models(
3. model_id=".elser_model_2",
4. include="definition_status"
5. )
7. if (status["trained_model_configs"][0]["fully_defined"]):
8. print("ELSER Model is downloaded and ready to be deployed.")
9. break
10. else:
11. print("ELSER Model is downloaded but not ready to be deployed.")
12. time.sleep(5)
在 Kibana 中显示的状态为:
下载完模型后,我们可以将模型部署到 ML 节点中。 使用以下命令部署模型。
ini
1. import time
3. # Start trained model deployment if not already deployed
4. es.ml.start_trained_model_deployment(
5. model_id=".elser_model_2",
6. number_of_allocations=1,
7. wait_for="starting"
8. )
如上所示,在 Kibana 的界面中,我们可以看到 .elser_model_2 已经被成功地部署了。我们可以使用如下的代码来查看状态:
lua
1. while True:
2. status = es.ml.get_trained_models_stats(
3. model_id=".elser_model_2",
4. )
5. if (status["trained_model_stats"][0]["deployment_stats"]["state"] == "started"):
6. print("ELSER Model has been successfully deployed.")
7. break
8. else:
9. print("ELSER Model is currently being deployed.")
10. time.sleep(5)
摄入一些文档到 Elasticsearch
为了在我们的 Elasticsearch 中使用 ELSER,我们需要创建一个包含运行 ELSER 模型的推理处理器的摄取管道。 让我们使用 put_pipeline 方法添加该管道。
ini
1. es.ingest.put_pipeline(
2. id="elser-ingest-pipeline",
3. description="Ingest pipeline for ELSER",
4. processors=[
5. {
6. "inference": {
7. "model_id": ".elser_model_2",
8. "input_output": [
9. {
10. "input_field": "plot",
11. "output_field": "plot_embedding"
12. }
13. ]
14. }
15. }
16. ]
17. )
让我们记下该 API 调用中的一些重要参数:
- inference:使用机器学习模型执行推理的处理器。
- model_id:指定要使用的机器学习模型的 ID。 在此示例中,模型 ID 设置为 .elser_model_2。
- input_output:指定输入和输出字段
- input_field:创建稀疏向量表示的字段名称。
- output_field:包含推理结果的字段名称。
创建索引
要在索引时使用 ELSER 模型,我们需要创建支持 text_expansion 查询的索引映射。 该映射包括一个 sparse_vector 类型的字段,用于处理我们感兴趣的特征向量。 该字段包含 ELSER 模型根据输入文本创建的 token-weight 对。
让我们使用我们需要的映射创建一个名为 elser-example-movies 的索引。
perl
1. es.indices.delete(index="elser-example-movies", ignore_unavailable=True)
2. es.indices.create(
3. index="elser-example-movies",
4. settings={
5. "index": {
6. "default_pipeline": "elser-ingest-pipeline"
7. }
8. },
9. mappings={
10. "properties": {
11. "plot": {
12. "type": "text",
13. "fields": {
14. "keyword": {
15. "type": "keyword",
16. "ignore_above": 256
17. }
18. }
19. },
20. "plot_embedding": {
21. "type": "sparse_vector"
22. }
23. }
24. }
25. )
摄入文档
让我们插入 12 部电影的示例数据集。
如果出现错误,请检查模型是否已部署并且在 ML 节点中可用。 在较新版本的 Elastic Cloud 中,ML 节点是自动缩放的,并且 ML 节点可能尚未准备好。 等待几分钟,然后重试。在进行下面的运行之前,我们先在项目的根目录下创建如下的一个 movies.json 文档:
movies.json
css
1. [2. {3. "title": "Pulp Fiction",4. "runtime": "154",5. "plot": "The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.",6. "keyScene": "John Travolta is forced to inject adrenaline directly into Uma Thurman's heart after she overdoses on heroin.",7. "genre": "Crime, Drama",8. "released": "1994"9. },10. {11. "title": "The Dark Knight",12. "runtime": "152",13. "plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.",14. "keyScene": "Batman angrily responds 'I'm Batman' when asked who he is by Falcone.",15. "genre": "Action, Crime, Drama, Thriller",16. "released": "2008"17. },18. {19. "title": "Fight Club",20. "runtime": "139",21. "plot": "An insomniac office worker and a devil-may-care soapmaker form an underground fight club that evolves into something much, much more.",22. "keyScene": "Brad Pitt explains the rules of Fight Club to Edward Norton. The first rule of Fight Club is: You do not talk about Fight Club. The second rule of Fight Club is: You do not talk about Fight Club.",23. "genre": "Drama",24. "released": "1999"25. },26. {27. "title": "Inception",28. "runtime": "148",29. "plot": "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into thed of a C.E.O.",30. "keyScene": "Leonardo DiCaprio explains the concept of inception to Ellen Page by using a child's spinning top.",31. "genre": "Action, Adventure, Sci-Fi, Thriller",32. "released": "2010"33. },34. {35. "title": "The Matrix",36. "runtime": "136",37. "plot": "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.",38. "keyScene": "Red pill or blue pill? Morpheus offers Neo a choice between the red pill, which will allow him to learn the truth about the Matrix, or the blue pill, which will return him to his former life.",39. "genre": "Action, Sci-Fi",40. "released": "1999"41. },42. {43. "title": "The Shawshank Redemption",44. "runtime": "142",45. "plot": "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.",46. "keyScene": "Andy Dufresne escapes from Shawshank prison by crawling through a sewer pipe.",47. "genre": "Drama",48. "released": "1994"49. },50. {51. "title": "Goodfellas",52. "runtime": "146",53. "plot": "The story of Henry Hill and his life in the mob, covering his relationship with his wife Karen Hill and his mob partners Jimmy Conway and Tommy DeVito in the Italian-American crime syndicate.",54. "keyScene": "Joe Pesci's character Tommy DeVito shoots young Spider in the foot for not getting him a drink.",55. "genre": "Biography, Crime, Drama",56. "released": "1990"57. },58. {59. "title": "Se7en",60. "runtime": "127",61. "plot": "Two detectives, a rookie and a veteran, hunt a serial killer who uses the seven deadly sins as his motives.",62. "keyScene": "Brad Pitt's character David Mills shoots John Doe after he reveals that he murdered Mills' wife.",63. "genre": "Crime, Drama, Mystery, Thriller",64. "released": "1995"65. },66. {67. "title": "The Silence of the Lambs",68. "runtime": "118",69. "plot": "A young F.B.I. cadet must receive the help of an incarcerated and manipulative cannibal killer to help catch another serial killer, a madman who skins his victims.",70. "keyScene": "Hannibal Lecter explains to Clarice Starling that he ate a census taker's liver with some fava beans and a nice Chianti.",71. "genre": "Crime, Drama, Thriller",72. "released": "1991"73. },74. {75. "title": "The Godfather",76. "runtime": "175",77. "plot": "An organized crime dynasty's aging patriarch transfers control of his clandestine empire to his reluctant son.",78. "keyScene": "James Caan's character Sonny Corleone is shot to death at a toll booth by a number of machine gun toting enemies.",79. "genre": "Crime, Drama",80. "released": "1972"81. },82. {83. "title": "The Departed",84. "runtime": "151",85. "plot": "An undercover cop and a mole in the police attempt to identify each other while infiltrating an Irish gang in South Boston.",86. "keyScene": "Leonardo DiCaprio's character Billy Costigan is shot to death by Matt Damon's character Colin Sullivan.",87. "genre": "Crime, Drama, Thriller",88. "released": "2006"89. },90. {91. "title": "The Usual Suspects",92. "runtime": "106",93. "plot": "A sole survivor tells of the twisty events leading up to a horrific gun battle on a boat, which began when five criminals met at a seemingly random police lineup.",94. "keyScene": "Kevin Spacey's character Verbal Kint is revealed to be the mastermind behind the crime, when his limp disappears as he walks away from the police station.",95. "genre": "Crime, Mystery, Thriller",96. "released": "1995"97. }98. ]
markdown
1. $ pwd
2. /Users/liuxg/python/elser
3. $ ls Install\ ELSER.ipynb
4. Install ELSER.ipynb
5. $ ls movies.json
6. movies.json
python
1. import json
2. from elasticsearch import helpers
4. with open('movies.json') as f:
5. data_json = json.load(f)
7. # Prepare the documents to be indexed
8. documents = []
9. for doc in data_json:
10. documents.append({
11. "_index": "elser-example-movies",
12. "_source": doc,
13. })
15. # Use helpers.bulk to index
16. helpers.bulk(es, documents)
18. print("Done indexing documents into `elser-example-movies` index!")
19. time.sleep(3)
检查新文档以确认它现在有一个 plot_embedding 字段,其中包含新的附加术语列表。 这些术语是创建管道时在 input_field 中用于 ELSER 推理的目标字段的文本扩展。 ELSER 实质上创建了一个扩展术语树,以提高文档的语义可搜索性。 我们将能够使用 text_expansion 查询来搜索这些文档。我们可以在 Kibana 中查看:
但首先让我们从简单的关键字搜索开始,看看 ELSER 如何提供开箱即用的语义相关结果。
搜索文档
ini
1. response = es.search(
2. index='elser-example-movies',
3. size=3,
4. query={
5. "text_expansion": {
6. "plot_embedding": {
7. "model_id":".elser_model_2",
8. "model_text":"fighting movie"
9. }
10. }
11. }
12. )
14. for hit in response['hits']['hits']:
15. doc_id = hit['_id']
16. score = hit['_score']
17. title = hit['_source']['title']
18. plot = hit['_source']['plot']
19. print(f"Score: {score}\nTitle: {title}\nPlot: {plot}\n")
上面的所有源码可以在地址 github.com/liu-xiao-gu... 进行下载。
下一步
现在我们有了使用 ELSER 进行语义搜索的工作示例,你可以在自己的数据上尝试一下。