加载中

导入训练模型和词汇表

Elastic Stack Serverless

重要提示

如果您想在受限或封闭的网络中安装已训练的模型,请参阅这些说明

选择模型后,您必须将其及其分词器词汇表导入到您的集群。导入模型时,由于模型较大,必须将其分块并一次一个块地导入,以便分段存储。

注意

已训练模型必须是 TorchScript 表示形式,才能与 Elastic Stack 的机器学习功能一起使用。

Eland 是一个 Elasticsearch Python 客户端,提供了一个简单的脚本来执行 Hugging Face Transformer 模型到 TorchScript 表示形式的转换、分块过程以及上传到 Elasticsearch;因此,它是推荐的导入方法。您可以将 Python Eland 客户端安装在您的机器上,或者使用 Docker 镜像来构建 Eland 并运行模型导入脚本。

  1. 使用 PyTorch 额外依赖项安装Eland Python 客户端

    python -m pip install 'eland[pytorch]'
    
  2. 运行 eland_import_hub_model 脚本以从 Hugging Face 下载模型,将其转换为 TorchScript 格式,并上传到 Elasticsearch 集群。例如:

    eland_import_hub_model \
    --cloud-id <cloud-id> \
    -u <username> -p <password> \
    --hub-model-id elastic/distilbert-base-cased-finetuned-conll03-english \
    --task-type ner
    
    1. 指定 Elastic Cloud 标识符。或者,可以使用 --url
    2. 提供身份验证详细信息以访问您的集群。有关更多信息,请参阅身份验证方法
    3. 指定 Hugging Face 模型中心中模型的标识符。
    4. 指定 NLP 任务的类型。支持的值包括 fill_masknerquestion_answeringtext_classificationtext_embeddingtext_expansiontext_similarityzero_shot_classification

有关更多详细信息,请参阅Eland 文档

如果您想在不安装 Eland 的情况下使用它,请运行以下命令:

$ docker run -it --rm --network host docker.elastic.co/eland/eland

eland_import_hub_model 脚本可以直接在 docker 命令中运行。

docker run -it --rm docker.elastic.co/eland/eland \
    eland_import_hub_model \
      --url $ELASTICSEARCH_URL \
      --hub-model-id elastic/distilbert-base-uncased-finetuned-conll03-english \
      --start

$ELASTICSEARCH_URL 替换为您的 Elasticsearch 集群的 URL。有关更多信息,请参阅身份验证方法

使用导入脚本时,以下身份验证选项可用:

  • 用户名/密码身份验证(使用 -u-p 选项指定)
eland_import_hub_model --url https://<hostname>:<port> -u <username> -p <password> ...
  • 用户名/密码身份验证(嵌入在 URL 中)
eland_import_hub_model --url https://<user>:<password>@<hostname>:<port> ...
  • API 密钥身份验证
eland_import_hub_model --url https://<hostname>:<port> --es-api-key <api-key> ...
© . This site is unofficial and not affiliated with Elasticsearch BV.