From 0f8c3ebdae99504aae4b748bbec233a031aff5bf Mon Sep 17 00:00:00 2001 From: zane-neo Date: Tue, 8 Oct 2024 14:10:43 +0800 Subject: [PATCH 1/2] Add bedrock multimodel build-in function usage example in ddoc Signed-off-by: zane-neo --- ...or_titan_multimodal_embedding_blueprint.md | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md b/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md index 928b226cc0..1f14f5c9e8 100644 --- a/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md +++ b/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md @@ -17,8 +17,9 @@ PUT /_cluster/settings ## 2. Create connector for Amazon Bedrock: -If you are using self-managed Opensearch, you should supply AWS credentials: - +If you are using self-managed Opensearch, you should supply AWS credentials. +You have two different approaches to specify the pre&post process function and request body in the API: +**Use build-in function** ```json POST /_plugins/_ml/connectors/_create { @@ -46,6 +47,21 @@ POST /_plugins/_ml/connectors/_create "content-type": "application/json", "x-amz-content-sha256": "required" }, + "request_body": "{\"body\":{\"inputText\": \"${parameters.inputText}\", \"inputImage\": \"${parameters.inputImage}\"}}", + "pre_process_function": "connector.pre_process.bedrock.multimodal_embedding", + "post_process_function": "connector.post_process.bedrock.embedding" + } + ] +} +``` +**Use painless script** +```json +POST /_plugins/_ml/connectors/_create +{ + ... //same above + "actions": [ + { + ... // same above "request_body": "{ \"inputText\": \"${parameters.inputText:-null}\", \"inputImage\": \"${parameters.inputImage:-null}\" }", "pre_process_function": "\n StringBuilder parametersBuilder = new StringBuilder(\"{\");\n if (params.text_docs.length > 0 && params.text_docs[0] != null) {\n parametersBuilder.append(\"\\\"inputText\\\":\");\n parametersBuilder.append(\"\\\"\");\n parametersBuilder.append(params.text_docs[0]);\n parametersBuilder.append(\"\\\"\");\n \n if (params.text_docs.length > 1 && params.text_docs[1] != null) {\n parametersBuilder.append(\",\");\n }\n }\n \n \n if (params.text_docs.length > 1 && params.text_docs[1] != null) {\n parametersBuilder.append(\"\\\"inputImage\\\":\");\n parametersBuilder.append(\"\\\"\");\n parametersBuilder.append(params.text_docs[1]);\n parametersBuilder.append(\"\\\"\");\n }\n parametersBuilder.append(\"}\");\n \n return \"{\" +\"\\\"parameters\\\":\" + parametersBuilder + \"}\";", "post_process_function": "\n def name = \"sentence_embedding\";\n def dataType = \"FLOAT32\";\n if (params.embedding == null || params.embedding.length == 0) {\n return null;\n }\n def shape = [params.embedding.length];\n def json = \"{\" +\n \"\\\"name\\\":\\\"\" + name + \"\\\",\" +\n \"\\\"data_type\\\":\\\"\" + dataType + \"\\\",\" +\n \"\\\"shape\\\":\" + shape + \",\" +\n \"\\\"data\\\":\" + params.embedding +\n \"}\";\n return json;\n " @@ -55,8 +71,9 @@ POST /_plugins/_ml/connectors/_create ``` If using the AWS Opensearch Service, you can provide an IAM role arn that allows access to the bedrock service. -Refer to this [AWS doc](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ml-amazon-connector.html) - +Refer to this [AWS doc](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ml-amazon-connector.html) +You have two different approaches to specify the pre&post process function and request body in the API: +**Use build-in function** ```json POST /_plugins/_ml/connectors/_create { @@ -82,6 +99,21 @@ POST /_plugins/_ml/connectors/_create "content-type": "application/json", "x-amz-content-sha256": "required" }, + "request_body": "{\"body\":{\"inputText\": \"${parameters.inputText}\", \"inputImage\": \"${parameters.inputImage}\"}}", + "pre_process_function": "connector.pre_process.bedrock.multimodal_embedding", + "post_process_function": "connector.post_process.bedrock.embedding" + } + ] +} +``` +**Use painless script** +```json +POST /_plugins/_ml/connectors/_create +{ + ... //same above + "actions": [ + { + ... //same above "request_body": "{ \"inputText\": \"${parameters.inputText:-null}\", \"inputImage\": \"${parameters.inputImage:-null}\" }", "pre_process_function": "\n StringBuilder parametersBuilder = new StringBuilder(\"{\");\n if (params.text_docs.length > 0 && params.text_docs[0] != null) {\n parametersBuilder.append(\"\\\"inputText\\\":\");\n parametersBuilder.append(\"\\\"\");\n parametersBuilder.append(params.text_docs[0]);\n parametersBuilder.append(\"\\\"\");\n \n if (params.text_docs.length > 1 && params.text_docs[1] != null) {\n parametersBuilder.append(\",\");\n }\n }\n \n \n if (params.text_docs.length > 1 && params.text_docs[1] != null) {\n parametersBuilder.append(\"\\\"inputImage\\\":\");\n parametersBuilder.append(\"\\\"\");\n parametersBuilder.append(params.text_docs[1]);\n parametersBuilder.append(\"\\\"\");\n }\n parametersBuilder.append(\"}\");\n \n return \"{\" +\"\\\"parameters\\\":\" + parametersBuilder + \"}\";", "post_process_function": "\n def name = \"sentence_embedding\";\n def dataType = \"FLOAT32\";\n if (params.embedding == null || params.embedding.length == 0) {\n return null;\n }\n def shape = [params.embedding.length];\n def json = \"{\" +\n \"\\\"name\\\":\\\"\" + name + \"\\\",\" +\n \"\\\"data_type\\\":\\\"\" + dataType + \"\\\",\" +\n \"\\\"shape\\\":\" + shape + \",\" +\n \"\\\"data\\\":\" + params.embedding +\n \"}\";\n return json;\n " From 44b5c83fe9c017685afdf9495d4ca115ae735f91 Mon Sep 17 00:00:00 2001 From: zane-neo Date: Thu, 10 Oct 2024 16:39:00 +0800 Subject: [PATCH 2/2] Add multimodal starting support version in doc Signed-off-by: zane-neo --- .../bedrock_connector_titan_multimodal_embedding_blueprint.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md b/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md index 1f14f5c9e8..092d4781b7 100644 --- a/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md +++ b/docs/remote_inference_blueprints/bedrock_connector_titan_multimodal_embedding_blueprint.md @@ -19,7 +19,9 @@ PUT /_cluster/settings If you are using self-managed Opensearch, you should supply AWS credentials. You have two different approaches to specify the pre&post process function and request body in the API: + **Use build-in function** +> You can use this starting from OpenSearch 2.16 ```json POST /_plugins/_ml/connectors/_create { @@ -73,7 +75,9 @@ POST /_plugins/_ml/connectors/_create If using the AWS Opensearch Service, you can provide an IAM role arn that allows access to the bedrock service. Refer to this [AWS doc](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ml-amazon-connector.html) You have two different approaches to specify the pre&post process function and request body in the API: + **Use build-in function** +> You can use this starting from OpenSearch 2.16 ```json POST /_plugins/_ml/connectors/_create {