日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

通過ES機器學習實現智能問答

本文使用文本嵌入模型(text_embedding)對用戶查詢進行深度語義解析,突破傳統關鍵詞匹配的局限,從海量數據中準確提取高度相關的內容。使用問答模型(question_answering)對關聯文本進行精細解讀,精準抽取答案,回答與文本相關的問題。文本嵌入模型和問答模型的組合可以用于智能搜索引擎、個性化推薦等場景,可以提升信息檢索和問題解答的精準度。

準備工作

上傳模型

本文選擇huggingface倉庫中的question_answering模型luhua/chinese_pretrain_mrc_macbert_large和text_embedding模型thenlper/gte-large-zh。將模型上傳到阿里云ES中,請參見通過Eland上傳第三方NLP模型

由于中國內地網絡訪問huggingface較慢,本文采用離線上傳模型的方式。

  1. 下載模型。

  2. 將模型上傳到ECS中。

  3. 在ECS中執行如下命令,在模型文件目錄下解壓模型。

    cd /model/
    tar -xzvf luhua--chinese_pretrain_mrc_macbert_large.tar.gz
    tar -xzvf thenlper--gte-large-zh.tar.gz
    cd
  4. 在ECS中執行如下命令,將模型上傳到ES中。

    1. 上傳問答模型luhua--chinese_pretrain_mrc_macbert_large:

      eland_import_hub_model       
      --url 'http://es-cn-lbj3l7erv0009****.elasticsearch.aliyuncs.com:9200'       
      --hub-model-id '/model/root/.cache/huggingface/hub/models--luhua--chinese_pretrain_mrc_macbert_large/snapshots/f2d95d06f16a3043002c9702f66c834f4e0aa944'       
      --task-type question_answering       
      --es-username elastic       
      --es-password  ****       
      --es-model-id models--luhua--chinese_pretrain_mrc_macbert_large \
    2. 上傳文本嵌入模型thenlper--gte-large-zh:

      eland_import_hub_model       
      --url 'http://es-cn-lbj3l7erv0009****.elasticsearch.aliyuncs.com:9200'       
      --hub-model-id '/model/root/.cache/huggingface/hub/models--thenlper--gte-large-zh/snapshots/952432e6b99137bbfd8397d5ad92f920be5f22e9'       
      --task-type text_embedding       
      --es-username elastic       
      --es-password  ****       
      --es-model-id models--thenlper--gte-large-zh \

部署模型

  1. 登錄Kibana。具體操作,請參見登錄Kibana控制臺

  2. 單擊Kibana頁面左上角的image圖標,選擇Analytics > Machine Learning

  3. 在左側菜單欄,單擊模型管理(Model Management) > 已訓練模型(Trained Models)。

  4. (可選)在頁面上方,單擊同步作業和已訓練模型(Synchronize your jobs and trained models),在彈出的面板中單擊同步(Synchronize)。

  5. 將鼠標移動到目標模型操作(Actions)列的前面,單擊image圖標,啟動模型。

    image

  6. 在彈出的對話框中,配置模型后,單擊啟動(Start)。

    頁面右下角彈出已成功啟動的提示對話框,表明模型部署成功。

    說明

    模型無法啟動可能是集群內存不足,升配集群后再試。無法啟動的具體原因,請在提示對話框中單擊請參閱完整的錯誤信息查看。

