Skip to content
Snippets Groups Projects
Commit b8278082 authored by Ben Bastow's avatar Ben Bastow Committed by Scott Euser
Browse files

Issue #3402319 by scott_euser, ben.bastow: Grant control of auto-apply...

Issue #3402319 by scott_euser, ben.bastow: Grant control of auto-apply suggestions configuration to auto-apply high relevance only or both
parent 7a306b28
No related branches found
No related tags found
1 merge request!3Grant control of auto-apply suggestions configuration to auto-apply high relevance only or both
Pipeline #82395 passed with warnings
<?php
/**
* @file
* Update scripts for the AI Auto Reference module.
*/
/**
* Add auto_apply_relevance_levels to the default configuration for ai_auto_reference.settings.
*/
function ai_auto_reference_update_10001() {
$config = \Drupal::configFactory()->getEditable('ai_auto_reference.settings');
$config->set('auto_apply_relevance_levels', ['high', 'medium'])->save();
}
......@@ -25,14 +25,18 @@ function ai_auto_reference_form_node_form_alter(&$form, FormStateInterface $form
\Drupal::messenger()->addMessage($apply_form);
}
$form['actions']['ai_auto_reference'] = [
'#type' => 'submit',
'#value' => t('Generate references with AI'),
'#name' => 'ai_auto_reference',
'#submit' => [
'ai_auto_reference_node_form_submit',
],
];
$build_info = $form_state->getBuildInfo();
$node = $build_info['callback_object']->getEntity();
if ($node instanceof NodeInterface && $node->id()) {
$form['actions']['ai_auto_reference'] = [
'#type' => 'submit',
'#value' => t('Generate references with AI'),
'#name' => 'ai_auto_reference',
'#submit' => [
'ai_auto_reference_node_form_submit',
],
];
}
}
}
}
......
......@@ -3,3 +3,4 @@ service_model: "gpt-3.5-turbo"
service_api_key: ""
token_limit: "4000"
auto_apply_suggestions: false
auto_apply_relevance_levels: ["high","medium"]
......@@ -17,3 +17,6 @@ ai_auto_reference.settings:
auto_apply_suggestions:
label: 'Automatically save the suggestions without editor review'
type: boolean
auto_apply_relevance_levels:
label: 'Automatically save the suggestions at the specified relevance without editor review'
type: array
\ No newline at end of file
......@@ -102,12 +102,29 @@ class AiReferenceBatch {
*/
public static function batchFinished($success, array $results, array $operations) {
$auto_apply = \Drupal::config('ai_auto_reference.settings')->get('auto_apply_suggestions');
$auto_apply_relevance_levels = \Drupal::config('ai_auto_reference.settings')->get('auto_apply_relevance_levels');
$level_mapping = [
'h' => 'high',
'm' => 'medium',
];
// Updating node with suggested items.
if ($auto_apply) {
if ($node = Node::load($results['nid'])) {
foreach ($results['query'] as $field_name => $relevancy_values) {
foreach ($relevancy_values as $relevancy_value_ids) {
foreach ($relevancy_values as $level => $relevancy_value_ids) {
// Check if the auto-apply settings should apply for the
// given relevance level.
if (
!isset($level_mapping[$level])
|| !in_array($level_mapping[$level], $auto_apply_relevance_levels)
) {
continue;
}
// If auto-applying, multiple IDs are separated by commas in
// the response from the AI.
if ($ids = explode(',', $relevancy_value_ids)) {
foreach ($ids as $id) {
if ($id) {
......
......@@ -93,6 +93,7 @@ class AutoReferenceApplyForm extends FormBase {
'#title' => $this->t('The following relationships have been suggested by the AI tool:'),
];
if ($auto_apply) {
$form['container']['#title'] = $this->t('The following relationships have been applied by the AI tool:');
$form['container']['#description'] = $this->t('The above suggestions have been already applied.');
}
}
......
......@@ -74,6 +74,25 @@ class SettingsForm extends ConfigFormBase {
'#description' => $this->t('By default, editors will be shown a screen with the suggested relationships, where suggestions can be optionally skipped then saved. Check this box to automatically accept the suggestions from the AI.'),
'#default_value' => $config->get('auto_apply_suggestions'),
];
$form['auto_apply_relevance_levels'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Auto-apply relevance levels'),
'#description' => $this->t('By default, editors will be shown a screen with the suggested relationships, where suggestions can be optionally skipped then saved. Select which relevance levels you would like to automatically apply.'),
'#options' => [
'high' => $this->t('High Relevance'),
'medium' => $this->t('Low Relevance'),
],
'#multiple' => TRUE,
'#default_value' => $config->get('auto_apply_relevance_levels'),
'#states' => [
'visible' => [
':input[name="auto_apply_suggestions"]' => ['checked' => TRUE],
],
'required' => [
':input[name="auto_apply_suggestions"]' => ['checked' => TRUE],
],
],
];
return parent::buildForm($form, $form_state);
}
......@@ -93,6 +112,16 @@ class SettingsForm extends ConfigFormBase {
if ($token_limit > $limits[$model]) {
$form_state->setErrorByName('token_limit', $this->t('Maximum token limit for selected model is %limit.', ['%limit' => $limits[$model]]));
}
// Ensure at least one of relevance level is applied when
// auto apply suggestions is in place.
if (
$form_state->getValue('auto_apply_suggestions')
&& !array_filter($form_state->getValue('auto_apply_relevance_levels'))
) {
$message = $this->t('At least one level of relevance should be auto-applied.');
$form_state->setErrorByName('auto_apply_relevance_levels', $message);
}
}
/**
......@@ -101,11 +130,13 @@ class SettingsForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$relevance_levels = array_filter($form_state->getValue('auto_apply_relevance_levels'));
$config = $this->config('ai_auto_reference.settings')
->set('service', $form_state->getValue('service'))
->set('service_model', $form_state->getValue('service_model'))
->set('token_limit', $form_state->getValue('token_limit'))
->set('auto_apply_suggestions', $form_state->getValue('auto_apply_suggestions'));
->set('auto_apply_suggestions', $form_state->getValue('auto_apply_suggestions'))
->set('auto_apply_relevance_levels', array_keys($relevance_levels));
// Only save API key if there is a new value, otherwise leave
// old value unchanged.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment