Skip to content
Snippets Groups Projects

Issue #3354838: openai_content functionalities appear only on edition and not on creation

Open Issue #3354838: openai_content functionalities appear only on edition and not on creation
Open Mitchel requested to merge issue/openai-3354838:1.0.x into 1.0.x
@@ -8,6 +8,7 @@
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\openai\Utility\StringHelper;
@@ -17,8 +18,10 @@ use Drupal\openai\Utility\StringHelper;
*/
function openai_content_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$account = \Drupal::currentUser();
if (preg_match('/node_(add|.*_edit)_form/', $form_id) && $account->hasPermission('access openai content tools')) {
/** @var \Drupal\node\Entity\Node $entity */
if (preg_match('/node_(.*)_form/', $form_id) && $account->hasPermission('access openai content tools')) {
$entity = $form_state->getFormObject()->getEntity();
$options = get_all_text_with_summary_fields($entity);
$form['openai_tone_edit'] = [
'#type' => 'details',
'#title' => t('Adjust content tone'),
@@ -30,6 +33,13 @@ function openai_content_form_alter(array &$form, FormStateInterface $form_state,
'#markup' => t('Have OpenAI check your content and adjust the tone of it for different reader audiences for you.'),
];
$form['openai_tone_edit']['target_field'] = [
'#type' => 'select',
'#title' => t('Choose field'),
'#description' => t('Select what field you would like to change the tone of.'),
'#options' => $options,
];
// @todo: these values should be configurable options
$form['openai_tone_edit']['tone'] = [
'#type' => 'select',
@@ -64,6 +74,14 @@ function openai_content_form_alter(array &$form, FormStateInterface $form_state,
'#type' => 'details',
'#title' => t('Summarize text'),
'#group' => 'advanced',
'#tree' => TRUE,
];
$form['openai_summarize']['target_field'] = [
'#type' => 'select',
'#title' => t('Choose field'),
'#description' => t('Select what field you would like to create a summary of.'),
'#options' => $options,
];
$form['openai_summarize']['response'] = [
@@ -87,6 +105,14 @@ function openai_content_form_alter(array &$form, FormStateInterface $form_state,
'#type' => 'details',
'#title' => t('Suggest taxonomy'),
'#group' => 'advanced',
'#tree' => TRUE,
];
$form['openai_suggest']['target_field'] = [
'#type' => 'select',
'#title' => t('Choose field'),
'#description' => t('Select what field you would like to suggest taxonomy of.'),
'#options' => $options,
];
$form['openai_suggest']['response'] = [
@@ -108,6 +134,26 @@ function openai_content_form_alter(array &$form, FormStateInterface $form_state,
}
}
/**
* Get a list of all text with summary fields on the current node.
*
* @param Node $node
* The node on the form.
*
* @return array
* List of all text with summary fields
*/
function get_all_text_with_summary_fields(Node $node) {
$fields = $node->getFieldDefinitions();
$text_and_summary_fields = [];
foreach ($fields as $field) {
if ($field->getType() === 'text_with_summary') {
$text_and_summary_fields[$field->getName()] = $field->getLabel();
}
}
return $text_and_summary_fields;
}
/**
* The AJAX callback for adjusting the tone of body content.
*
@@ -120,17 +166,19 @@ function openai_content_form_alter(array &$form, FormStateInterface $form_state,
* The HTML response.
*/
function openai_content_node_adjust_tone(array &$form, FormStateInterface $form_state) {
$body = $form_state->getValue('body')[0]['value'];
$tone = $form_state->getValue('openai_tone_edit')['tone'];
$openai_tone_edit = $form_state->getValue('openai_tone_edit');
$target_field = $openai_tone_edit['target_field'];
$target_field = $form_state->getValue($target_field)[0]['value'];
$tone = $openai_tone_edit['tone'];
if (!empty($body)) {
if (!empty($target_field)) {
$client = \Drupal::service('openai.client');
$body = StringHelper::prepareText($body, [], 3900);
$target_field = StringHelper::prepareText($target_field, [], 3900);
$response = $client->completions()->create(
[
'model' => 'text-davinci-003',
'prompt' => 'Change the tone of the following text to be more ' . $tone . ':\r\n"' . $body . '"',
'prompt' => 'Change the tone of the following text to be more ' . $tone . ':\r\n"' . $target_field . '"',
'temperature' => 0.7,
'max_tokens' => 2048,
],
@@ -159,16 +207,18 @@ function openai_content_node_adjust_tone(array &$form, FormStateInterface $form_
* The HTML response.
*/
function openai_content_node_field_summarize(array &$form, FormStateInterface $form_state) {
$body = $form_state->getValue('body')[0]['value'];
$openai_summarize = $form_state->getValue('openai_summarize');
$target_field = $openai_summarize['target_field'];
$target_field = $form_state->getValue($target_field)[0]['value'];
if (!empty($body)) {
if (!empty($target_field)) {
$client = \Drupal::service('openai.client');
$body = StringHelper::prepareText($body, [], 3900);
$target_field = StringHelper::prepareText($target_field, [], 3900);
$response = $client->completions()->create(
[
'model' => 'text-davinci-003',
'prompt' => 'Create a detailed summary of the following text in less than 130 words:\r\n"' . $body . '"',
'prompt' => 'Create a detailed summary of the following text in less than 130 words:\r\n"' . $target_field . '"',
'temperature' => 0.7,
'max_tokens' => 2048,
],
@@ -197,16 +247,18 @@ function openai_content_node_field_summarize(array &$form, FormStateInterface $f
* The HTML response.
*/
function openai_content_node_suggest_taxonomy(array &$form, FormStateInterface $form_state) {
$body = $form_state->getValue('body')[0]['value'];
$openai_suggest = $form_state->getValue('openai_suggest');
$target_field = $openai_suggest['target_field'];
$target_field = $form_state->getValue($target_field)[0]['value'];
if (!empty($body)) {
if (!empty($target_field)) {
$client = \Drupal::service('openai.client');
$body = StringHelper::prepareText($body, [], 3900);
$target_field = StringHelper::prepareText($target_field, [], 3900);
$response = $client->completions()->create(
[
'model' => 'text-davinci-003',
'prompt' => 'Suggest five words to classify the following text. The words must be nouns or adjectives in a comma delimited list:\r\n"' . $body . '"',
'prompt' => 'Suggest five words to classify the following text. The words must be nouns or adjectives in a comma delimited list:\r\n"' . $target_field . '"',
'temperature' => 0.4,
'max_tokens' => 2048,
],
Loading