驗證模型的可用性

  1. 單擊Kibana頁面左上角的image圖標,選擇Management > 開發工具(Dev Tools)。

  2. 驗證模型的可用性

    • 執行以下命令,驗證問答模型models--luhua--chinese_pretrain_mrc_macbert_large的可用性。

      POST /_ml/trained_models/models--luhua--chinese_pretrain_mrc_macbert_large/_infer
      {
       "docs":[{"text_field": "沒有必要。安裝兩個殺毒軟件可以增強電腦的安全性,但是會導致以下弊端: 占用硬盤資源過多; 在同時啟用兩款軟件的實時防護時,會導致系統硬件資源大量損耗,例如CPU處理過多,導致計算機工作能力下降; >在進行殺毒時,會導致系統占用資源過多,不但可能影響系統工作能力,還可能誤報對方組件,導致.."}],
       "inference_config": {"question_answering": {"question": "電腦可以同時安裝多個殺毒軟件嗎"}}
      }
      
      

      返回結果如下所示,結果符合預期。

      {
        "inference_results": [
          {
            "predicted_value": "沒有必要",
            "start_offset": 0,
            "end_offset": 4,
            "prediction_probability": 0.8775923604355836
          }
        ]
      }
    • 執行以下命令,驗證文本嵌入模型models--thenlper--gte-large-zh的可用性。

      POST /_ml/trained_models/models--thenlper--gte-large-zh/_infer
      {
        "docs":[{"text_field": "沒有必要。安裝兩個殺毒軟件可以增強電腦的安全性,但是會導致以下弊端: 占用硬盤資源過多; 在同時啟用兩款軟件的實時防護時,會導致系統硬件資源大量損耗,例如CPU處理過多,導致計算機工作能力下降; >在進行殺毒時,會導致系統占用資源過多,不但可能影響系統工作能力,還可能誤報對方組件,導致.."}]
      }

      部分返回結果如下所示,結果符合預期。

      {
        "inference_results": [
          {
            "predicted_value": [
              1.389997959136963,
              -0.6398589611053467,
              -0.5746312737464905,
              -0.5629222393035889,
              -0.49914881587028503,
              0.5277091264724731,
              -1.2194437980651855,
              0.19847321510314941,
              ..............
              0.6711148619651794,
              1.6224931478500366,
              2.0970489978790283,
              -0.4506820738315582,
              -0.298383504152298
            ]
          }
        ]
      }

準備數據集

索引數據時需要示例數據集。

  1. 下載示例數據集。

    文本以extracted_data.json數據集為例,請參見extracted_data.json

  2. 將數據集上傳到ECS中,例如文本將數據集上傳到/model/目錄下。

