|
12 | 12 | "\n",
|
13 | 13 | "In this notebook you'll learn how to implement semantic reranking in Elasticsearch using the built-in [Elastic Rerank model](https://www.elastic.co/guide/en/machine-learning/master/ml-nlp-rerank.html). You'll also learn about the `retriever` abstraction, a simpler syntax for crafting queries and combining different search operations.\n",
|
14 | 14 | "\n",
|
15 |
| - "You will:\n", |
16 |
| - "\n", |
17 |
| - "- Create an inference endpoint to manage your `rerank` task. This will download and deploy the Elastic Rerank model.\n", |
18 |
| - "- Query your data using the `text_similarity_rerank` retriever, leveraging the Elastic Rerank model." |
| 15 | + "You will query your data using the `text_similarity_rerank` retriever, and the Elastic Rerank model to boost the relevance of your search results." |
19 | 16 | ]
|
20 | 17 | },
|
21 | 18 | {
|
|
234 | 231 | "time.sleep(3)"
|
235 | 232 | ]
|
236 | 233 | },
|
237 |
| - { |
238 |
| - "cell_type": "markdown", |
239 |
| - "metadata": { |
240 |
| - "id": "DRIABkGAgV_Q" |
241 |
| - }, |
242 |
| - "source": [ |
243 |
| - "## Create inference endpoint\n", |
244 |
| - "\n", |
245 |
| - "Next we'll create an inference endpoint for the `rerank` task to deploy and manage our model and, if necessary, spin up the necessary ML resources behind the scenes." |
246 |
| - ] |
247 |
| - }, |
248 |
| - { |
249 |
| - "cell_type": "code", |
250 |
| - "execution_count": null, |
251 |
| - "metadata": { |
252 |
| - "colab": { |
253 |
| - "base_uri": "https://localhost:8080/" |
254 |
| - }, |
255 |
| - "id": "DiKsd3YygV_Q", |
256 |
| - "outputId": "c3c46c6b-b502-4167-c98c-d2e2e0a4613c" |
257 |
| - }, |
258 |
| - "outputs": [], |
259 |
| - "source": [ |
260 |
| - "try:\n", |
261 |
| - " client.inference.delete(inference_id=\"my-elastic-reranker\")\n", |
262 |
| - "except exceptions.NotFoundError:\n", |
263 |
| - " # Inference endpoint does not exist\n", |
264 |
| - " pass\n", |
265 |
| - "\n", |
266 |
| - "try:\n", |
267 |
| - " client.options(\n", |
268 |
| - " request_timeout=60, max_retries=3, retry_on_timeout=True\n", |
269 |
| - " ).inference.put(\n", |
270 |
| - " task_type=\"rerank\",\n", |
271 |
| - " inference_id=\"my-elastic-reranker\",\n", |
272 |
| - " inference_config={\n", |
273 |
| - " \"service\": \"elasticsearch\",\n", |
274 |
| - " \"service_settings\": {\n", |
275 |
| - " \"model_id\": \".rerank-v1\",\n", |
276 |
| - " \"num_threads\": 1,\n", |
277 |
| - " \"adaptive_allocations\": {\n", |
278 |
| - " \"enabled\": True,\n", |
279 |
| - " \"min_number_of_allocations\": 1,\n", |
280 |
| - " \"max_number_of_allocations\": 4,\n", |
281 |
| - " },\n", |
282 |
| - " },\n", |
283 |
| - " },\n", |
284 |
| - " )\n", |
285 |
| - " print(\"Inference endpoint created successfully\")\n", |
286 |
| - "except exceptions.BadRequestError as e:\n", |
287 |
| - " if e.error == \"resource_already_exists_exception\":\n", |
288 |
| - " print(\"Inference endpoint created successfully\")\n", |
289 |
| - " else:\n", |
290 |
| - " raise e" |
291 |
| - ] |
292 |
| - }, |
293 |
| - { |
294 |
| - "cell_type": "markdown", |
295 |
| - "metadata": {}, |
296 |
| - "source": [ |
297 |
| - "Run the following command to confirm your inference endpoint is deployed." |
298 |
| - ] |
299 |
| - }, |
300 |
| - { |
301 |
| - "cell_type": "code", |
302 |
| - "execution_count": null, |
303 |
| - "metadata": {}, |
304 |
| - "outputs": [], |
305 |
| - "source": [ |
306 |
| - "client.inference.get().body" |
307 |
| - ] |
308 |
| - }, |
309 |
| - { |
310 |
| - "cell_type": "markdown", |
311 |
| - "metadata": {}, |
312 |
| - "source": [ |
313 |
| - "\n", |
314 |
| - "⚠️ When you deploy your model, you might need to sync your ML saved objects in the Kibana (or Serverless) UI.\n", |
315 |
| - "Go to **Trained Models** and select **Synchronize saved objects**." |
316 |
| - ] |
317 |
| - }, |
318 | 234 | {
|
319 | 235 | "cell_type": "markdown",
|
320 | 236 | "metadata": {
|
|
465 | 381 | "source": [
|
466 | 382 | "## Semantic reranker\n",
|
467 | 383 | "\n",
|
468 |
| - "In the following `retriever` syntax, we wrap our standard `match` query retriever in a `text_similarity_reranker`. This allows us to leverage the NLP model we deployed to Elasticsearch to rerank the results based on the phrase \"flesh-eating bad guy\"." |
| 384 | + "In the following `retriever` syntax, we wrap our standard `match` query retriever in a `text_similarity_reranker`. This allows us to leverage the [Elastic rerank model](https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-rerank.html) to rerank the results based on the phrase \"flesh-eating bad guy\"." |
469 | 385 | ]
|
470 | 386 | },
|
471 | 387 | {
|
|
523 | 439 | " }\n",
|
524 | 440 | " },\n",
|
525 | 441 | " \"field\": \"plot\",\n",
|
526 |
| - " \"inference_id\": \"my-elastic-reranker\",\n", |
527 | 442 | " \"inference_text\": \"flesh-eating bad guy\",\n",
|
528 | 443 | " }\n",
|
529 | 444 | " },\n",
|
|
543 | 458 | "source": [
|
544 | 459 | "Success! \"The Silence of the Lambs\" is our top result. Semantic reranking helped us find the most relevant result by parsing a natural language query, overcoming the limitations of lexical search that relies on keyword matching.\n",
|
545 | 460 | "\n",
|
546 |
| - "Semantic reranking enables semantic search in a few steps, without the need for generating and storing embeddings. This a great tool for testing and building hybrid search systems in Elasticsearch." |
| 461 | + "Semantic reranking enables semantic search in a few steps, without the need for generating and storing embeddings. This a great tool for testing and building hybrid search systems in Elasticsearch.\n", |
| 462 | + "\n", |
| 463 | + "*Note* Starting with Elasticsearch version `8.18`, The `inference_id` field is optional. If not specified, it defaults to `.rerank-v1-elasticsearch`. If you are using an earlier version or prefer to manage your own endpoint, you can set up a custom `rerank` inference endpoint using the [create inference API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-inference-put)." |
547 | 464 | ]
|
548 | 465 | },
|
549 | 466 | {
|
|
0 commit comments