Skip to content

Commit

Permalink
feat: Add Amazon Titan Premier
Browse files Browse the repository at this point in the history
Refs# OPS-10644
  • Loading branch information
attiks committed Jul 23, 2024
1 parent 748b3d9 commit af36bea
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/field.field.node.action_points.field_ai_brain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ required: false
translatable: false
default_value:
-
value: azure_trained
value: amazon_titan_premier
default_value_callback: ''
settings: { }
field_type: list_string
2 changes: 1 addition & 1 deletion config/field.field.node.summary.field_ai_brain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ required: false
translatable: false
default_value:
-
value: azure_trained
value: amazon_titan_premier
default_value_callback: ''
settings: { }
field_type: list_string
3 changes: 3 additions & 0 deletions config/field.storage.node.field_ai_brain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ settings:
-
value: claude
label: Claude
-
value: amazon_titan_premier
label: 'Amazon Titan Text Premier'
allowed_values_function: ''
module: options
locked: false
Expand Down
59 changes: 53 additions & 6 deletions html/modules/custom/ocha_ai_summarize/ocha_ai_summarize.module
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,48 @@ function ocha_ai_summarize_http_call_bedrock($prompt) {
}
}

/**
* Make chat call to Titan Premier.
*/
function ocha_ai_summarize_http_call_titan_premier($prompt) {
$model = 'amazon.titan-text-premier-v1:0';
$client_options = ocha_ai_summarize_get_aws_client_options();

$bedRock = new BedrockRuntimeClient($client_options);

$payload = [
'accept' => 'application/json',
'body' => json_encode([
'inputText' => $prompt,
'textGenerationConfig' => [
'maxTokenCount' => 3000,
'stopSequences' => [],
'temperature' => 0.0,
'topP' => 0.9,
],
]),
'contentType' => 'application/json',
'modelId' => $model,
];

try {
/** @var \Aws\Result $response */
$response = $bedRock->invokeModel($payload);
}
catch (\Exception $exception) {
\Drupal::logger('AI - Titan premier')->error($exception->getMessage());
return '';
}

try {
return json_decode($response->get('body')->getContents(), TRUE);
}
catch (\Exception $exception) {
\Drupal::logger('AI - Titan premier')->error($exception->getMessage());
return '';
}
}

/**
* Make chat call to Claude.
*/
Expand Down Expand Up @@ -1344,6 +1386,11 @@ function ocha_ai_summarize_check_length($text, $bot) {
case 'bedrock':
$max_tokens = 2 * 42000;
break;

case 'amazon_titan_premier':
$max_tokens = 2 * 3072;
break;

}

$max_tokens = round($max_tokens, 0);
Expand Down Expand Up @@ -1597,22 +1644,22 @@ function ocha_ai_summarize_log_time(string $action, int $nid, int $ms) : void {
/**
* Track execution times of text extraction.
*/
function ocha_ai_summarize_log_time_extract(int $nid, int $ms) : void {
ocha_ai_summarize_log_time('extract_text', $nid, $ms);
function ocha_ai_summarize_log_time_extract(int $nid, float $ms) : void {
ocha_ai_summarize_log_time('extract_text', $nid, round($ms));
}

/**
* Track execution times of summarize.
*/
function ocha_ai_summarize_log_time_summarize(int $nid, int $ms) : void {
ocha_ai_summarize_log_time('summarize', $nid, $ms);
function ocha_ai_summarize_log_time_summarize(int $nid, float $ms) : void {
ocha_ai_summarize_log_time('summarize', $nid, round($ms));
}

/**
* Track execution times of action points.
*/
function ocha_ai_summarize_log_time_action_points(int $nid, int $ms) : void {
ocha_ai_summarize_log_time('action_point', $nid, $ms);
function ocha_ai_summarize_log_time_action_points(int $nid, float $ms) : void {
ocha_ai_summarize_log_time('action_point', $nid, round($ms));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public function processItem($data) {
$results[] = $this->sendToBedRock($prompt . $text);
break;

case 'amazon_titan_premier':
$results[] = $this->sendToTitanPremier($prompt . $text);
break;

}
}
ocha_ai_summarize_log_time_summarize($nid, Timer::read('summarize'));
Expand Down Expand Up @@ -191,6 +195,11 @@ public function processItem($data) {
case 'bedrock':
$action_points = $this->sendToBedRock("$prompt:\n\n" . $text);
break;

case 'amazon_titan_premier':
$action_points = $this->sendToTitanPremier($prompt . $text);
break;

}
ocha_ai_summarize_log_time_action_points($nid, Timer::read('action_points'));
Timer::stop('action_points');
Expand Down Expand Up @@ -255,6 +264,15 @@ protected function sendToBedRock($text) : string {
return $result['results'][0]['outputText'] ?? '';
}

/**
* Send query to Titan Premier.
*/
protected function sendToTitanPremier($text) : string {
$result = ocha_ai_summarize_http_call_titan_premier($text);
print_r($result);
return $result['results'][0]['outputText'] ?? '';
}

/**
* Send query to Claude AI.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public function processItem($data) {
$results[] = $this->sendToBedRock($prompt . $text);
break;

case 'amazon_titan_premier':
$results[] = $this->sendToTitanPremier($prompt . $text);
break;

}
}

Expand All @@ -190,6 +194,11 @@ public function processItem($data) {
case 'bedrock':
$summary = $this->sendToBedRock($prompt . $text);
break;

case 'amazon_titan_premier':
$summary = $this->sendToTitanPremier($prompt . $text);
break;

}
ocha_ai_summarize_log_time_summarize($nid, Timer::read('summarize'));
Timer::stop('summarize');
Expand Down Expand Up @@ -251,6 +260,15 @@ protected function sendToBedRock($text) : string {
return $result['results'][0]['outputText'] ?? '';
}

/**
* Send query to Titan Premier.
*/
protected function sendToTitanPremier($text) : string {
$result = ocha_ai_summarize_http_call_titan_premier($text);
print_r($result);
return $result['results'][0]['outputText'] ?? '';
}

/**
* Send query to Claude AI.
*/
Expand Down

0 comments on commit af36bea

Please sign in to comment.