Spaces:
Sleeping
Sleeping
File size: 8,187 Bytes
40d903c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/jeffvestal/ElasticDocs_GPT/blob/main/load_embedding_model.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# ElasticDocs GPT Blog\n",
"# Loading an embedding from Hugging Face into Elasticsearch\n",
"\n",
"This code will show you how to load a supported embedding model from Hugging Face into an elasticsearch cluster in [Elastic Cloud](https://cloud.elastic.co/)\n",
"\n",
"[Blog - ChatGPT and Elasticsearch: OpenAI meets private data](https://www.elastic.co/blog/chatgpt-elasticsearch-openai-meets-private-data)"
],
"metadata": {
"id": "6xoLDtS_6Df1"
}
},
{
"cell_type": "markdown",
"source": [
"# Setup\n"
],
"metadata": {
"id": "DgxCKQS7mCZw"
}
},
{
"cell_type": "markdown",
"source": [
"## Install and import required python libraries"
],
"metadata": {
"id": "Ly1f1P-l9ri8"
}
},
{
"cell_type": "markdown",
"source": [
"Elastic uses the [eland python library](https://github.com/elastic/eland) to download modesl from Hugging Face hub and load them into elasticsearch"
],
"metadata": {
"id": "MJAb_8zlPFhQ"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rUedSzQW9FIF"
},
"outputs": [],
"source": [
"pip -q install eland elasticsearch sentence_transformers transformers torch==1.11"
]
},
{
"cell_type": "code",
"source": [
"from pathlib import Path\n",
"from eland.ml.pytorch import PyTorchModel\n",
"from eland.ml.pytorch.transformers import TransformerModel\n",
"from elasticsearch import Elasticsearch\n",
"from elasticsearch.client import MlClient\n",
"\n",
"import getpass"
],
"metadata": {
"id": "wyUZXUi4RWWL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Configure elasticsearch authentication. \n",
"The recommended authentication approach is using the [Elastic Cloud ID](https://www.elastic.co/guide/en/cloud/current/ec-cloud-id.html) and a [cluster level API key](https://www.elastic.co/guide/en/kibana/current/api-keys.html)\n",
"\n",
"You can use any method you wish to set the required credentials. We are using getpass in this example to prompt for credentials to avoide storing them in github."
],
"metadata": {
"id": "r7nMIbHke37Q"
}
},
{
"cell_type": "code",
"source": [
"es_cloud_id = getpass.getpass('Enter Elastic Cloud ID: ')\n",
"es_user = getpass.getpass('Enter cluster username: ') \n",
"es_pass = getpass.getpass('Enter cluster password: ') \n",
"\n",
"#es_api_id = getpass.getpass('Enter cluster API key ID: ') \n",
"#es_api_key = getpass.getpass('Enter cluster API key: ')"
],
"metadata": {
"id": "SSGgYHome69o"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Connect to Elastic Cloud"
],
"metadata": {
"id": "jL4VDnVp96lf"
}
},
{
"cell_type": "code",
"source": [
"#es = Elasticsearch(cloud_id=es_cloud_id, \n",
"# api_key=(es_api_id, es_api_key)\n",
"# )\n",
"es = Elasticsearch(cloud_id=es_cloud_id, \n",
" basic_auth=(es_user, es_pass)\n",
" )\n",
"es.info() # should return cluster info"
],
"metadata": {
"id": "I8mVJkKmetXo"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Load the model From Hugging Face into Elasticsearch\n",
"Here we specify the model id from Hugging Face. The easiest way to get this id is clicking the copy the model name icon next to the name on the model page. \n",
"\n",
"When calling `TransformerModel` you specify the HF model id and the task type. You can try specifying `auto` and eland will attempt to determine the correct type from info in the model config. This is not always possible so a list of specific `task_type` values can be viewed in the following code: \n",
"[Supported values](https://github.com/elastic/eland/blob/15a300728876022b206161d71055c67b500a0192/eland/ml/pytorch/transformers.py#*L41*)"
],
"metadata": {
"id": "uBMWHj-ZmtvE"
}
},
{
"cell_type": "code",
"source": [
"# Set the model name from Hugging Face and task type\n",
"hf_model_id='sentence-transformers/all-distilroberta-v1'\n",
"tm = TransformerModel(model_id=hf_model_id, task_type='text_embedding')\n",
"\n",
"#set the modelID as it is named in Elasticsearch\n",
"es_model_id = tm.elasticsearch_model_id()\n",
"\n",
"# Download the model from Hugging Face\n",
"tmp_path = \"models\"\n",
"Path(tmp_path).mkdir(parents=True, exist_ok=True)\n",
"model_path, config, vocab_path = tm.save(tmp_path)\n",
"\n",
"# Load the model into Elasticsearch\n",
"ptm = PyTorchModel(es, es_model_id)\n",
"ptm.import_model(model_path=model_path, config_path=None, vocab_path=vocab_path, config=config) \n"
],
"metadata": {
"id": "zPV3oFsKiYFL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Starting the Model"
],
"metadata": {
"id": "4UYSzFp3vHdB"
}
},
{
"cell_type": "markdown",
"source": [
"## View information about the model\n",
"This is not required but can be handy to get a model overivew"
],
"metadata": {
"id": "wQwfozwznK4Y"
}
},
{
"cell_type": "code",
"source": [
"# List the in elasticsearch\n",
"m = MlClient.get_trained_models(es, model_id=es_model_id)\n",
"m.body"
],
"metadata": {
"id": "b4Wv8EJvpfZI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Deploy the model\n",
"This will load the model on the ML nodes and start the process(es) making it available for the NLP task"
],
"metadata": {
"id": "oMGw3sk-pbaN"
}
},
{
"cell_type": "code",
"source": [
"s = MlClient.start_trained_model_deployment(es, model_id=es_model_id)\n",
"s.body"
],
"metadata": {
"id": "w5muJ1rLqvUW"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Verify the model started without issue\n",
"Should output -> {'routing_state': 'started'}"
],
"metadata": {
"id": "ZytlELrsnn_O"
}
},
{
"cell_type": "code",
"source": [
"stats = MlClient.get_trained_models_stats(es, model_id=es_model_id)\n",
"stats.body['trained_model_stats'][0]['deployment_stats']['nodes'][0]['routing_state']"
],
"metadata": {
"id": "ZaQUUWe0Hxwz"
},
"execution_count": null,
"outputs": []
}
]
}
|