|
15 | 15 | },
|
16 | 16 | {
|
17 | 17 | "cell_type": "markdown",
|
18 |
| - "id": "9c99b06d", |
| 18 | + "id": "f9101eb9", |
19 | 19 | "metadata": {},
|
20 | 20 | "source": [
|
21 | 21 | "# 🧰 Requirements\n",
|
22 | 22 | "\n",
|
23 | 23 | "For this example, you will need:\n",
|
24 | 24 | "\n",
|
25 |
| - "- An Elastic deployment with minimum **4GB machine learning node**\n", |
| 25 | + "- An Elastic deployment:\n", |
26 | 26 | " - We'll be using [Elastic Cloud](https://www.elastic.co/guide/en/cloud/current/ec-getting-started.html) for this example (available with a [free trial](https://cloud.elastic.co/registration?utm_source=github&utm_content=elasticsearch-labs-notebook))\n",
|
27 | 27 | " \n",
|
28 | 28 | "- A paid [OpenAI account](https://openai.com/) is required to use the Inference API with \n",
|
|
31 | 31 | },
|
32 | 32 | {
|
33 | 33 | "cell_type": "markdown",
|
34 |
| - "id": "15193c10", |
| 34 | + "id": "4cd69cc0", |
35 | 35 | "metadata": {},
|
36 | 36 | "source": [
|
37 | 37 | "# Create Elastic Cloud deployment\n",
|
38 | 38 | "\n",
|
39 |
| - "If you don't have an Elastic Cloud deployment, sign up [here](https://cloud.elastic.co/registration?utm_source=github&utm_content=elasticsearch-labs-notebook) for a free trial.\n", |
40 |
| - "\n", |
41 |
| - "- Go to the [Create deployment](https://cloud.elastic.co/deployments/create) page\n", |
42 |
| - " - Under **Advanced settings**, go to **Machine Learning instances**\n", |
43 |
| - " - You'll need at least **4GB** RAM per zone for this tutorial\n", |
44 |
| - " - Select **Create deployment**" |
| 39 | + "If you don't have an Elastic Cloud deployment, sign up [here](https://cloud.elastic.co/registration?utm_source=github&utm_content=elasticsearch-labs-notebook) for a free trial." |
45 | 40 | ]
|
46 | 41 | },
|
47 | 42 | {
|
|
79 | 74 | },
|
80 | 75 | {
|
81 | 76 | "cell_type": "code",
|
82 |
| - "execution_count": null, |
| 77 | + "execution_count": 3, |
83 | 78 | "id": "690ff9af",
|
84 | 79 | "metadata": {},
|
85 | 80 | "outputs": [],
|
86 | 81 | "source": [
|
87 |
| - "from elasticsearch import Elasticsearch, helpers\n", |
| 82 | + "from elasticsearch import Elasticsearch, helpers, exceptions\n", |
88 | 83 | "from urllib.request import urlopen\n",
|
89 | 84 | "import getpass\n",
|
90 | 85 | "import json\n",
|
|
134 | 129 | },
|
135 | 130 | {
|
136 | 131 | "cell_type": "code",
|
137 |
| - "execution_count": null, |
| 132 | + "execution_count": 14, |
138 | 133 | "id": "cc0de5ea",
|
139 | 134 | "metadata": {},
|
140 |
| - "outputs": [], |
| 135 | + "outputs": [ |
| 136 | + { |
| 137 | + "name": "stdout", |
| 138 | + "output_type": "stream", |
| 139 | + "text": [ |
| 140 | + "{'name': 'instance-0000000000', 'cluster_name': '0a47378bc5e04c1995cd4c4c92131cd0', 'cluster_uuid': 'DgpshH2GTGefHGUStkD85w', 'version': {'number': '8.12.0', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '5077850702d0aa4fc42d3eb53bd39b282ae8ad3a', 'build_date': '2023-12-28T10:04:50.840819947Z', 'build_snapshot': False, 'lucene_version': '9.9.1', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}\n" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
141 | 144 | "source": [
|
142 | 145 | "print(client.info())"
|
143 | 146 | ]
|
144 | 147 | },
|
145 | 148 | {
|
146 | 149 | "cell_type": "markdown",
|
147 |
| - "id": "4e9e7354", |
| 150 | + "id": "659c5890", |
148 | 151 | "metadata": {},
|
149 | 152 | "source": [
|
150 | 153 | "Refer to [the documentation](https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/connecting.html#connect-self-managed-new) to learn how to connect to a self-managed deployment.\n",
|
151 | 154 | "\n",
|
152 |
| - "Read [this page](https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/connecting.html#connect-self-managed-new) to learn how to connect using API keys.\n" |
| 155 | + "Read [this page](https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/connecting.html#connect-self-managed-new) to learn how to connect using API keys." |
153 | 156 | ]
|
154 | 157 | },
|
155 | 158 | {
|
156 | 159 | "cell_type": "markdown",
|
157 |
| - "id": "96788aa1", |
| 160 | + "id": "840d92f0", |
158 | 161 | "metadata": {},
|
159 | 162 | "source": [
|
160 | 163 | "## Create the inference task\n",
|
|
167 | 170 | {
|
168 | 171 | "cell_type": "code",
|
169 | 172 | "execution_count": null,
|
170 |
| - "id": "3e6d98af", |
| 173 | + "id": "0d007737", |
171 | 174 | "metadata": {},
|
172 | 175 | "outputs": [],
|
173 | 176 | "source": [
|
174 |
| - "API_KEY = getpass.getpass('Enter OpenAI API key: ')\n", |
| 177 | + "API_KEY = getpass.getpass('OpenAI API key: ')\n", |
175 | 178 | "\n",
|
176 | 179 | "client.inference.put_model(\n",
|
177 | 180 | " task_type=\"text_embedding\",\n",
|
178 |
| - " model_id=\"openai_embeddings\",\n", |
| 181 | + " model_id=\"my_openai_embedding_model\",\n", |
179 | 182 | " body={\n",
|
180 | 183 | " \"service\": \"openai\",\n",
|
181 | 184 | " \"service_settings\": {\n",
|
|
190 | 193 | },
|
191 | 194 | {
|
192 | 195 | "cell_type": "markdown",
|
193 |
| - "id": "e5feaf12", |
| 196 | + "id": "1024d070", |
194 | 197 | "metadata": {},
|
195 | 198 | "source": [
|
196 | 199 | "## Create an ingest pipeline with an inference processor\n",
|
|
201 | 204 | {
|
202 | 205 | "cell_type": "code",
|
203 | 206 | "execution_count": null,
|
204 |
| - "id": "c5897fe4", |
| 207 | + "id": "6ace9e2e", |
205 | 208 | "metadata": {},
|
206 | 209 | "outputs": [],
|
207 | 210 | "source": [
|
208 | 211 | "client.ingest.put_pipeline(\n",
|
209 |
| - " id=\"openai_embeddings\", \n", |
| 212 | + " id=\"openai_embeddings_pipeline\", \n", |
210 | 213 | " description=\"Ingest pipeline for OpenAI inference.\",\n",
|
211 | 214 | " processors=[\n",
|
212 | 215 | " {\n",
|
213 | 216 | " \"inference\": {\n",
|
214 |
| - " \"model_id\": \"openai_embeddings\",\n", |
| 217 | + " \"model_id\": \"my_openai_embedding_model\",\n", |
215 | 218 | " \"input_output\": {\n",
|
216 | 219 | " \"input_field\": \"plot\",\n",
|
217 | 220 | " \"output_field\": \"plot_embedding\"\n",
|
218 |
| - " }\n", |
| 221 | + " }\n", |
219 | 222 | " }\n",
|
220 | 223 | " }\n",
|
221 | 224 | " ]\n",
|
|
224 | 227 | },
|
225 | 228 | {
|
226 | 229 | "cell_type": "markdown",
|
227 |
| - "id": "7b6dd89c", |
| 230 | + "id": "76d07567", |
228 | 231 | "metadata": {},
|
229 | 232 | "source": [
|
230 | 233 | "Let's note a few important parameters from that API call:\n",
|
231 | 234 | "\n",
|
232 | 235 | "- `inference`: A processor that performs inference using a machine learning model.\n",
|
233 |
| - "- `model_id`: Specifies the ID of the machine learning model to be used. In this example, the model ID is set to `openai_embeddings`.\n", |
| 236 | + "- `model_id`: Specifies the ID of the machine learning model to be used. In this example, the model ID is set to `my_openai_embedding_model`. Use the model ID you defined when created the inference task.\n", |
234 | 237 | "- `input_output`: Specifies input and output fields.\n",
|
235 | 238 | "- `input_field`: Field name from which the `dense_vector` representation is created.\n",
|
236 | 239 | "- `output_field`: Field name which contains inference results. "
|
237 | 240 | ]
|
238 | 241 | },
|
239 | 242 | {
|
240 | 243 | "cell_type": "markdown",
|
241 |
| - "id": "f167c8cf", |
| 244 | + "id": "28e12d7a", |
242 | 245 | "metadata": {},
|
243 | 246 | "source": [
|
244 | 247 | "## Create index\n",
|
245 | 248 | "\n",
|
246 |
| - "The mapping of the destination index – the index that contains the embeddings that the model will create based on your input text – must be created. The destination index must have a field with the [dense_vector](https://www.elastic.co/guide/en/elasticsearch/reference/current/dense-vector.html) field type to index the output of the OpenAI model.\n", |
| 249 | + "The mapping of the destination index - the index that contains the embeddings that the model will create based on your input text - must be created. The destination index must have a field with the [dense_vector](https://www.elastic.co/guide/en/elasticsearch/reference/current/dense-vector.html) field type to index the output of the OpenAI model.\n", |
247 | 250 | "\n",
|
248 | 251 | "Let's create an index named `openai-movie-embeddings` with the mappings we need."
|
249 | 252 | ]
|
250 | 253 | },
|
251 | 254 | {
|
252 | 255 | "cell_type": "code",
|
253 | 256 | "execution_count": null,
|
254 |
| - "id": "37558907", |
| 257 | + "id": "6ddcbca3", |
255 | 258 | "metadata": {},
|
256 | 259 | "outputs": [],
|
257 | 260 | "source": [
|
|
260 | 263 | " index=\"openai-movie-embeddings\",\n",
|
261 | 264 | " settings={\n",
|
262 | 265 | " \"index\": {\n",
|
263 |
| - " \"default_pipeline\": \"openai_embeddings\"\n", |
| 266 | + " \"default_pipeline\": \"openai_embeddings_pipeline\"\n", |
264 | 267 | " }\n",
|
265 | 268 | " },\n",
|
266 | 269 | " mappings={\n",
|
267 | 270 | " \"properties\": {\n",
|
268 | 271 | " \"plot_embedding\": { \n",
|
269 | 272 | " \"type\": \"dense_vector\", \n",
|
270 |
| - " \"dims\": 1536,\n", |
| 273 | + " \"dims\": 1536, \n", |
271 | 274 | " \"similarity\": \"dot_product\" \n",
|
272 | 275 | " },\n",
|
273 |
| - " \"plot\": { \n", |
274 |
| - " \"type\": \"text\" \n", |
| 276 | + " \"plot\": {\n", |
| 277 | + " \"type\": \"text\"\n", |
| 278 | + " }\n", |
275 | 279 | " }\n",
|
276 | 280 | " }\n",
|
277 |
| - " }\n", |
278 | 281 | ")"
|
279 | 282 | ]
|
280 | 283 | },
|
281 | 284 | {
|
282 | 285 | "cell_type": "markdown",
|
283 |
| - "id": "e9d4bfd2", |
| 286 | + "id": "07c187a9", |
284 | 287 | "metadata": {},
|
285 | 288 | "source": [
|
286 | 289 | "## Insert Documents\n",
|
|
291 | 294 | {
|
292 | 295 | "cell_type": "code",
|
293 | 296 | "execution_count": null,
|
294 |
| - "id": "cfa8eda5", |
| 297 | + "id": "d68737cb", |
295 | 298 | "metadata": {},
|
296 | 299 | "outputs": [],
|
297 | 300 | "source": [
|
|
318 | 321 | },
|
319 | 322 | {
|
320 | 323 | "cell_type": "markdown",
|
321 |
| - "id": "a68e808e", |
| 324 | + "id": "cf0f6df7", |
322 | 325 | "metadata": {},
|
323 | 326 | "source": [
|
324 | 327 | "## Semantic search\n",
|
|
328 | 331 | },
|
329 | 332 | {
|
330 | 333 | "cell_type": "code",
|
331 |
| - "execution_count": null, |
332 |
| - "id": "a47cdc60", |
| 334 | + "execution_count": 23, |
| 335 | + "id": "d9b21b71", |
333 | 336 | "metadata": {},
|
334 |
| - "outputs": [], |
| 337 | + "outputs": [ |
| 338 | + { |
| 339 | + "name": "stdout", |
| 340 | + "output_type": "stream", |
| 341 | + "text": [ |
| 342 | + "Score: 0.91674197\n", |
| 343 | + "Title: Fight Club\n", |
| 344 | + "Plot: An insomniac office worker and a devil-may-care soapmaker form an underground fight club that evolves into something much, much more.\n", |
| 345 | + "\n", |
| 346 | + "Score: 0.9069592\n", |
| 347 | + "Title: Pulp Fiction\n", |
| 348 | + "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.\n", |
| 349 | + "\n", |
| 350 | + "Score: 0.8992071\n", |
| 351 | + "Title: The Dark Knight\n", |
| 352 | + "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.\n", |
| 353 | + "\n" |
| 354 | + ] |
| 355 | + } |
| 356 | + ], |
335 | 357 | "source": [
|
336 | 358 | "response = client.search(\n",
|
337 | 359 | " index='openai-movie-embeddings', \n",
|
|
340 | 362 | " \"field\": \"plot_embedding\",\n",
|
341 | 363 | " \"query_vector_builder\": {\n",
|
342 | 364 | " \"text_embedding\": {\n",
|
343 |
| - " \"model_id\": \"openai_embeddings\",\n", |
| 365 | + " \"model_id\": \"my_openai_embedding_model\",\n", |
344 | 366 | " \"model_text\": \"Fighting movie\"\n",
|
345 | 367 | " }\n",
|
346 | 368 | " },\n",
|
|
0 commit comments