-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Classify themes, endpoint management
- Loading branch information
Showing
6 changed files
with
329 additions
and
5 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
html/modules/custom/reliefweb_openai/config/install/reliefweb_openai.settings.yml
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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
token: 'xyzzy' | ||
api_org: 'unocha' | ||
aws_access_key: '' | ||
aws_secret_key: '' | ||
aws_region: 'eu-central-1' | ||
aws_endpoint_theme_classifier: 'arn:aws:comprehend:eu-central-1:694216630861:document-classifier-endpoint/rw-themes' |
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
192 changes: 192 additions & 0 deletions
192
html/modules/custom/reliefweb_openai/src/Command/ReliefwebOpenAIAwsCommand.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,192 @@ | ||
<?php | ||
|
||
namespace Drupal\reliefweb_openai\Command; | ||
|
||
use Aws\Comprehend\ComprehendClient; | ||
use Consolidation\OutputFormatters\Options\FormatterOptions; | ||
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; | ||
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; | ||
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; | ||
use Consolidation\SiteProcess\ProcessManagerAwareTrait; | ||
use Drupal\Core\Database\Connection; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Logger\LoggerChannelFactoryInterface; | ||
use Drupal\Core\State\StateInterface; | ||
use Drush\Commands\DrushCommands; | ||
use GuzzleHttp\ClientInterface; | ||
|
||
/** | ||
* ReliefWeb Open AI Drush commandfile. | ||
*/ | ||
class ReliefwebOpenAIAwsCommand extends DrushCommands implements SiteAliasManagerAwareInterface { | ||
|
||
// Drush traits. | ||
use ProcessManagerAwareTrait; | ||
use SiteAliasManagerAwareTrait; | ||
|
||
/** | ||
* The database connection. | ||
* | ||
* @var \Drupal\Core\Database\Connection | ||
*/ | ||
protected $database; | ||
|
||
/** | ||
* The entity type manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected $entityTypeManager; | ||
|
||
/** | ||
* The account switcher. | ||
* | ||
* @var \Drupal\Core\Session\AccountSwitcherInterface | ||
*/ | ||
protected $accountSwitcher; | ||
|
||
/** | ||
* An http client. | ||
* | ||
* @var \GuzzleHttp\ClientInterface | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* The logger factory. | ||
* | ||
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface | ||
*/ | ||
protected $loggerFactory; | ||
|
||
/** | ||
* The state store. | ||
* | ||
* @var Drupal\Core\State\StateInterface | ||
*/ | ||
protected $state; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct( | ||
Connection $database, | ||
EntityTypeManagerInterface $entity_type_manager, | ||
$account_switcher, | ||
ClientInterface $http_client, | ||
LoggerChannelFactoryInterface $logger_factory, | ||
StateInterface $state, | ||
) { | ||
$this->database = $database; | ||
$this->entityTypeManager = $entity_type_manager; | ||
$this->accountSwitcher = $account_switcher; | ||
$this->httpClient = $http_client; | ||
$this->loggerFactory = $logger_factory; | ||
$this->state = $state; | ||
} | ||
|
||
/** | ||
* List endpoints. | ||
* | ||
* @command reliefweb_openai:aws_endpoints:list | ||
* @usage reliefweb_openai:aws_endpoints:list | ||
* List AWS endpoints. | ||
* @validate-module-enabled reliefweb_openai | ||
*/ | ||
public function listEndpoints(array $options = [ | ||
'format' => 'table', | ||
]) : RowsOfFields { | ||
$config = \Drupal::config('reliefweb_openai.settings'); | ||
|
||
$access_key = $config->get('aws_access_key'); | ||
$secret_key = $config->get('aws_secret_key'); | ||
$region = $config->get('aws_region'); | ||
|
||
$client = new ComprehendClient([ | ||
'region' => $region, | ||
'version' => 'latest', | ||
'credentials' => [ | ||
'key' => $access_key, | ||
'secret' => $secret_key, | ||
] | ||
]); | ||
|
||
$result = $client->listEndpoints(); | ||
$data = []; | ||
|
||
foreach ($result->get('EndpointPropertiesList') as $row) { | ||
$data[$row['EndpointArn']] = [ | ||
'EndpointArn' => $row['EndpointArn'], | ||
'Status' => $row['Status'], | ||
'DesiredInferenceUnits' => $row['DesiredInferenceUnits'], | ||
]; | ||
} | ||
|
||
return new RowsOfFields($data); | ||
} | ||
|
||
/** | ||
* Create endpoint. | ||
* | ||
* @command reliefweb_openai:aws_endpoints:create | ||
* @usage reliefweb_openai:aws_endpoints:create | ||
* Create AWS endpoints. | ||
* @validate-module-enabled reliefweb_openai | ||
*/ | ||
public function createEndpoint() : string { | ||
$config = \Drupal::config('reliefweb_openai.settings'); | ||
|
||
$access_key = $config->get('aws_access_key'); | ||
$secret_key = $config->get('aws_secret_key'); | ||
$region = $config->get('aws_region'); | ||
|
||
$client = new ComprehendClient([ | ||
'region' => $region, | ||
'version' => 'latest', | ||
'credentials' => [ | ||
'key' => $access_key, | ||
'secret' => $secret_key, | ||
] | ||
]); | ||
|
||
$result = $client->createEndpoint([ | ||
'EndpointName' => 'rw-themes', | ||
'ModelArn' => 'arn:aws:comprehend:eu-central-1:694216630861:document-classifier/RW-Job-Tagging/version/v0-0-3', | ||
'DesiredInferenceUnits' => 1, | ||
]); | ||
|
||
return 'Endpoint will be created, takes 5-10 minutes to complete'; | ||
} | ||
|
||
/** | ||
* Delete endpoint. | ||
* | ||
* @command reliefweb_openai:aws_endpoints:delete | ||
* @usage reliefweb_openai:aws_endpoints:delete | ||
* Delete AWS endpoints. | ||
* @validate-module-enabled reliefweb_openai | ||
*/ | ||
public function deleteEndpoints(string $arn) : string { | ||
$config = \Drupal::config('reliefweb_openai.settings'); | ||
|
||
$access_key = $config->get('aws_access_key'); | ||
$secret_key = $config->get('aws_secret_key'); | ||
$region = $config->get('aws_region'); | ||
|
||
$client = new ComprehendClient([ | ||
'region' => $region, | ||
'version' => 'latest', | ||
'credentials' => [ | ||
'key' => $access_key, | ||
'secret' => $secret_key, | ||
] | ||
]); | ||
|
||
$client->deleteEndpoint([ | ||
'EndpointArn' => $arn, | ||
]); | ||
|
||
return 'Endpoint will be deleted'; | ||
} | ||
|
||
} |
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