-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement model, model group and connector apis (#170)
* Add createConnector to the machine learning namespace Signed-off-by: Andrew Chappell <[email protected]> * Implement connectors API Signed-off-by: Andrew Chappell <[email protected]> * Added model group apis Signed-off-by: Andrew Chappell <[email protected]> * Implement Models API Signed-off-by: Andrew Chappell <[email protected]> * Update license header for new files only Signed-off-by: Andrew Chappell <[email protected]> * Update changelog Signed-off-by: Andrew Chappell <[email protected]> * Added ability to search all models Signed-off-by: Andrew Chappell <[email protected]> * Add Tasks namespace Signed-off-by: Andrew Chappell <[email protected]> * Added tests for machine learning namespace and ran cs fixer Signed-off-by: Andrew Chappell <[email protected]> * Added sample usage to documentation Signed-off-by: Andrew Chappell <[email protected]> * Added documentation to namespace header Signed-off-by: Andrew Chappell <[email protected]> * Updates based on feedback Signed-off-by: Andrew Chappell <[email protected]> --------- Signed-off-by: Andrew Chappell <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,655 additions
and
15 deletions.
There are no files selected for viewing
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
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
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,71 @@ | ||
# Machine Learning Example Usage | ||
|
||
Walks through the process of setting up a model to generate | ||
vector embeddings from OpenAI on AWS managed Opensearch. | ||
|
||
### Prerequisites | ||
|
||
* This example assumes you are using the AWS managed OpenSearch | ||
service. See [The ml commons documentation](https://github.com/opensearch-project/ml-commons/blob/main/docs/remote_inference_blueprints/openai_connector_embedding_blueprint.md) for more examples and further information. | ||
* You will need an API key from OpenAI. [Sign up](https://platform.openai.com/signup) | ||
* The API key must be stored in [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/) | ||
|
||
```php | ||
<?php | ||
|
||
# Register a model group. | ||
$modelGroupResponse = $client->ml()->registerModelGroup([ | ||
'body' => [ | ||
'name' => 'openai_model_group', | ||
'description' => 'Group containing models for OpenAI', | ||
], | ||
]); | ||
|
||
# Create the connector. | ||
$connectorResponse = $client->ml()->createConnector([ | ||
'body' => [ | ||
'name' => "Open AI Embedding Connector", | ||
'description' => "Creates a connector to Open AI's embedding endpoint", | ||
'version' => 1, | ||
'protocol' => 'http', | ||
'parameters' => ['model' => 'text-embedding-ada-002'], | ||
'credential' => [ | ||
"secretArn" => '<Your Secret ARN from AWS Secrets Manager>', | ||
"roleArn" => '<Your IAM role ARN>', | ||
] | ||
'actions' => [ | ||
[ | ||
'action_type' => 'predict', | ||
'method' => 'POST', | ||
'url' => 'https://api.openai.com/v1/embeddings', | ||
'headers' => [ | ||
'Authorization': 'Bearer ${credential.secretArn.<Your Open AI Secret in Secrets Manager>}' | ||
], | ||
'request_body' => "{ \"input\": \${parameters.input}, \"model\": \"\${parameters.model}\" }", | ||
'pre_process_function' => "connector.pre_process.openai.embedding", | ||
'post_process_function' => "connector.post_process.openai.embedding", | ||
], | ||
], | ||
], | ||
]); | ||
|
||
# Register the model. | ||
$registerModelResponse = $client->ml()->registerModel([ | ||
'body' => [ | ||
'name' => 'OpenAI embedding model', | ||
'function_name' => 'remote', | ||
'model_group_id' => $modelGroupResponse['model_group_id'], | ||
'description' => 'Model for retrieving vector embeddings from OpenAI', | ||
'connector_id' => $connectorResponse['connector_id'], | ||
], | ||
]); | ||
|
||
# Monitor the state of the register model task. | ||
$taskResponse = $client->ml()->getTask(['id' => $registerModelResponse['task_id']]); | ||
|
||
assert($taskResponse['state'] === 'COMPLETED'); | ||
|
||
# Finally deploy the model. You will now be able to generate vector | ||
# embeddings from OpenSearch (via OpenAI). | ||
$client->ml()->deployModel(['id' => $taskResponse['model_id']]); | ||
``` |
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
43 changes: 43 additions & 0 deletions
43
src/OpenSearch/Endpoints/MachineLearning/Connectors/CreateConnector.php
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
namespace OpenSearch\Endpoints\MachineLearning\Connectors; | ||
|
||
use OpenSearch\Endpoints\AbstractEndpoint; | ||
|
||
class CreateConnector extends AbstractEndpoint | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
public function getParamWhitelist(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getURI(): string | ||
{ | ||
return "/_plugins/_ml/connectors/_create"; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMethod(): string | ||
{ | ||
return 'POST'; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/OpenSearch/Endpoints/MachineLearning/Connectors/DeleteConnector.php
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
namespace OpenSearch\Endpoints\MachineLearning\Connectors; | ||
|
||
use OpenSearch\Common\Exceptions\RuntimeException; | ||
use OpenSearch\Endpoints\AbstractEndpoint; | ||
|
||
class DeleteConnector extends AbstractEndpoint | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
public function getParamWhitelist(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getURI(): string | ||
{ | ||
if ($this->id) { | ||
return "/_plugins/_ml/connectors/$this->id"; | ||
} | ||
|
||
throw new RuntimeException( | ||
'id is required for delete' | ||
); | ||
|
||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMethod(): string | ||
{ | ||
return 'DELETE'; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/OpenSearch/Endpoints/MachineLearning/Connectors/GetConnector.php
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
namespace OpenSearch\Endpoints\MachineLearning\Connectors; | ||
|
||
use OpenSearch\Common\Exceptions\RuntimeException; | ||
use OpenSearch\Endpoints\AbstractEndpoint; | ||
|
||
class GetConnector extends AbstractEndpoint | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
public function getParamWhitelist(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getURI(): string | ||
{ | ||
if ($this->id) { | ||
return "/_plugins/_ml/connectors/$this->id"; | ||
} | ||
|
||
throw new RuntimeException( | ||
'id is required for get' | ||
); | ||
|
||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMethod(): string | ||
{ | ||
return 'GET'; | ||
} | ||
} |
Oops, something went wrong.