导入训练好的模型和词汇表
Elastic Stack Serverless
重要提示
如果您想在受限或封闭网络中安装训练好的模型,请参考这些说明。
选择模型后,您必须将其及其分词器词汇表导入到您的集群。 导入模型时,由于其大小,必须对其进行分块并一次导入一个块,以便分部分存储。
注意
训练好的模型必须采用 TorchScript 表示形式,才能与 Elastic Stack 机器学习功能一起使用。
Eland 是一个 Elasticsearch Python 客户端,它提供了一个简单的脚本来执行 Hugging Face transformer 模型到其 TorchScript 表示形式的转换、分块过程以及上传到 Elasticsearch;因此,这是推荐的导入方法。 您可以在机器上安装 Python Eland 客户端,也可以使用 Docker 镜像来构建 Eland 并运行模型导入脚本。
安装带有 PyTorch 额外依赖项的 Eland Python 客户端。
python -m pip install 'eland[pytorch]'
运行
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
- 指定 Elastic Cloud 标识符。或者,使用
--url
。 - 提供身份验证详细信息以访问您的集群。 有关更多信息,请参阅身份验证方法。
- 在 Hugging Face 模型中心指定模型的标识符。
- 指定 NLP 任务的类型。 支持的值为
fill_mask
、ner
、question_answering
、text_classification
、text_embedding
、text_expansion
、text_similarity
和zero_shot_classification
。
- 指定 Elastic Cloud 标识符。或者,使用
有关更多详细信息,请参阅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> ...