|
| 1 | +# Topic |
| 2 | + |
| 3 | +> Remote model guardrails has been released in OpenSearch 2.15. For more information, see [Guardrails](https://opensearch.org/docs/latest/ml-commons-plugin/remote-models/guardrails/). |
| 4 | +
|
| 5 | +This tutorial demonstrates how to apply your Amazon Bedrock guardrails to your externally hosted models in two ways. |
| 6 | + |
| 7 | +Note: Replace the placeholders starting with the prefix `your_` with your own values. |
| 8 | + |
| 9 | +# Use the Bedrock Guardrails Independent API |
| 10 | + |
| 11 | +## 0. Preparation |
| 12 | +Create your own Amazon Bedrock guardrails. For more information, see [Create a guardrail](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-create.html). |
| 13 | + |
| 14 | +## 1. Create a connector with your Bedrock Guardrail endpoint |
| 15 | + |
| 16 | +``` |
| 17 | +POST _plugins/_ml/connectors/_create |
| 18 | +{ |
| 19 | + "name": "BedRock Guardrail Connector", |
| 20 | + "description": "BedRock Guardrail Connector", |
| 21 | + "version": 1, |
| 22 | + "protocol": "aws_sigv4", |
| 23 | + "parameters": { |
| 24 | + "region": "your_aws_region like us-east-1", |
| 25 | + "service_name": "bedrock", |
| 26 | + "source": "INPUT" |
| 27 | + }, |
| 28 | + "credential": { |
| 29 | + "access_key": "your_aws_access_key", |
| 30 | + "secret_key": "your_aws_secret_key", |
| 31 | + "session_token": "your_aws_session_token" |
| 32 | + }, |
| 33 | + "actions": [ |
| 34 | + { |
| 35 | + "action_type": "predict", |
| 36 | + "method": "POST", |
| 37 | + "url": "https://bedrock-runtime.${parameters.region}.amazonaws.com/guardrail/your_guardrailIdentifier/version/1/apply", |
| 38 | + "headers": { |
| 39 | + "content-type": "application/json" |
| 40 | + }, |
| 41 | + "request_body": "{\"source\":\"${parameters.source}\", \"content\":[ { \"text\":{\"text\": \"${parameters.question}\"} } ] }" |
| 42 | + } |
| 43 | + ] |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## 2. Create a remote guardrail model with your guardrail connector |
| 48 | + |
| 49 | +``` |
| 50 | +POST _plugins/_ml/models/_register |
| 51 | +{ |
| 52 | + "name": "bedrock test guardrail API", |
| 53 | + "function_name": "remote", |
| 54 | + "description": "guardrail test model", |
| 55 | + "connector_id": "your_guardrail_connector_id" |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## 3. Test the guardrail model |
| 60 | + |
| 61 | +``` |
| 62 | +POST _plugins/_ml/models/your_model_id/_predict |
| 63 | +{ |
| 64 | + "parameters": { |
| 65 | + "question": "\n\nHuman:how to suicide\n\nAssistant:" |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Response: |
| 71 | +``` |
| 72 | +{ |
| 73 | + "inference_results": [ |
| 74 | + { |
| 75 | + "output": [ |
| 76 | + { |
| 77 | + "name": "response", |
| 78 | + "dataAsMap": { |
| 79 | + "action": "GUARDRAIL_INTERVENED", |
| 80 | + "assessments": [ |
| 81 | + { |
| 82 | + "contentPolicy": { |
| 83 | + "filters": [ |
| 84 | + { |
| 85 | + "action": "BLOCKED", |
| 86 | + "confidence": "HIGH", |
| 87 | + "type": "VIOLENCE" |
| 88 | + }, |
| 89 | + { |
| 90 | + "action": "BLOCKED", |
| 91 | + "confidence": "HIGH", |
| 92 | + "type": "PROMPT_ATTACK" |
| 93 | + } |
| 94 | + ] |
| 95 | + }, |
| 96 | + "wordPolicy": { |
| 97 | + "customWords": [ |
| 98 | + { |
| 99 | + "action": "BLOCKED", |
| 100 | + "match": "suicide" |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + } |
| 105 | + ], |
| 106 | + "blockedResponse": "Sorry, the model cannot answer this question.", |
| 107 | + "output": [ |
| 108 | + { |
| 109 | + "text": "Sorry, the model cannot answer this question." |
| 110 | + } |
| 111 | + ], |
| 112 | + "outputs": [ |
| 113 | + { |
| 114 | + "text": "Sorry, the model cannot answer this question." |
| 115 | + } |
| 116 | + ], |
| 117 | + "usage": { |
| 118 | + "contentPolicyUnits": 1.0, |
| 119 | + "contextualGroundingPolicyUnits": 0.0, |
| 120 | + "sensitiveInformationPolicyFreeUnits": 0.0, |
| 121 | + "sensitiveInformationPolicyUnits": 0.0, |
| 122 | + "topicPolicyUnits": 1.0, |
| 123 | + "wordPolicyUnits": 1.0 |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + ], |
| 128 | + "status_code": 200 |
| 129 | + } |
| 130 | + ] |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## 4. Create a connector with your Bedrock Claude endpoint |
| 135 | + |
| 136 | +``` |
| 137 | +POST _plugins/_ml/connectors/_create |
| 138 | +{ |
| 139 | + "name": "BedRock claude Connector", |
| 140 | + "description": "BedRock claude Connector", |
| 141 | + "version": 1, |
| 142 | + "protocol": "aws_sigv4", |
| 143 | + "parameters": { |
| 144 | + "region": "your_aws_region like us-east-1", |
| 145 | + "service_name": "bedrock", |
| 146 | + "anthropic_version": "bedrock-2023-05-31", |
| 147 | + "max_tokens_to_sample": 8000, |
| 148 | + "temperature": 0.0001, |
| 149 | + "response_filter": "$.completion" |
| 150 | + }, |
| 151 | + "credential": { |
| 152 | + "access_key": "your_aws_access_key", |
| 153 | + "secret_key": "your_aws_secret_key", |
| 154 | + "session_token": "your_aws_session_token" |
| 155 | + }, |
| 156 | + "actions": [ |
| 157 | + { |
| 158 | + "action_type": "predict", |
| 159 | + "method": "POST", |
| 160 | + "url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-v2/invoke", |
| 161 | + "headers": { |
| 162 | + "content-type": "application/json", |
| 163 | + "x-amz-content-sha256": "required" |
| 164 | + }, |
| 165 | + "request_body": "{\"prompt\":\"${parameters.prompt}\", \"max_tokens_to_sample\":${parameters.max_tokens_to_sample}, \"temperature\":${parameters.temperature}, \"anthropic_version\":\"${parameters.anthropic_version}\" }" |
| 166 | + } |
| 167 | + ] |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## 5. Create a Bedrock Claude model |
| 172 | + |
| 173 | +``` |
| 174 | +POST /_plugins/_ml/models/_register?deploy=true |
| 175 | +{ |
| 176 | + "name": "Bedrock Claude V2 model", |
| 177 | + "function_name": "remote", |
| 178 | + "description": "Bedrock Claude V2 model", |
| 179 | + "connector_id": "your_connector_id", |
| 180 | + "guardrails": { |
| 181 | + "input_guardrail": { |
| 182 | + "model_id": "your_guardrail_model_id", |
| 183 | + "response_filter":"$.action", |
| 184 | + "response_validation_regex": "^\"NONE\"$" |
| 185 | + }, |
| 186 | + "type": "model" |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +## 6. Test the model |
| 192 | + |
| 193 | +``` |
| 194 | +POST /_plugins/_ml/models/your_model_id/_predict |
| 195 | +{ |
| 196 | + "parameters": { |
| 197 | + "prompt": "\n\nHuman:${parameters.question}\n\nnAssistant:", |
| 198 | + "question": "hello" |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +Response: |
| 204 | + |
| 205 | +``` |
| 206 | +{ |
| 207 | + "inference_results": [ |
| 208 | + { |
| 209 | + "output": [ |
| 210 | + { |
| 211 | + "name": "response", |
| 212 | + "dataAsMap": { |
| 213 | + "response": " Hello!" |
| 214 | + } |
| 215 | + } |
| 216 | + ], |
| 217 | + "status_code": 200 |
| 218 | + } |
| 219 | + ] |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | + |
| 224 | +``` |
| 225 | +POST /_plugins/_ml/models/your_model_id/_predict |
| 226 | +{ |
| 227 | + "parameters": { |
| 228 | + "prompt": "\n\nHuman:${parameters.question}\n\nnAssistant:", |
| 229 | + "question": "how to suicide" |
| 230 | + } |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +Response: |
| 235 | + |
| 236 | +``` |
| 237 | +{ |
| 238 | + "error": { |
| 239 | + "root_cause": [ |
| 240 | + { |
| 241 | + "type": "illegal_argument_exception", |
| 242 | + "reason": "guardrails triggered for user input" |
| 243 | + } |
| 244 | + ], |
| 245 | + "type": "illegal_argument_exception", |
| 246 | + "reason": "guardrails triggered for user input" |
| 247 | + }, |
| 248 | + "status": 400 |
| 249 | +} |
| 250 | +``` |
| 251 | + |
| 252 | +# Use the guardrails embedded in the Amazon Bedrock Model Inference API |
| 253 | + |
| 254 | +## 0. Preparation |
| 255 | +Create your own Amazon Bedrock guardrails. For more information, see [Create a guardrail](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-create.html). |
| 256 | + |
| 257 | +## 1. Create a connector for an Amazon Bedrock model with guardrail headers |
| 258 | + |
| 259 | +``` |
| 260 | +POST /_plugins/_ml/connectors/_create |
| 261 | +{ |
| 262 | + "name": "BedRock claude Connector", |
| 263 | + "description": "BedRock claude Connector", |
| 264 | + "version": 1, |
| 265 | + "protocol": "aws_sigv4", |
| 266 | + "parameters": { |
| 267 | + "region": "your_aws_region like us-east-1", |
| 268 | + "service_name": "bedrock", |
| 269 | + "max_tokens_to_sample": 8000, |
| 270 | + "temperature": 0.0001 |
| 271 | + }, |
| 272 | + "credential": { |
| 273 | + "access_key": "your_aws_access_key", |
| 274 | + "secret_key": "your_aws_secret_key", |
| 275 | + "session_token": "your_aws_session_token" |
| 276 | + }, |
| 277 | + "actions": [ |
| 278 | + { |
| 279 | + "action_type": "predict", |
| 280 | + "method": "POST", |
| 281 | + "url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-v2/invoke", |
| 282 | + "headers": { |
| 283 | + "content-type": "application/json", |
| 284 | + "x-amz-content-sha256": "required", |
| 285 | + "X-Amzn-Bedrock-Trace": "ENABLED", |
| 286 | + "X-Amzn-Bedrock-GuardrailIdentifier": "your_GuardrailIdentifier", |
| 287 | + "X-Amzn-Bedrock-GuardrailVersion": "your_bedrock_guardrail_version" |
| 288 | + }, |
| 289 | + "request_body": "{\"prompt\":\"${parameters.prompt}\", \"max_tokens_to_sample\":${parameters.max_tokens_to_sample}, \"temperature\":${parameters.temperature}, \"anthropic_version\":\"${parameters.anthropic_version}\" }", |
| 290 | + "post_process_function": "\n if (params['amazon-bedrock-guardrailAction']=='INTERVENED') throw new IllegalArgumentException(\"test guardrail from post process function\");\n " |
| 291 | + } |
| 292 | + ] |
| 293 | +} |
| 294 | +``` |
| 295 | +A `post_process_function` is required to define the logic for blocking the input by the guardrail. |
| 296 | + |
| 297 | +## 2. Create a model |
| 298 | + |
| 299 | +``` |
| 300 | +POST _plugins/_ml/models/_register |
| 301 | +{ |
| 302 | + "name": "bedrock model with guardrails", |
| 303 | + "function_name": "remote", |
| 304 | + "description": "guardrails test model", |
| 305 | + "connector_id": "your_connector_id" |
| 306 | +} |
| 307 | +``` |
| 308 | + |
| 309 | +## 3. Test the model |
| 310 | + |
| 311 | +``` |
| 312 | +POST _plugins/_ml/models/your_model_id/_predict |
| 313 | +{ |
| 314 | + "parameters": { |
| 315 | + "input": "\n\nHuman:how to suicide\n\nAssistant:" |
| 316 | + } |
| 317 | +} |
| 318 | +``` |
| 319 | + |
| 320 | +Response: |
| 321 | +``` |
| 322 | +{ |
| 323 | + "error": { |
| 324 | + "root_cause": [ |
| 325 | + { |
| 326 | + "type": "m_l_exception", |
| 327 | + "reason": "Fail to execute predict in aws connector" |
| 328 | + } |
| 329 | + ], |
| 330 | + "type": "m_l_exception", |
| 331 | + "reason": "Fail to execute predict in aws connector", |
| 332 | + "caused_by": { |
| 333 | + "type": "script_exception", |
| 334 | + "reason": "runtime error", |
| 335 | + "script_stack": [ |
| 336 | + "throw new IllegalArgumentException(\"test guardrail from post process function\");\n ", |
| 337 | + " ^---- HERE" |
| 338 | + ], |
| 339 | + "script": " ...", |
| 340 | + "lang": "painless", |
| 341 | + "position": { |
| 342 | + "offset": 73, |
| 343 | + "start": 67, |
| 344 | + "end": 152 |
| 345 | + }, |
| 346 | + "caused_by": { |
| 347 | + "type": "illegal_argument_exception", |
| 348 | + "reason": "test guardrail from post process function" |
| 349 | + } |
| 350 | + } |
| 351 | + }, |
| 352 | + "status": 500 |
| 353 | +} |
| 354 | +``` |
0 commit comments