步驟一:索引數據

  1. 通過ingest pipeline在索引中配置模型,以便在數據寫入過程中完成文本轉換。

    說明

    針對索引已存儲數據需要應用模型完成向量轉換,請參見reindex方式重建索引

    PUT _ingest/pipeline/text-embedding-pipeline
    {
      "processors": [
        {
          "inference": {
            "model_id": "models--thenlper--gte-large-zh",
            "target_field": "text_embedding",
            "field_map": {
              "context": "text_field"
            }
          }
        }
      ]
    }
    
    #####創建索引mapping,指定ingest pipeline
    PUT question_answering
    {
      "settings": {
        "index": {
              "number_of_shards": 2,
              "number_of_replicas": 1,
              "default_pipeline": "text-embedding-pipeline"
          }
      }, 
      "mappings": {
        "properties": {
          "text_embedding.predicted_value": {
            "type": "dense_vector",
            "dims": 1024,
            "index": true,
            "similarity": "l2_norm"
          },
          "context": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  2. 通過Python導入準備的數據集(python 3.10):

    # -*- coding:utf-8 -*-
    import json
    from pprint import pprint
    import os
    import time
    from elasticsearch import helpers
    import time
    from elasticsearch import Elasticsearch
    
    elastic_user="elastic"
    elastic_password="****"
    elastic_endpoint="es-cn-lbj3l7erv0009****.elasticsearch.aliyuncs.com"
    url = f"http://{elastic_user}:{elastic_password}@{elastic_endpoint}:9200"
    es = Elasticsearch(url)
    print(es.info())
    with open('/model/extracted_data.json') as f:
     data_json = json.load(f)
    
    # Prepare the documents to be indexed
    documents = []
    for doc in data_json:
     documents.append({
     "_index": "question_answering",
     "_source": doc,
     })
    
    # Use helpers.bulk to index
    helpers.bulk(es, documents)
    
    print("Done indexing documents into `question_answering` index!")
    time.sleep(5)

步驟二、通過kibana檢索

以“阿里云Elasticsearch應用場景有哪些”問題為例,在kibana上進行檢索演示:

  1. 單擊Kibana頁面左上角的image圖標,選擇Management > 開發工具(Dev Tools)。

  2. 調用阿里云ES機器學習推理接口,將文檔轉換為向量數據。

    ES 8.7及以上版本

    ES 8.7及以上版本可以通過query_vector_builder構建。

    GET question_answering/_search
    {
      "_source": ["context","title"],
      "knn": {
        "field": "text_embedding.predicted_value",
        "k": 5,
        "num_candidates": 10,
        "query_vector_builder": {
          "text_embedding": { 
            "model_id": "models--thenlper--gte-large-zh", 
            "model_text": "阿里云elasticsearch應用場景有哪些" 
          }
        }
      }
    }

    返回結果如下所示,可以看出前5條文檔和ES相關性比較大,第一條內容主要介紹阿里云ES服務。

    {
      "took": 30,
      "timed_out": false,
      "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": {
          "value": 5,
          "relation": "eq"
        },
        "max_score": 0.003581697,
        "hits": [
          {
            "_index": "question_answering",
            "_id": "evdbaI0BU_1of7kX5_Hn",
            "_score": 0.003581697,
            "_source": {
              "context": "阿里云Elasticsearch是基于開源Elasticsearch構建的全托管Elasticsearch云服務,在100%兼容開源功能的同時,支持開箱即用、按需付費。不僅提供云上開箱即用的Elasticsearch、Logstash、Kibana、Beats在內的Elastic Stack生態組件,還與Elastic官方合作提供免費X-Pack(白金版高級特性)商業插件,集成了安全、SQL、機器學習、告警、監控等高級特性,被廣泛應用于實時日志分析處理、信息檢索、以及數據的多維查詢和統計分析等場景。",
              "title": "什么是阿里云Elasticsearch"
            }
          },
          {
            "_index": "question_answering",
            "_id": "2VBdaI0Bg5EzGjIAN-Tx",
            "_score": 0.003581697,
            "_source": {
              "context": "阿里云Elasticsearch是基于開源Elasticsearch構建的全托管Elasticsearch云服務,在100%兼容開源功能的同時,支持開箱即用、按需付費。不僅提供云上開箱即用的Elasticsearch、Logstash、Kibana、Beats在內的Elastic Stack生態組件,還與Elastic官方合作提供免費X-Pack(白金版高級特性)商業插件,集成了安全、SQL、機器學習、告警、監控等高級特性,被廣泛應用于實時日志分析處理、信息檢索、以及數據的多維查詢和統計分析等場景。",
              "title": "什么是阿里云Elasticsearch"
            }
          },
          {
            "_index": "question_answering",
            "_id": "t49gaI0BNqB2ciGcDbmF",
            "_score": 0.003581697,
            "_source": {
              "context": "阿里云Elasticsearch是基于開源Elasticsearch構建的全托管Elasticsearch云服務,在100%兼容開源功能的同時,支持開箱即用、按需付費。不僅提供云上開箱即用的Elasticsearch、Logstash、Kibana、Beats在內的Elastic Stack生態組件,還與Elastic官方合作提供免費X-Pack(白金版高級特性)商業插件,集成了安全、SQL、機器學習、告警、監控等高級特性,被廣泛應用于實時日志分析處理、信息檢索、以及數據的多維查詢和統計分析等場景。",
              "title": "什么是阿里云Elasticsearch"
            }
          },
          {
            "_index": "question_answering",
            "_id": "efdbaI0BU_1of7kX5_Hn",
            "_score": 0.0027631863,
            "_source": {
              "context": "登錄Kibana控制臺的具體操作,請參見登錄Kibana控制臺。Kibana控制臺的用戶名默認為elastic,密碼為您創建阿里云Elasticsearch實例時設置的密碼。如果忘記密碼可以重置,重置密碼的注意事項及具體操作,請參見重置實例訪問密碼。",
              "title": "如何登錄Kibana控制臺,用戶名和密碼是什么?"
            }
          },
          {
            "_index": "question_answering",
            "_id": "2FBdaI0Bg5EzGjIAN-Tx",
            "_score": 0.0027631863,
            "_source": {
              "context": "登錄Kibana控制臺的具體操作,請參見登錄Kibana控制臺。Kibana控制臺的用戶名默認為elastic,密碼為您創建阿里云Elasticsearch實例時設置的密碼。如果忘記密碼可以重置,重置密碼的注意事項及具體操作,請參見重置實例訪問密碼。",
              "title": "如何登錄Kibana控制臺,用戶名和密碼是什么?"
            }
          }
        ]
      }
    }

    ES 8.7以下版本

    1. 調用ES機器學習推理接口。

      POST /_ml/trained_models/thenlper__gte-large-zh/_infer
      {
        "docs":[{"text_field": "阿里云elasticsearch應用場景有哪些"}]
      }

      返回結果如下所示:

      image.png

    2. 復制向量數據,使用knn搜索索引中相似的文檔。

      GET question_answering/_search
      { 
        "_source": ["context","title"],
        "knn": {
          "field": "text_embedding.predicted_value",
          "k": 5,
          "num_candidates": 10,
          "query_vector": [
            0.2767493426799774,
            0.05577810853719711,
            0.2760164141654968,
            -0.9484721422195435,
            ..............
            0.8358230590820312,
            0.6053569316864014,
            -0.5380803942680359
          ]
        }
      }
  3. 將第一個文檔的context復制下來調用QA模型實現問答。

    POST /_ml/trained_models/models--luhua--chinese_pretrain_mrc_macbert_large/_infer
    {
      "docs":[{"text_field": "阿里云Elasticsearch是基于開源Elasticsearch構建的全托管Elasticsearch云服務,在100%兼容開源功能的同時,支持開箱即用、按需付費。不僅提供云上開箱即用的Elasticsearch、Logstash、Kibana、Beats在內的Elastic Stack生態組件,還與Elastic官方合作提供免費X-Pack(白金版高級特性)商業插件,集成了安全、SQL、機器學習、告警、監控等高級特性,被廣泛應用于實時日志分析處理、信息檢索、以及數據的多維查詢和統計分析等場景"}],
      "inference_config": {"question_answering": {"question": "阿里云elasticsearch應用場景有哪些"}}
    }

    返回結果如下:

    {
      "inference_results": [
        {
          "predicted_value": "實時日志分析處理",
          "start_offset": 220,
          "end_offset": 228,
          "prediction_probability": 0.014678373776954107
        }
      ]
    }

步驟三:模擬問答場景

在ECS中執行Python3加載Python環境后,執行以下命令模擬問答場景。

# -*- coding:utf-8  -*-
import json
from pprint import pprint
import os
import time
from elasticsearch import helpers
import time
from elasticsearch import Elasticsearch

elastic_user="elastic"
elastic_password="Es123456"
elastic_endpoint="es-cn-lbj3l7erv0009bz81.public.elasticsearch.aliyuncs.com"
url = f"http://{elastic_user}:{elastic_password}@{elastic_endpoint}:9200"
es = Elasticsearch(url)

question = input("請輸入您的問題: ")

query_args = {
    "field": "text_embedding.predicted_value",
    "k": 5,
    "num_candidates": 10,
    "query_vector_builder": {
        "text_embedding": {
            "model_id": "models--thenlper--gte-large-zh",
            "model_text": question
        }
    }
}

result=es.search(index="question_answering",source=["context","title"],knn=query_args)
# print(result["hits"]["hits"])
concatenated_content = ""
for hit in result["hits"]["hits"]:
    context=hit["_source"]["context"]
    concatenated_content += context+" "

#print(concatenated_content)
docs=[{"text_field": concatenated_content}]
inference_config= {"question_answering": {"question": question}}

response=es.ml.infer_trained_model(model_id="models--luhua--chinese_pretrain_mrc_macbert_large",docs=docs,inference_config=inference_config)
print("推理結果是: ")
print(response["inference_results"][0]["predicted_value"])

返回結果如下所示。

請輸入您的問題:
張家港高鐵站位置在哪
推理結果是:
塘橋鎮新204國道東側,人民路北路