-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add connector tool #7384
Merged
Merged
Add connector tool #7384
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6e5120d
add connector tool
ylwu-amzn 28e7127
Merge branch 'main' into agent_tools
kolchfa-aws be603ad
address comments
ylwu-amzn 1bc04d1
Doc review
kolchfa-aws 57df6da
Update _ml-commons-plugin/agents-tools/tools/connector-tool.md
kolchfa-aws f3f35ac
Update _ml-commons-plugin/agents-tools/tools/connector-tool.md
kolchfa-aws 7b0d718
Update _ml-commons-plugin/agents-tools/tools/connector-tool.md
kolchfa-aws d829054
Update _ml-commons-plugin/agents-tools/tools/connector-tool.md
kolchfa-aws 313552d
Update _ml-commons-plugin/agents-tools/tools/connector-tool.md
kolchfa-aws b2b79dc
Update _ml-commons-plugin/remote-models/blueprints.md
kolchfa-aws e82d658
Apply suggestions from code review
kolchfa-aws 5001ec2
Merge branch 'main' into agent_tools
kolchfa-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
_ml-commons-plugin/agents-tools/tools/connector-tool.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
--- | ||
layout: default | ||
title: Connector tool | ||
has_children: false | ||
has_toc: false | ||
nav_order: 20 | ||
parent: Tools | ||
grand_parent: Agents and tools | ||
--- | ||
|
||
<!-- vale off --> | ||
# Connector tool | ||
**Introduced 2.15** | ||
{: .label .label-purple } | ||
<!-- vale on --> | ||
|
||
The `ConnectorTool` uses a [connector]({{site.url}}{{site.baseurl}}/ml-commons-plugin/remote-models/connectors/) to call any REST API function. For example, you can use a `ConnectorTool` to call a Lambda function through its REST API interface. | ||
|
||
## Step 1: Register a connector with an execute action | ||
|
||
The `ConnectorTool` can only run an `execute` action within a connector. Before you can create a `ConnectorTool`, you need to configure a connector and provide an `execute` action in the `actions` array. The `execute` action is used to invoke a function at a REST API endpoint. It is similar to the `predict` action, which is used to invoke a machine learning (ML) model. | ||
|
||
For this example, you'll create a connector for a simple AWS Lambda function that accepts two integers and returns their sum. This function is hosted on a dedicated endpoint with a specific URL, which you'll provide in the `url` parameter. For more information, see [Lambda function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html). | ||
|
||
To create a connector, send the following request: | ||
|
||
```json | ||
POST _plugins/_ml/connectors/_create | ||
{ | ||
"name": "Lambda connector of simple calculator", | ||
"description": "Demo connector of lambda function", | ||
"version": 1, | ||
"protocol": "aws_sigv4", | ||
"parameters": { | ||
"region": "YOUR AWS REGION", | ||
"service_name": "lambda" | ||
}, | ||
"credential": { | ||
"access_key": "YOUR ACCESS KEY", | ||
"secret_key": "YOUR SECRET KEY", | ||
"session_token": "YOUR SESSION TOKEN" | ||
}, | ||
"actions": [ | ||
{ | ||
"action_type": "execute", | ||
"method": "POST", | ||
"url": "YOUR LAMBDA FUNCTION URL", | ||
"headers": { | ||
"content-type": "application/json" | ||
}, | ||
"request_body": "{ \"number1\":\"${parameters.number1}\", \"number2\":\"${parameters.number2}\" }" | ||
} | ||
] | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
OpenSearch responds with a connector ID: | ||
|
||
```json | ||
{ | ||
"connector_id": "Zz1XEJABXWrLmr4mewEF" | ||
} | ||
``` | ||
|
||
## Step 2: Register a flow agent that will run the ConnectorTool | ||
|
||
For this example, the Lambda function adds the two input numbers and returns their sum in the `result` field: | ||
|
||
```json | ||
{ | ||
"result": 5 | ||
} | ||
``` | ||
|
||
By default, the `ConnectorTool` expects the response from the Lambda function to contain a field named `response`. However, in this example the Lambda function response doesn't include a `response` field. To retrieve the result from the `result` field instead, you need to provide a `response_filter`, specifying the [JSON path](https://github.com/json-path/JsonPath) to the `result` field (`$.result`). Using the `response_filter`, the `ConnectorTool` will retrieve the result with the specified JSON path and return it in the `response` field. | ||
|
||
To configure the Lambda function workflow, create a flow agent. A flow agent runs a sequence of tools in order and returns the last tool's output. To create a flow agent, send the following register agent request, providing the connector ID from the previous step and a `response_filter`: | ||
|
||
```json | ||
POST /_plugins/_ml/agents/_register | ||
{ | ||
"name": "Demo agent of Lambda connector", | ||
"type": "flow", | ||
"description": "This is a demo agent", | ||
"app_type": "demo", | ||
"tools": [ | ||
{ | ||
"type": "ConnectorTool", | ||
"name": "lambda_function", | ||
"parameters": { | ||
"connector_id": "YOUR CONNECTOR ID", | ||
"response_filter": "$.result" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
For parameter descriptions, see [Register parameters](#register-parameters). | ||
|
||
OpenSearch responds with an agent ID: | ||
|
||
```json | ||
{ | ||
"agent_id": "az1XEJABXWrLmr4miAFj" | ||
} | ||
``` | ||
|
||
## Step 3: Run the agent | ||
|
||
Then, run the agent by sending the following request: | ||
|
||
```json | ||
POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute | ||
{ | ||
"parameters": { | ||
"number1": 2, | ||
"number2": 3 | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
OpenSearch returns the output of the Lambda function execution. In the output, the field name is `response`, and the `result` field contains the Lambda function result: | ||
|
||
```json | ||
{ | ||
"inference_results": [ | ||
{ | ||
"output": [ | ||
{ | ||
"name": "response", | ||
"result": 5 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Register parameters | ||
|
||
The following table lists all tool parameters that are available when registering an agent. | ||
|
||
Parameter | Type | Required/Optional | Description | ||
:--- | :--- | :--- | :--- | ||
`connector_id` | String | Required | A connector ID of a connector configured with an `execute` action that invokes an API. | ||
`response_filter` | String | Optional | A [JSON path](https://github.com/json-path/JsonPath) to the response field that contains the result of invoking the API. If a `response_filter` is not specified, then the `ConnectorTool` expects the API response to be in a field named `response`. | ||
|
||
## Execute parameters | ||
|
||
When running the agent, you can define any parameter needed for the API call in the `request_body` of your connector's `execute` action. In this example, the parameters are `number1` and `number2`: | ||
|
||
```json | ||
"actions": [ | ||
{ | ||
"action_type": "execute", | ||
"method": "POST", | ||
"url": "YOUR LAMBDA FUNCTION URL", | ||
"headers": { | ||
"content-type": "application/json" | ||
}, | ||
"request_body": "{ \"number1\":\"${parameters.number1}\", \"number2\":\"${parameters.number2}\" }" | ||
} | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this refers to an AWS Lambda, function, we should use the full service name here and just "Lambda" thereafter.