Skip to content

Commit f4ac9a0

Browse files
committed
Apply Code/Writing review
Signed-off-by: Brian Flores <[email protected]>
1 parent d48f75c commit f4ac9a0

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

docs/tutorials/semantic_search/asymmetric_embedding_model.md

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tutorial: Running Asymmetric Semnantic Search within OpenSearch
22

3-
This tutorial demonstrates how to generate text embeddings using an asymmetric embedding model in OpenSearch which will be used
4-
to run semantic search. This is implemented within a Docker container, the example model used in this tutorial is the multilingual
3+
This tutorial demonstrates how generating text embeddings using an asymmetric embedding model in OpenSearch. The embeddings will be used
4+
to run semantic search, implemented using a Docker container. The example model used in this tutorial is the multilingual
55
`intfloat/multilingual-e5-small` model from Hugging Face.
66
You will learn how to prepare the model, register it in OpenSearch, and run inference to generate embeddings.
77

@@ -13,16 +13,26 @@ You will learn how to prepare the model, register it in OpenSearch, and run infe
1313

1414
- Docker Desktop installed and running on your local machine.
1515
- Basic familiarity with Docker and OpenSearch.
16-
- Access to the Hugging Face model `intfloat/multilingual-e5-small` (or another model of your choice).
16+
- Access to the Hugging Face `intfloat/multilingual-e5-small` model (or another model of your choice).
1717
---
1818

19-
## Step 1: Spin Up a Docker OpenSearch Cluster
19+
## Step 1: Spin up a Docker OpenSearch cluster
2020

21-
To run OpenSearch in a local development environment, you can use Docker and a pre-configured `docker-compose` file.
21+
To run OpenSearch in a local development environment, you can use Docker and a preconfigured `docker-compose` file.
2222

23-
### a. Update Cluster Settings
23+
### a. Create a Docker Compose File
2424

