|
| 1 | +# Bedrock connector standard blueprint example for Cohere text embedding model |
| 2 | +This blueprint demonstrates how to deploy a cohere.embed-english-v3 using the Bedrock connector without pre and post processing functions. |
| 3 | +This is recommended for version after OS 2.14.0 for models to use the ML inference processor to handle input/output mapping. |
| 4 | +Note that if using a model that requires pre and post processing functions, you must provide the functions in the blueprint. Please refer to legacy blueprint: [Bedrock connector blueprint example for Cohere embed-english-v3 model](https://github.com/opensearch-project/ml-commons/blob/main/docs/remote_inference_blueprints/bedrock_connector_cohere_cohere.embed-english-v3_blueprint.md) |
| 5 | + |
| 6 | +## 1. Create connector for Amazon Bedrock: |
| 7 | + |
| 8 | +If you are using self-managed Opensearch, you should supply AWS credentials: |
| 9 | + |
| 10 | +```json |
| 11 | +POST /_plugins/_ml/connectors/_create |
| 12 | +{ |
| 13 | + "name": "Amazon Bedrock Connector: Cohere embed-english-v3", |
| 14 | + "description": "Test connector for Amazon Bedrock Cohere embed-english-v3", |
| 15 | + "version": 1, |
| 16 | + "protocol": "aws_sigv4", |
| 17 | + "credential": { |
| 18 | + "access_key": "<PLEASE ADD YOUR AWS ACCESS KEY HERE>", |
| 19 | + "secret_key": "<PLEASE ADD YOUR AWS SECRET KEY HERE>", |
| 20 | + "session_token": "<PLEASE ADD YOUR AWS SECURITY TOKEN HERE>" |
| 21 | + }, |
| 22 | + "parameters": { |
| 23 | + "region": "<PLEASE ADD YOUR AWS REGION HERE>", |
| 24 | + "service_name": "bedrock", |
| 25 | + "truncate": "<NONE|START|END>", |
| 26 | + "input_type": "<search_document|search_query|classification|clustering>", |
| 27 | + "model": "cohere.embed-english-v3" |
| 28 | + }, |
| 29 | + "actions": [ |
| 30 | + { |
| 31 | + "action_type": "predict", |
| 32 | + "method": "POST", |
| 33 | + "headers": { |
| 34 | + "x-amz-content-sha256": "required", |
| 35 | + "content-type": "application/json" |
| 36 | + }, |
| 37 | + "url": "https://bedrock-runtime.${parameters.region}.amazonaws.com/model/${parameters.model}/invoke", |
| 38 | + "request_body": "{ \"texts\": ${parameters.texts}, \"truncate\": \"${parameters.truncate}\", \"input_type\": \"${parameters.input_type}\" }" |
| 39 | + } |
| 40 | + ] |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +If using the AWS Opensearch Service, you can provide an IAM role arn that allows access to the bedrock service. |
| 45 | +Refer to this [AWS doc](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ml-amazon-connector.html) |
| 46 | + |
| 47 | +```json |
| 48 | +POST /_plugins/_ml/connectors/_create |
| 49 | +{ |
| 50 | + "name": "Amazon Bedrock Connector: Cohere embed-english-v3", |
| 51 | + "description": "Test connector for Amazon Bedrock Cohere embed-english-v3 model", |
| 52 | + "version": 1, |
| 53 | + "protocol": "aws_sigv4", |
| 54 | + "credential": { |
| 55 | + "roleArn": "<PLEASE ADD YOUR AWS ROLE ARN HERE>" |
| 56 | + }, |
| 57 | + "parameters": { |
| 58 | + "region": "<PLEASE ADD YOUR AWS REGION HERE>", |
| 59 | + "service_name": "bedrock", |
| 60 | + "truncate": "<NONE|START|END>", |
| 61 | + "input_type": "<search_document|search_query|classification|clustering>", |
| 62 | + "model": "cohere.embed-english-v3" |
| 63 | + }, |
| 64 | + "actions": [ |
| 65 | + { |
| 66 | + "action_type": "predict", |
| 67 | + "method": "POST", |
| 68 | + "headers": { |
| 69 | + "x-amz-content-sha256": "required", |
| 70 | + "content-type": "application/json" |
| 71 | + }, |
| 72 | + "url": "https://bedrock-runtime.${parameters.region}.amazonaws.com/model/${parameters.model}/invoke", |
| 73 | + "request_body": "{ \"texts\": ${parameters.texts}, \"truncate\": \"${parameters.truncate}\", \"input_type\": \"${parameters.input_type}\" }" |
| 74 | + } |
| 75 | + ] |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Sample response: |
| 80 | +```json |
| 81 | +{ |
| 82 | + "connector_id": "hijwwZABNrAVdFa9prf7" |
| 83 | +} |
| 84 | +``` |
| 85 | +For more information of the model inference parameters in the connector, please refer to this [AWS doc](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-embed.html) |
| 86 | + |
| 87 | +## 2. Create model group(Optional): |
| 88 | + |
| 89 | +```json |
| 90 | +POST /_plugins/_ml/model_groups/_register |
| 91 | +{ |
| 92 | + "name": "remote_model_group_cohere", |
| 93 | + "description": "model group for cohere models" |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Sample response: |
| 98 | +```json |
| 99 | +{ |
| 100 | + "model_group_id": "IMobmY8B8aiZvtEZeO_i", |
| 101 | + "status": "CREATED" |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## 3. Register model to model group & deploy model: |
| 106 | + |
| 107 | +```json |
| 108 | +POST /_plugins/_ml/models/_register?deploy=true |
| 109 | +{ |
| 110 | + "name": "cohere.embed-english-v3", |
| 111 | + "function_name": "remote", |
| 112 | + "model_group_id": "IMobmY8B8aiZvtEZeO_i", |
| 113 | + "description": "cohere embed-english v3 model", |
| 114 | + "connector_id": "hijwwZABNrAVdFa9prf7" |
| 115 | +} |
| 116 | +``` |
| 117 | +Sample response: |
| 118 | +```json |
| 119 | +{ |
| 120 | + "task_id": "rMormY8B8aiZvtEZIO_j", |
| 121 | + "status": "CREATED", |
| 122 | + "model_id": "lyjxwZABNrAVdFa9zrcZ" |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## 4. Test model inference |
| 127 | + |
| 128 | +```json |
| 129 | +POST /_plugins/_ml/models/lyjxwZABNrAVdFa9zrcZ/_predict |
| 130 | +{ |
| 131 | + "parameters": { |
| 132 | + "texts" : ["Hello World", "This is a test"] |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +Sample response: |
| 138 | +```json |
| 139 | +{ |
| 140 | + "inference_results": [ |
| 141 | + { |
| 142 | + "output": [ |
| 143 | + { |
| 144 | + "name": "response", |
| 145 | + "dataAsMap": { |
| 146 | + "id": "74372fce-e09d-483e-af6a-b45c84e3999e", |
| 147 | + "texts": [ |
| 148 | + "hello world", |
| 149 | + "this is a test" |
| 150 | + ], |
| 151 | + "embeddings": [ |
| 152 | + [ |
| 153 | + -0.029205322, |
| 154 | + -0.02357483, |
| 155 | + -0.05987549, |
| 156 | + ... |
| 157 | + ], |
| 158 | + [ |
| 159 | + -0.013885498, |
| 160 | + 0.009994507, |
| 161 | + -0.03253174, |
| 162 | + ... |
| 163 | + ] |
| 164 | + ], |
| 165 | + "response_type": "embeddings_floats" |
| 166 | + } |
| 167 | + } |
| 168 | + ], |
| 169 | + "status_code": 200 |
| 170 | + } |
| 171 | + ] |
| 172 | +} |
| 173 | +``` |
| 174 | + |
0 commit comments