Creating connectors for third-party ML platforms

Introduced 2.9

Connectors facilitate access to models hosted on third-party machine learning (ML) platforms.

OpenSearch provides connectors for several platforms, for example:

  • Amazon SageMaker allows you to host and manage the lifecycle of text embedding models, powering semantic search queries in OpenSearch. When connected, Amazon SageMaker hosts your models and OpenSearch is used to query inferences. This benefits Amazon SageMaker users who value its functionality, such as model monitoring, serverless hosting, and workflow automation for continuous training and deployment.
  • OpenAI ChatGPT enables you to invoke an OpenAI chat model from inside an OpenSearch cluster.
  • Cohere allows you to use data from OpenSearch to power the Cohere large language models.
  • Amazon Bedrock supports models like Bedrock Titan Embeddings, which can drive semantic search and retrieval-augmented generation in OpenSearch.

Connector blueprints

A connector blueprint defines the set of parameters (the request body) you need to provide when sending an API request to create a specific connector. Connector blueprints may differ based on the platform and the model that you are accessing.

OpenSearch provides connector blueprints for several ML platforms and models. For a full list of connector blueprints provided by OpenSearch, see Supported connectors.

As an ML developer, you can also create connector blueprints for other platforms and models. Data scientists and administrators can then use the blueprint to create connectors. They are only required to enter their credential settings, such as openAI_key, for the service to which they are connecting. For information about creating connector blueprints, including descriptions of all parameters, see Connector blueprints.

Supported connectors

The following table lists all connector blueprints provided by OpenSearch. Follow the links to each connector blueprint for an example request that you can use to create the connector, including all parameters, and an example Predict API request.

PlatformModelConnector blueprint
Amazon BedrockA21 Labs Jurassic-2 MidBlueprint
Amazon BedrockAnthropic Claude v2Blueprint
Amazon BedrockTitan Text EmbeddingsBlueprint
Amazon SageMakerText embedding modelsBlueprint
CohereThe embed-english-v2.0 text embedding modelBlueprint
CohereThe embed-english-v3.0 text embedding modelBlueprint
OpenAIChat models (for example, gpt-3.5-turbo)Blueprint
OpenAICompletion models (for example, text-davinci-003)Blueprint
OpenAIText embedding models (for example, text-embedding-ada-002)Blueprint

Creating a connector

You can provision connectors in two ways:

  1. Create a standalone connector: A standalone connector can be reused and shared by multiple models but requires access to both the model and connector in OpenSearch and the third-party platform, such as OpenAI or Amazon SageMaker, that the connector is accessing. Standalone connectors are saved in a connector index.

  2. Create a connector for a specific externally hosted model: Alternatively, you can create a connector that can only be used with the model for which it was created. To access such a connector, you only need access to the model itself because the connection is established inside the model. These connectors are saved in the model index.

Creating a standalone connector

Standalone connectors can be used by multiple models. To create a standalone connector, send a request to the connectors/_create endpoint and provide all of the parameters described in Connector blueprints:

  1. POST /_plugins/_ml/connectors/_create
  2. {
  3. "name": "OpenAI Chat Connector",
  4. "description": "The connector to public OpenAI model service for GPT 3.5",
  5. "version": 1,
  6. "protocol": "http",
  7. "parameters": {
  8. "endpoint": "api.openai.com",
  9. "model": "gpt-3.5-turbo"
  10. },
  11. "credential": {
  12. "openAI_key": "..."
  13. },
  14. "actions": [
  15. {
  16. "action_type": "predict",
  17. "method": "POST",
  18. "url": "https://${parameters.endpoint}/v1/chat/completions",
  19. "headers": {
  20. "Authorization": "Bearer ${credential.openAI_key}"
  21. },
  22. "request_body": "{ \"model\": \"${parameters.model}\", \"messages\": ${parameters.messages} }"
  23. }
  24. ]
  25. }

copy

Creating a connector for a specific model

To create a connector for a specific model, provide all of the parameters described in Connector blueprints within the connector object of a request to the models/_register endpoint:

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "openAI-GPT-3.5 model with a connector",
  4. "function_name": "remote",
  5. "model_group_id": "lEFGL4kB4ubqQRzegPo2",
  6. "description": "test model",
  7. "connector": {
  8. "name": "OpenAI Connector",
  9. "description": "The connector to public OpenAI model service for GPT 3.5",
  10. "version": 1,
  11. "protocol": "http",
  12. "parameters": {
  13. "endpoint": "api.openai.com",
  14. "max_tokens": 7,
  15. "temperature": 0,
  16. "model": "text-davinci-003"
  17. },
  18. "credential": {
  19. "openAI_key": "..."
  20. },
  21. "actions": [
  22. {
  23. "action_type": "predict",
  24. "method": "POST",
  25. "url": "https://${parameters.endpoint}/v1/completions",
  26. "headers": {
  27. "Authorization": "Bearer ${credential.openAI_key}"
  28. },
  29. "request_body": "{ \"model\": \"${parameters.model}\", \"prompt\": \"${parameters.prompt}\", \"max_tokens\": ${parameters.max_tokens}, \"temperature\": ${parameters.temperature} }"
  30. }
  31. ]
  32. }
  33. }

copy

Connector examples

The following sections contain examples of connectors for popular ML platforms. For a full list of supported connectors, see Supported connectors.

OpenAI chat connector

