From 1a5618e11279f11cc55baf586e58f625e3128f6c Mon Sep 17 00:00:00 2001 From: Gaurav Kumar Sinha Date: Mon, 25 May 2026 21:29:08 -0500 Subject: [PATCH 1/2] Add Bedrock Cohere Rerank v3.5 connector blueprint Add a new connector blueprint for the Cohere Rerank v3.5 model on Amazon Bedrock. This enables OpenSearch users to leverage Bedrock's reranking capabilities for improving search relevance in RAG pipelines. The blueprint includes: - Self-managed OpenSearch configuration with AWS credentials - AWS OpenSearch Service configuration with IAM role - Built-in pre/post processing functions (connector.pre_process.cohere.rerank and connector.post_process.cohere.rerank) for OpenSearch 2.19+ - Custom Painless scripts for earlier OpenSearch versions - Model registration, deployment, and inference testing - Integration with OpenSearch reranking search pipeline - query_text_path usage to avoid duplicate query specification Resolves #4831 Related: #3254, #3396 Signed-off-by: Gaurav Kumar Sinha --- ...drock_connector_cohere_rerank_blueprint.md | 333 ++++++++++++++++++ 1 file changed, 333 insertions(+) create mode 100644 docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md diff --git a/docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md b/docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md new file mode 100644 index 0000000000..20ae349fa0 --- /dev/null +++ b/docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md @@ -0,0 +1,333 @@ +# Bedrock connector blueprint example for Cohere Rerank model + +This blueprint shows how to create a connector for the [Cohere Rerank v3.5](https://docs.aws.amazon.com/bedrock/latest/userguide/rerank.html) model on Amazon Bedrock. The Cohere Rerank model calculates relevance scores for documents with respect to a query, enabling you to reorder search results for improved accuracy in RAG pipelines and enterprise search. + +For a complete end-to-end tutorial including search pipeline setup, see [Reranking search results using Cohere Rerank on Amazon Bedrock](https://opensearch.org/docs/latest/tutorials/reranking/reranking-cohere-bedrock/). + +## 1. Add connector endpoint to trusted URLs: + +Note: no need to do this after 2.11.0 + +```json +PUT /_cluster/settings +{ + "persistent": { + "plugins.ml_commons.trusted_connector_endpoints_regex": [ + "^https://bedrock-runtime\\..*[a-z0-9-]\\.amazonaws\\.com/.*$" + ] + } +} +``` + +## 2. Create connector for Amazon Bedrock: + +### 2.1 Self-managed OpenSearch with AWS credentials + +If you are using self-managed OpenSearch, you should supply AWS credentials: + +```json +POST /_plugins/_ml/connectors/_create +{ + "name": "Amazon Bedrock Connector: Cohere Rerank", + "description": "Connector for Cohere Rerank v3.5 model on Amazon Bedrock", + "version": 1, + "protocol": "aws_sigv4", + "credential": { + "access_key": "", + "secret_key": "", + "session_token": "" + }, + "parameters": { + "service_name": "bedrock", + "endpoint": "bedrock-runtime", + "region": "", + "model_name": "cohere.rerank-v3-5:0", + "api_version": 2 + }, + "actions": [ + { + "action_type": "predict", + "method": "POST", + "headers": { + "x-amz-content-sha256": "required", + "content-type": "application/json" + }, + "url": "https://${parameters.endpoint}.${parameters.region}.amazonaws.com/model/${parameters.model_name}/invoke", + "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": ${parameters.api_version} }", + "pre_process_function": "connector.pre_process.cohere.rerank", + "post_process_function": "connector.post_process.cohere.rerank" + } + ] +} +``` + +### 2.2 AWS OpenSearch Service with IAM role + +If using the AWS OpenSearch Service, you can provide an IAM role ARN that allows access to the Amazon Bedrock service. +Refer to this [AWS doc](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ml-amazon-connector.html). + +```json +POST /_plugins/_ml/connectors/_create +{ + "name": "Amazon Bedrock Connector: Cohere Rerank", + "description": "Connector for Cohere Rerank v3.5 model on Amazon Bedrock", + "version": 1, + "protocol": "aws_sigv4", + "credential": { + "roleArn": "" + }, + "parameters": { + "service_name": "bedrock", + "endpoint": "bedrock-runtime", + "region": "", + "model_name": "cohere.rerank-v3-5:0", + "api_version": 2 + }, + "actions": [ + { + "action_type": "predict", + "method": "POST", + "headers": { + "x-amz-content-sha256": "required", + "content-type": "application/json" + }, + "url": "https://${parameters.endpoint}.${parameters.region}.amazonaws.com/model/${parameters.model_name}/invoke", + "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": ${parameters.api_version} }", + "pre_process_function": "connector.pre_process.cohere.rerank", + "post_process_function": "connector.post_process.cohere.rerank" + } + ] +} +``` + +Sample response: +```json +{ + "connector_id": "aB1cD2eF3gH4iJ5kL6mN" +} +``` + +### 2.3 Using custom Painless scripts (for OpenSearch versions before built-in function support) + +If your OpenSearch version does not support the built-in `connector.pre_process.cohere.rerank` and `connector.post_process.cohere.rerank` functions, you can use custom Painless scripts instead: + +```json +POST /_plugins/_ml/connectors/_create +{ + "name": "Amazon Bedrock Connector: Cohere Rerank", + "description": "Connector for Cohere Rerank v3.5 model on Amazon Bedrock", + "version": 1, + "protocol": "aws_sigv4", + "credential": { + "access_key": "", + "secret_key": "", + "session_token": "" + }, + "parameters": { + "service_name": "bedrock", + "endpoint": "bedrock-runtime", + "region": "", + "model_name": "cohere.rerank-v3-5:0", + "api_version": 2 + }, + "actions": [ + { + "action_type": "predict", + "method": "POST", + "headers": { + "x-amz-content-sha256": "required", + "content-type": "application/json" + }, + "url": "https://${parameters.endpoint}.${parameters.region}.amazonaws.com/model/${parameters.model_name}/invoke", + "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": ${parameters.api_version} }", + "pre_process_function": "\n def query_text = params.query_text;\n def text_docs = params.text_docs;\n def textDocsBuilder = new StringBuilder('[');\n for (int i=0; i Date: Mon, 25 May 2026 23:55:50 -0500 Subject: [PATCH 2/2] Address PR review: hardcode api_version, escape special chars in Painless script - Hardcode api_version as 2 in request_body instead of using parameter substitution, since the value is fixed for this model - Add .replace() calls in pre_process_function to escape backslashes and double quotes in query_text and text_docs, preventing malformed JSON when documents contain special characters - Add security note recommending IAM roles or Secrets Manager over hardcoded credentials Signed-off-by: Gaurav Kumar Sinha --- ...drock_connector_cohere_rerank_blueprint.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md b/docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md index 20ae349fa0..d2e8199707 100644 --- a/docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md +++ b/docs/remote_inference_blueprints/bedrock_connector_cohere_rerank_blueprint.md @@ -41,8 +41,7 @@ POST /_plugins/_ml/connectors/_create "service_name": "bedrock", "endpoint": "bedrock-runtime", "region": "", - "model_name": "cohere.rerank-v3-5:0", - "api_version": 2 + "model_name": "cohere.rerank-v3-5:0" }, "actions": [ { @@ -53,7 +52,7 @@ POST /_plugins/_ml/connectors/_create "content-type": "application/json" }, "url": "https://${parameters.endpoint}.${parameters.region}.amazonaws.com/model/${parameters.model_name}/invoke", - "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": ${parameters.api_version} }", + "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": 2 }", "pre_process_function": "connector.pre_process.cohere.rerank", "post_process_function": "connector.post_process.cohere.rerank" } @@ -61,6 +60,8 @@ POST /_plugins/_ml/connectors/_create } ``` +> **Security note:** Do not commit real AWS credentials to version control. For production deployments, consider using IAM roles (section 2.2) or [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) to manage credentials securely. + ### 2.2 AWS OpenSearch Service with IAM role If using the AWS OpenSearch Service, you can provide an IAM role ARN that allows access to the Amazon Bedrock service. @@ -80,8 +81,7 @@ POST /_plugins/_ml/connectors/_create "service_name": "bedrock", "endpoint": "bedrock-runtime", "region": "", - "model_name": "cohere.rerank-v3-5:0", - "api_version": 2 + "model_name": "cohere.rerank-v3-5:0" }, "actions": [ { @@ -92,7 +92,7 @@ POST /_plugins/_ml/connectors/_create "content-type": "application/json" }, "url": "https://${parameters.endpoint}.${parameters.region}.amazonaws.com/model/${parameters.model_name}/invoke", - "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": ${parameters.api_version} }", + "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": 2 }", "pre_process_function": "connector.pre_process.cohere.rerank", "post_process_function": "connector.post_process.cohere.rerank" } @@ -127,8 +127,7 @@ POST /_plugins/_ml/connectors/_create "service_name": "bedrock", "endpoint": "bedrock-runtime", "region": "", - "model_name": "cohere.rerank-v3-5:0", - "api_version": 2 + "model_name": "cohere.rerank-v3-5:0" }, "actions": [ { @@ -139,8 +138,8 @@ POST /_plugins/_ml/connectors/_create "content-type": "application/json" }, "url": "https://${parameters.endpoint}.${parameters.region}.amazonaws.com/model/${parameters.model_name}/invoke", - "request_body": "{ \"documents\": ${parameters.documents}, \"query\": \"${parameters.query}\", \"api_version\": ${parameters.api_version} }", - "pre_process_function": "\n def query_text = params.query_text;\n def text_docs = params.text_docs;\n def textDocsBuilder = new StringBuilder('[');\n for (int i=0; i