25-
Before proceeding, ensure your cluster is configured to allow registering models. You can do this by updating the cluster settings via the following request:
25+
You can use this sample [file](https://opensearch.org/docs/latest/install-and-configure/install-opensearch/docker/#sample-docker-compose-file-for-development) as an example.
26+
Once your `docker-compose.yml` file is created, run the following command to start OpenSearch in the background:
27+
28+
```
29+
docker-compose up -d
30+
```
31+
32+
33+
### b. Update cluster settings
34+
35+
Ensure your cluster is configured to allow registering models. You can do this by updating the cluster settings using the following request:
2636

2737
```
2838
PUT _cluster/settings
@@ -38,22 +48,14 @@ PUT _cluster/settings
3848

3949
This configuration ensures that OpenSearch can accept machine learning models from external URLs and can run models across non-ML nodes.
4050

41-
### b. Use a Docker Compose File
42-
43-
You can use this sample [file](https://opensearch.org/docs/latest/install-and-configure/install-opensearch/docker/#sample-docker-compose-file-for-development) as an example.
44-
Once your `docker-compose.yml` file is ready, run the following command to start OpenSearch in the background:
45-
46-
```
47-
docker-compose up -d
48-
```
4951

5052
---
5153

52-
## Step 2: Prepare the Model for OpenSearch
54+
## Step 2: Prepare the model for OpenSearch
5355

54-
In this tutorial, we’ll use the Hugging Face model `intfloat/multilingual-e5-small`, which is capable of generating multilingual embeddings. Follow these steps to prepare and zip the model for use in OpenSearch.
56+
In this tutorial, you’ll use the Hugging Face `intfloat/multilingual-e5-small` model, which is capable of generating multilingual embeddings. Follow these steps to prepare and zip the model for use in OpenSearch.
5557

56-
### a. Clone the Model from Hugging Face
58+
### a. Clone the model from Hugging Face
5759

5860
To download the model, use the following steps:
5961

@@ -71,31 +73,31 @@ To download the model, use the following steps:
7173

7274
This will download the model files into a directory on your local machine.
7375

74-
### b. Zip the Model Files
76+
### b. Zip the model files
7577

76-
In order to upload the model to OpenSearch, you must zip the necessary model files (`model.onnx`, `sentencepiece.bpe.model`, and `tokenizer.json`). The `model.onnx` file is located in the `onnx` directory of the cloned repository.
78+
To upload the model to OpenSearch, you must zip the necessary model files (`model.onnx`, `sentencepiece.bpe.model`, and `tokenizer.json`). The `model.onnx` file is located in the `onnx` directory of the cloned repository.
7779

7880
Run the following command in the directory containing these files:
7981

8082
```
8183
zip -r intfloat-multilingual-e5-small-onnx.zip model.onnx tokenizer.json sentencepiece.bpe.model
8284
```
8385

84-
This command will create a zip file named `intfloat-multilingual-e5-small-onnx.zip`, with the previous mentioned files.
86+
This command will create a zip file named `intfloat-multilingual-e5-small-onnx.zip`, with the all necessary files.
8587

86-
### c. Calculate the Model File Hash
88+
### c. Calculate the model file's hash
8789

8890
Before registering the model, you need to calculate the SHA-256 hash of the zip file. Run this command to generate the hash:
8991

9092
```
9193
shasum -a 256 intfloat-multilingual-e5-small-onnx.zip
9294
```
9395

94-
Make a note of the hash value, as you will need it during the model registration process.
96+
Note the hash value; You'll need it during the model registration.
9597

96-
### d. Serve the Model File Using a Python HTTP Server
98+
### d. Serve the model file using a Python HTTP server
9799

98-
To allow OpenSearch to access the model file, you need to serve it via HTTP. Since this is a local development environment, you can use Python's built-in HTTP server:
100+
To allow OpenSearch to access the model file, you can serve it through HTTP. Because this tutorial uses a local development environment, you can use Python's built-in HTTP server command:
99101

100102
Navigate to the directory containing the zip file and run the following command:
101103

@@ -107,7 +109,7 @@ This will serve the zip file at `http://0.0.0.0:8080/intfloat-multilingual-e5-sm
107109

108110
---
109111

110-
## Step 3: Register a Model Group
112+
## Step 3: Register a model group
111113

112114
Before registering the model itself, you need to create a model group. This helps organize models in OpenSearch. Run the following request to create a new model group:
113115

@@ -119,13 +121,13 @@ POST /_plugins/_ml/model_groups/_register
119121
}
120122
```
121123

122-
Take note of the `model_group_id` returned in the response, as it will be required when registering the model.
124+
Note of the `model_group_id` returned in the response; you'll use it to register the model.
123125

124126
---
125127

126-
## Step 4: Register the Model
128+
## Step 4: Register the model
127129

128-
Now that you have the model zip file and the model group ID, you can register the model in OpenSearch. Run the following request:
130+
Now that you have the model zip file and the model group ID, you can register the model in OpenSearch:
129131

130132
```
131133
POST /_plugins/_ml/models/_register
@@ -148,21 +150,21 @@ POST /_plugins/_ml/models/_register
148150
}
149151
```
150152

151-
Replace `your_group_id` and `your_model_zip_content_hash_value` with the actual values from earlier. This will initiate the model registration process, and you’ll receive a task ID in the response.
153+
Replace `your_group_id` and `your_model_zip_content_hash_value` with the values from previous steps. This will initiate the model registration process, and you’ll receive a task ID in the response.
152154

153-
To check the status of the registration, run:
155+
To check the status of the registration, run the following request:
154156

155157
```
156158
GET /_plugins/_ml/tasks/your_task_id
157159
```
158160

159-
Once successful, note the `model_id` returned, as you'll need it for deployment and inference.
161+
Once successful, note the `model_id` returned; you'll need it for deployment and inference.
160162

161163
---
162164

163-
## Step 5: Deploy the Model
165+
## Step 5: Deploy the model
164166

165-
After the model is registered, you can deploy it by running:
167+
After the model is registered, you can deploy it by running the following request:
166168

167169
```
168170
POST /_plugins/_ml/models/your_model_id/_deploy
@@ -174,15 +176,15 @@ Check the status of the deployment using the task ID:
174176
GET /_plugins/_ml/tasks/your_task_id
175177
```
176178

177-
When the model is successfully deployed, it will be in the **DEPLOYED** state, and you can use it for inference.
179+
When the model is successfully deployed, it's state will change to the **DEPLOYED** state, and you can use it for inference.
178180

179181
---
180182

181-
## Step 6: Run Inference
183+
## Step 6: Run inference
182184

183185
Now that your model is deployed, you can use it to generate text embeddings for both queries and passages.
184186

185-
### a. Generating Passage Embeddings
187+
### Generating passage embeddings
186188

187189
To generate embeddings for a passage, use the following request:
188190

@@ -218,7 +220,7 @@ The response will include a sentence embedding of size 384:
218220
}
219221
```
220222

