|
| 1 | +### Cohere Embedding Connector Blueprint: |
| 2 | + |
| 3 | +This blueprint will show you how to connect a Cohere embedding model to your Opensearch cluster, including creating a k-nn index and your own Embedding pipeline. You will require a Cohere API key to create a connector. |
| 4 | + |
| 5 | +Cohere currently offers the following Embedding models (with model name and embedding dimensions). Note that only the following have been tested with the blueprint guide. |
| 6 | + |
| 7 | +- embed-english-v3.0 1024 |
| 8 | +- embed-english-v2.0 4096 |
| 9 | + |
| 10 | +See [Cohere's /embed API docs](https://docs.cohere.com/reference/embed) for more details. |
| 11 | + |
| 12 | +#### 1. Create a connector and model group |
| 13 | + |
| 14 | +##### 1a. Register model group |
| 15 | + |
| 16 | +```json |
| 17 | +POST /_plugins/_ml/model_groups/_register |
| 18 | + |
| 19 | +{ |
| 20 | + "name": "cohere_model_group", |
| 21 | + "description": "Your Cohere model group" |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +This request response will return the `model_group_id`, note it down. |
| 26 | + |
| 27 | +##### 1b. Create a connector |
| 28 | + |
| 29 | +See above for all the values the `parameters > model` parameter can take. |
| 30 | + |
| 31 | +```json |
| 32 | +POST /_plugins/_ml/connectors/_create |
| 33 | +{ |
| 34 | + "name": "Cohere Embed Model", |
| 35 | + "description": "The connector to Cohere's public embed API", |
| 36 | + "version": "1", |
| 37 | + "protocol": "http", |
| 38 | + "credential": { |
| 39 | + "cohere_key": "<ENTER_COHERE_API_KEY_HERE>" |
| 40 | + }, |
| 41 | + "parameters": { |
| 42 | + "model": "<ENTER_MODEL_NAME_HERE>", // Choose a Model from the provided list above |
| 43 | + "input_type":"search_document", |
| 44 | + "truncate": "END" |
| 45 | + }, |
| 46 | + "actions": [ |
| 47 | + { |
| 48 | + "action_type": "predict", |
| 49 | + "method": "POST", |
| 50 | + "url": "https://api.cohere.ai/v1/embed", |
| 51 | + "headers": { |
| 52 | + "Authorization": "Bearer ${credential.cohere_key}", |
| 53 | + "Request-Source": "unspecified:opensearch" |
| 54 | + }, |
| 55 | + "request_body": "{ \"texts\": ${parameters.texts}, \"truncate\": \"${parameters.truncate}\", \"model\": \"${parameters.model}\", \"input_type\": \"${parameters.input_type}\" }", |
| 56 | + "pre_process_function": "connector.pre_process.cohere.embedding", |
| 57 | + "post_process_function": "connector.post_process.cohere.embedding" |
| 58 | + } |
| 59 | + ] |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +This request response will return the `connector_id`, note it down. |
| 64 | + |
| 65 | +##### 1c. Register a model with your connector |
| 66 | + |
| 67 | +You can now register your model with the `model_group_id` and `connector_id` created from the previous steps. |
| 68 | + |
| 69 | +```json |
| 70 | +POST /_plugins/_ml/models/_register |
| 71 | +Content-Type: application/json |
| 72 | + |
| 73 | +{ |
| 74 | + "name": "Cohere Embed Model", |
| 75 | + "function_name": "remote", |
| 76 | + "model_group_id": "<MODEL_GROUP_ID>", |
| 77 | + "description": "Your Cohere Embedding Model", |
| 78 | + "connector_id": "<CONNECTOR_ID>" |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +This will create a registration task, the response should look like: |
| 83 | + |
| 84 | +```json |
| 85 | +{ |
| 86 | + "task_id": "9bXpRY0BRil1qhQaUK-u", |
| 87 | + "status": "CREATED", |
| 88 | + "model_id": "9rXpRY0BRil1qhQaUK_8" |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +##### 1d. Deploy model |
| 93 | + |
| 94 | +The last step is to deploy your model. Use the `model_id` returned by the registration request, and run: |
| 95 | + |
| 96 | +```json |
| 97 | +POST /_plugins/_ml/models/<MODEL_ID>/_deploy |
| 98 | +``` |
| 99 | + |
| 100 | +This will once again spawn a task to deploy your Model, with a response that will look like: |
| 101 | + |
| 102 | +```json |
| 103 | +{ |
| 104 | + "task_id": "97XrRY0BRil1qhQaQK_c", |
| 105 | + "task_type": "DEPLOY_MODEL", |
| 106 | + "status": "COMPLETED" |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +You can run the GET tasks request again to verify the status. |
| 111 | + |
| 112 | +```json |
| 113 | +GET /_plugins/_ml/tasks/<TASK_ID> |
| 114 | +``` |
| 115 | + |
| 116 | +Once this is complete, your Model is deployed and ready! |
| 117 | + |
| 118 | +##### 1e. Test model |
| 119 | + |
| 120 | +You can try this request to test that the Model behaves correctly: |
| 121 | + |
| 122 | +```json |
| 123 | +POST /_plugins/_ml/models/<MODEL_ID_HERE>/_predict |
| 124 | +{ |
| 125 | + "parameters": { |
| 126 | + "texts": ["Say this is a test"] |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +It should return a response similar to this: |
| 132 | + |
| 133 | +```json |
| 134 | +{ |
| 135 | + "inference_results": [ |
| 136 | + { |
| 137 | + "output": [ |
| 138 | + { |
| 139 | + "name": "sentence_embedding", |
| 140 | + "data_type": "FLOAT32", |
| 141 | + "shape": [ |
| 142 | + 1024 |
| 143 | + ], |
| 144 | + "data": [ |
| 145 | + -0.0024547577, |
| 146 | + 0.0062217712, |
| 147 | + -0.01675415, |
| 148 | + -0.020736694, |
| 149 | + -0.020263672, |
| 150 | + ... ... |
| 151 | + 0.038635254 |
| 152 | + ] |
| 153 | + } |
| 154 | + ], |
| 155 | + "status_code": 200 |
| 156 | + } |
| 157 | + ] |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +#### (Optional) 2. Setup k-NN index and ingestion pipeline |
| 162 | + |
| 163 | +##### 2a. Create your pipeline |
| 164 | + |
| 165 | +It is important that the `field_map` parameter contains all the document fields you'd like to embed as a vector. The key value is the document field name, and the value will be the field containing the embedding. |
| 166 | + |
| 167 | +```json |
| 168 | +PUT /_ingest/pipeline/cohere-ingest-pipeline |
| 169 | +{ |
| 170 | + "description": "Test Cohere Embedding pipeline", |
| 171 | + "processors": [ |
| 172 | + { |
| 173 | + "text_embedding": { |
| 174 | + "model_id": "<MODEL_ID>", |
| 175 | + "field_map": { |
| 176 | + "passage_text": "passage_embedding" |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + ] |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +Sample response: |
| 185 | + |
| 186 | +```json |
| 187 | +{ |
| 188 | + "acknowledged": true |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +##### 2b. Create a k-NN index |
| 193 | + |
| 194 | +Here `cohere-nlp-index` is the name of your index, you can change it as needed. |
| 195 | + |
| 196 | +````json |
| 197 | +PUT /cohere-nlp-index |
| 198 | + |
| 199 | +{ |
| 200 | + "settings": { |
| 201 | + "index.knn": true, |
| 202 | + "default_pipeline": "cohere-ingest-pipeline" |
| 203 | + }, |
| 204 | + "mappings": { |
| 205 | + "properties": { |
| 206 | + "id": { |
| 207 | + "type": "text" |
| 208 | + }, |
| 209 | + "passage_embedding": { |
| 210 | + "type": "knn_vector", |
| 211 | + "dimension": 1024, |
| 212 | + "method": { |
| 213 | + "engine": "lucene", |
| 214 | + "space_type": "l2", |
| 215 | + "name": "hnsw", |
| 216 | + "parameters": {} |
| 217 | + } |
| 218 | + }, |
| 219 | + "passage_text": { |
| 220 | + "type": "text" |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +Sample response: |
| 227 | + |
| 228 | +```json |
| 229 | +{ |
| 230 | + "acknowledged": true, |
| 231 | + "shards_acknowledged": true, |
| 232 | + "index": "cohere-nlp-index" |
| 233 | +} |
| 234 | +```` |
| 235 | + |
| 236 | +##### 2c. Testing the index and pipeline |
| 237 | + |
| 238 | +First, you can insert a record: |
| 239 | + |
| 240 | +```json |
| 241 | +PUT /cohere-nlp-index/_doc/1 |
| 242 | +{ |
| 243 | + "passage_text": "Hi - Cohere Embeddings are cool!", |
| 244 | + "id": "c1" |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +Sample response: |
| 249 | + |
| 250 | +```json |
| 251 | +{ |
| 252 | + "_index": "cohere-nlp-index", |
| 253 | + "_id": "1", |
| 254 | + "_version": 1, |
| 255 | + "result": "created", |
| 256 | + "_shards": { |
| 257 | + "total": 2, |
| 258 | + "successful": 1, |
| 259 | + "failed": 0 |
| 260 | + }, |
| 261 | + "_seq_no": 0, |
| 262 | + "_primary_term": 1 |
| 263 | +} |
| 264 | +``` |
| 265 | + |
| 266 | +The last step is to check that the embeddings were properly created. Notice that the embedding field created corresponds to the `field_map` mapping you defined in step 3a. |
| 267 | + |
| 268 | +```json |
| 269 | +GET /cohere-nlp-index/\_search |
| 270 | + |
| 271 | +{ |
| 272 | + "query": { |
| 273 | + "match_all": {} |
| 274 | + } |
| 275 | +} |
| 276 | +``` |
| 277 | + |
| 278 | +Sample response: |
| 279 | + |
| 280 | +```json |
| 281 | +{ |
| 282 | + "took": 2, |
| 283 | + "timed_out": false, |
| 284 | + "_shards": { |
| 285 | + "total": 1, |
| 286 | + "successful": 1, |
| 287 | + "skipped": 0, |
| 288 | + "failed": 0 |
| 289 | + }, |
| 290 | + "hits": { |
| 291 | + "total": { |
| 292 | + "value": 1, |
| 293 | + "relation": "eq" |
| 294 | + }, |
| 295 | + "max_score": 1, |
| 296 | + "hits": [ |
| 297 | + { |
| 298 | + "_index": "cohere-nlp-index", |
| 299 | + "_id": "1", |
| 300 | + "_score": 1, |
| 301 | + "_source": { |
| 302 | + "passage_text": "Hi - Cohere Embeddings are cool!", |
| 303 | + "passage_embedding": [ |
| 304 | + 0.02494812, |
| 305 | + -0.009391785, |
| 306 | + -0.015716553, |
| 307 | + -0.051849365, |
| 308 | + -0.015930176, |
| 309 | + -0.024734497, |
| 310 | + -0.028518677, |
| 311 | + -0.008323669, |
| 312 | + -0.008323669, |
| 313 | + ............. |
| 314 | + |
| 315 | + ], |
| 316 | + "id": "c1" |
| 317 | + } |
| 318 | + } |
| 319 | + ] |
| 320 | + } |
| 321 | +} |
| 322 | +``` |
| 323 | + |
| 324 | +Congratulations! You've successfully created your ingestion pipeline. |
0 commit comments