You can use the following example request to create a standalone OpenAI chat connector:

  1. POST /_plugins/_ml/connectors/_create
  2. {
  3. "name": "OpenAI Chat Connector",
  4. "description": "The connector to public OpenAI model service for GPT 3.5",
  5. "version": 1,
  6. "protocol": "http",
  7. "parameters": {
  8. "endpoint": "api.openai.com",
  9. "model": "gpt-3.5-turbo"
  10. },
  11. "credential": {
  12. "openAI_key": "..."
  13. },
  14. "actions": [
  15. {
  16. "action_type": "predict",
  17. "method": "POST",
  18. "url": "https://${parameters.endpoint}/v1/chat/completions",
  19. "headers": {
  20. "Authorization": "Bearer ${credential.openAI_key}"
  21. },
  22. "request_body": "{ \"model\": \"${parameters.model}\", \"messages\": ${parameters.messages} }"
  23. }
  24. ]
  25. }

copy

Amazon SageMaker connector

You can use the following example request to create a standalone Amazon SageMaker connector:

  1. POST /_plugins/_ml/connectors/_create
  2. {
  3. "name": "sagemaker: embedding",
  4. "description": "Test connector for Sagemaker embedding model",
  5. "version": 1,
  6. "protocol": "aws_sigv4",
  7. "credential": {
  8. "access_key": "...",
  9. "secret_key": "...",
  10. "session_token": "..."
  11. },
  12. "parameters": {
  13. "region": "us-west-2",
  14. "service_name": "sagemaker"
  15. },
  16. "actions": [
  17. {
  18. "action_type": "predict",
  19. "method": "POST",
  20. "headers": {
  21. "content-type": "application/json"
  22. },
  23. "url": "https://runtime.sagemaker.${parameters.region}.amazonaws.com/endpoints/lmi-model-2023-06-24-01-35-32-275/invocations",
  24. "request_body": "[\"${parameters.inputs}\"]"
  25. }
  26. ]
  27. }

copy

The credential parameter contains the following options reserved for aws_sigv4 authentication:

  • access_key: Required. Provides the access key for the AWS instance.
  • secret_key: Required. Provides the secret key for the AWS instance.
  • session_token: Optional. Provides a temporary set of credentials for the AWS instance.

The parameters section requires the following options when using aws_sigv4 authentication:

  • region: The AWS Region in which the AWS instance is located.
  • service_name: The name of the AWS service for the connector.

Cohere connector

You can use the following example request to create a standalone Cohere connector:

  1. POST /_plugins/_ml/connectors/_create
  2. {
  3. "name": "<YOUR CONNECTOR NAME>",
  4. "description": "<YOUR CONNECTOR DESCRIPTION>",
  5. "version": "<YOUR CONNECTOR VERSION>",
  6. "protocol": "http",
  7. "credential": {
  8. "cohere_key": "<YOUR Cohere API KEY HERE>"
  9. },
  10. "parameters": {
  11. "model": "embed-english-v2.0",
  12. "truncate": "END"
  13. },
  14. "actions": [
  15. {
  16. "action_type": "predict",
  17. "method": "POST",
  18. "url": "https://api.cohere.ai/v1/embed",
  19. "headers": {
  20. "Authorization": "Bearer ${credential.cohere_key}"
  21. },
  22. "request_body": "{ \"texts\": ${parameters.texts}, \"truncate\": \"${parameters.truncate}\", \"model\": \"${parameters.model}\" }",
  23. "pre_process_function": "connector.pre_process.cohere.embedding",
  24. "post_process_function": "connector.post_process.cohere.embedding"
  25. }
  26. ]
  27. }

copy

Amazon Bedrock connector

You can use the following example request to create a standalone Amazon Bedrock connector:

  1. POST /_plugins/_ml/connectors/_create
  2. {
  3. "name": "Amazon Bedrock Connector: embedding",
  4. "description": "The connector to the Bedrock Titan embedding model",
  5. "version": 1,
  6. "protocol": "aws_sigv4",
  7. "parameters": {
  8. "region": "<YOUR AWS REGION>",
  9. "service_name": "bedrock"
  10. },
  11. "credential": {
  12. "access_key": "<YOUR AWS ACCESS KEY>",
  13. "secret_key": "<YOUR AWS SECRET KEY>",
  14. "session_token": "<YOUR AWS SECURITY TOKEN>"
  15. },
  16. "actions": [
  17. {
  18. "action_type": "predict",
  19. "method": "POST",
  20. "url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-embed-text-v1/invoke",
  21. "headers": {
  22. "content-type": "application/json",
  23. "x-amz-content-sha256": "required"
  24. },
  25. "request_body": "{ \"inputText\": \"${parameters.inputText}\" }",
  26. "pre_process_function": "\n StringBuilder builder = new StringBuilder();\n builder.append(\"\\\"\");\n String first = params.text_docs[0];\n builder.append(first);\n builder.append(\"\\\"\");\n def parameters = \"{\" +\"\\\"inputText\\\":\" + builder + \"}\";\n return \"{\" +\"\\\"parameters\\\":\" + parameters + \"}\";",
  27. "post_process_function": "\n def name = \"sentence_embedding\";\n def dataType = \"FLOAT32\";\n if (params.embedding == null || params.embedding.length == 0) {\n return params.message;\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 "
  28. }
  29. ]
  30. }

copy

Next steps