Skip to content

Commit

Permalink
Merge pull request #115 from UN-OCHA/OPS-10643
Browse files Browse the repository at this point in the history
Ops 10643
  • Loading branch information
attiks authored Jul 23, 2024
2 parents c8f7d07 + af36bea commit 725a5f2
Show file tree
Hide file tree
Showing 22 changed files with 156 additions and 119 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
5 changes: 4 additions & 1 deletion config/field.storage.node.field_ai_brain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ settings:
label: 'Open AI (chat-gpt-3.5)'
-
value: azure_trained
label: 'Trained Azure AI'
label: 'Azure OpenAI 3.5'
-
value: bedrock
label: 'BedRock (Titan)'
-
value: claude
label: Claude
-
value: amazon_titan_premier
label: 'Amazon Titan Text Premier'
allowed_values_function: ''
module: options
locked: false
Expand Down
4 changes: 1 addition & 3 deletions config/monitoring.sensor_config.core_cron_last_run_age.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
uuid: 2eb4fe7f-035b-42b4-9ddf-86030f5c107f
uuid: null
langcode: en
status: true
dependencies: { }
_core:
default_config_hash: 9xaHOMw0Ra9sOuNKGoBqCLFoX2pj_66FbTQPK1CFK1U
id: core_cron_last_run_age
label: 'Last cron run age'
description: 'Time since last cron run'
Expand Down
89 changes: 2 additions & 87 deletions config/monitoring.sensor_config.monitoring_installed_modules.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
uuid: aeaaa913-b6f9-4412-b1da-586b728af2fc
uuid: null
langcode: en
status: true
dependencies: { }
_core:
default_config_hash: HaL0P0uZ-Ej7bGqTmmN6ZhrX1cK-IJEWfQv8xDom61A
id: monitoring_installed_modules
label: 'Installed modules'
description: 'Installed only expected modules'
Expand All @@ -15,89 +13,6 @@ value_type: number
caching_time: 3600
settings:
allow_additional: false
modules:
admin_denied: admin_denied
allowed_formats: allowed_formats
block: block
breakpoint: breakpoint
ckeditor5: ckeditor5
comment: comment
components: components
config: config
config_filter: config_filter
config_split: config_split
config_translation: config_translation
content_moderation: content_moderation
csp: csp
ctools: ctools
datetime: datetime
datetime_range: datetime_range
dblog: dblog
devel: devel
dynamic_page_cache: dynamic_page_cache
editor: editor
entity_reference_revisions: entity_reference_revisions
environment_indicator: environment_indicator
environment_indicator_ui: environment_indicator_ui
field: field
field_ui: field_ui
file: file
file_mdm: file_mdm
filter: filter
image: image
imageapi_optimize: imageapi_optimize
imageapi_optimize_binaries: imageapi_optimize_binaries
imagemagick: imagemagick
inline_form_errors: inline_form_errors
language: language
layout_discovery: layout_discovery
layout_paragraphs: layout_paragraphs
link: link
locale: locale
maintenance200: maintenance200
media: media
media_library: media_library
memcache: memcache
memcache_admin: memcache_admin
menu_link_content: menu_link_content
menu_ui: menu_ui
monitoring: monitoring
mysql: mysql
node: node
node_view_permissions: node_view_permissions
ocha_ai_summarize: ocha_ai_summarize
ocha_monitoring: ocha_monitoring
options: options
page_cache: page_cache
path: path
path_alias: path_alias
private_content: private_content
queue_ui: queue_ui
redirect: redirect
responsive_image: responsive_image
social_api: social_api
social_auth: social_auth
social_auth_hid: social_auth_hid
sophron: sophron
syslog: syslog
system: system
taxonomy: taxonomy
text: text
token: token
toolbar: toolbar
update: update
user: user
user_display_name: user_display_name
user_expire: user_expire
username_enumeration_prevention: username_enumeration_prevention
views_ui: views_ui
workflows: workflows
xmlsitemap_engines: xmlsitemap_engines
pathauto: pathauto
xmlsitemap: xmlsitemap
content_translation: content_translation
views: views
paragraphs: paragraphs
minimal: minimal
modules: { }
thresholds:
type: none
18 changes: 18 additions & 0 deletions config/monitoring.sensor_config.ocha_admin_paths.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
uuid: null
langcode: en
status: true
dependencies:
module:
- ocha_monitoring
id: ocha_admin_paths
label: 'UN admin paths'
description: 'UN admin paths'
category: OCHA
plugin_id: ocha_admin_paths
result_class: null
value_label: Version
value_type: string
caching_time: 86400
settings: { }
thresholds:
type: none
2 changes: 1 addition & 1 deletion config/monitoring.sensor_config.ocha_common_design.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: af6d384e-d4e8-4b80-a516-cdb4a3fa95eb
uuid: null
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: 070d4341-af5c-4263-8624-b1b735443810
uuid: null
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: 9757d8fb-4b8b-4e4e-8870-76f918c5155f
uuid: null
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: 4bedc27b-9886-4fd8-aa7a-5ae0d337c59d
uuid: null
langcode: en
status: true
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion config/monitoring.sensor_config.ocha_current_release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: 3e0c8eab-7b26-46cf-ae6a-0142fd29fd8e
uuid: null
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: fefbe8a3-ad5d-4b6b-8ccf-18a3c32be418
uuid: null
langcode: en
status: true
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion config/monitoring.sensor_config.ocha_env_link_fixer.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: b7c1d38c-bf25-45c4-a5d7-664e89a2385b
uuid: null
langcode: en
status: true
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion config/monitoring.sensor_config.ocha_google_tag.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
uuid: 4fb9d053-5d3b-4b4d-a27e-2639ddd828ba
langcode: en
status: true
status: false
dependencies:
module:
- ocha_monitoring
Expand Down
2 changes: 1 addition & 1 deletion config/monitoring.sensor_config.ocha_gtm_barebones.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: 6ce278b7-c2da-4db7-9c7a-5e65a6ad8229
uuid: null
langcode: en
status: true
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion config/monitoring.sensor_config.ocha_ocha_monitoring.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: 90959c64-378f-4921-b4e5-504fbbc5cb90
uuid: null
langcode: en
status: true
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion config/monitoring.sensor_config.ocha_un_date.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: ba8b97cd-eb0a-4973-ac28-3337d00ea006
uuid: null
langcode: en
status: true
dependencies:
Expand Down
4 changes: 1 addition & 3 deletions config/monitoring.sensor_config.update_contrib.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
uuid: e58eb6c7-f66f-406e-b092-a191c7fc48f7
uuid: null
langcode: en
status: true
dependencies:
module:
- update
_core:
default_config_hash: '-re8-AFGO4z0a0i4NeTZV5HlEipd-KC9kkLxzgz1mgs'
id: update_contrib
label: 'Modules and themes'
description: 'Available updates for contrib modules and themes'
Expand Down
4 changes: 1 addition & 3 deletions config/monitoring.sensor_config.update_core.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
uuid: aa1a958a-1cc0-4f29-a0a8-3b79d682464e
uuid: null
langcode: en
status: true
dependencies:
module:
- update
_core:
default_config_hash: FSvo5_4kGyvNZu9RVgfFKnGfbGkIS8EgI46ckVkT6CM
id: update_core
label: Core
description: 'Available updates for Drupal core'
Expand Down
89 changes: 80 additions & 9 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 @@ -1544,13 +1591,37 @@ function ocha_ai_summarize_notify_user(NodeInterface $node) {

$ai_models = $node->get('field_ai_brain')->getSettings()['allowed_values'];
$ai_model = $ai_models[$node->get('field_ai_brain')->value];
$sub_title = $node->type->entity->label();

$field_name = '';
$ai_text = '';

switch ($node->bundle()) {
case 'summary':
$field_name = 'field_summary';
$ai_text = $node->get($field_name)->value;
break;

$params['subject'] = t('AI Summarize: your document has been processed.');
$params['message'] = implode("\r\n\r\n", [
case 'action_points':
$field_name = 'field_action_points';
$ai_text = $node->get($field_name)->value;
break;

}

$params['subject'] = t('AI Summarize: @title - has been processed.', [
'@title' => $node->getTitle(),
]);
$params['message'] = implode("\r\n", [
t('Your document has been processed by @model.', ['@model' => $ai_model]),
t('You can see the result on @link', [
'',
t('You can always see your results and provide feedback online here: @link', [
'@link' => $node->toUrl()->setAbsolute()->toString(),
]),
'',
$sub_title,
'',
$ai_text,
]);

$mail_manager->mail($module, $key, $to, $langcode, $params);
Expand All @@ -1573,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
Loading

0 comments on commit 725a5f2

Please sign in to comment.