221-
### b. Generating Query Embeddings
223+
### Generating query embeddings
222224

223225
Similarly, you can generate embeddings for a query:
224226

@@ -254,16 +256,16 @@ The response will look like this:
254256

255257
---
256258

257-
# Applying Semantic Search using an ML Inference processor
259+
# Applying semantic search using an ML Inference processor
258260

259-
In this section you are going to apply semantic search on facts about New York City. First you will create an ingest pipeline
261+
In this section you'll run semantic search on facts about New York City. First, you'll create an ingest pipeline
260262
using the ML inference processor to create embeddings on ingestion. Then create a search pipeline to run a search using
261263
the same asymmetric embedding model.
262264

263265

264266
## 2. Create an ingest pipeline
265267

266-
### 2.1 Create the test KNN index
268+
### 2.1 Create a test KNN index
267269
```
268270
PUT nyc_facts
269271
{
@@ -324,9 +326,9 @@ PUT _ingest/pipeline/asymmetric_embedding_ingest_pipeline
324326
}
325327
```
326328

327-
### 2.3 Simulate pipeline
329+
### 2.3 Simulate the pipeline
328330

329-
- Case1: two book objects with title
331+
Simulate the pipeline by running the following request:
330332
```
331333
POST /_ingest/pipeline/asymmetric_embedding_ingest_pipeline/_simulate
332334
{
@@ -342,7 +344,7 @@ POST /_ingest/pipeline/asymmetric_embedding_ingest_pipeline/_simulate
342344
]
343345
}
344346
```
345-
Response
347+
The response contains the embedding generated by the model after we "ingested" a document using the pipeline.
346348
```
347349
{
348350
"docs": [
@@ -375,8 +377,8 @@ Response
375377
}
376378
```
377379

378-
### 2.4 Test ingest data
379-
Perform bulk ingestion, this will now trigger the ingest pipeline to have embeddings for each document.
380+
### 2.4 Test data ingestion
381+
When you perform bulk ingestion, the ingest pipeline will generate embeddings for each document:
380382
```
381383
POST /_bulk
382384
{ "index": { "_index": "nyc_facts" } }
@@ -414,8 +416,8 @@ POST /_bulk
414416

415417
## 3. Run Semantic Search
416418

417-
### 3.1 Create the Search Pipeline
418-
Create the search pipeline which will convert your query into a embedding and run KNN on the index to return the best documents.
419+
### 3.1 Create a Search Pipeline
420+
Create a search pipeline that will convert your query into an embedding and run K-NN search on the index to return the best-matching documents:
419421

420422
```
421423
PUT /_search/pipeline/asymmetric_embedding_search_pipeline
@@ -447,7 +449,7 @@ PUT /_search/pipeline/asymmetric_embedding_search_pipeline
447449
```
448450

449451
### 3.1 Run Semantic Search
450-
In this scenario we are going to see the top 3 results, when asking about sporting activities in New York City.
452+
Run a query about sporting activities in New York City:
451453
```
452454
GET /nyc_facts/_search?search_pipeline=asymmetric_embedding_search_pipeline
453455
{
@@ -462,7 +464,7 @@ GET /nyc_facts/_search?search_pipeline=asymmetric_embedding_search_pipeline
462464
}
463465
```
464466

465-
Which yields the following
467+
The response contains the top 3 matching documents:
466468
```json
467469
{
468470
"took": 22,

0 commit comments

Comments
 (0)