Skip to content
Snippets Groups Projects
Commit ada139b3 authored by Marcus Johansson's avatar Marcus Johansson
Browse files

Issue #3446939 by Marcus_Johansson: Add base class for text to JSON field

parent 087103a1
No related branches found
No related tags found
No related merge requests found
Pipeline #171886 failed
<?php
namespace Drupal\ai_interpolator\PluginBaseClasses;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
/**
* This is a base class that can be used for LLMs json output.
*/
class ComplexTextChat extends SimpleTextChat {
/**
* {@inheritDoc}
*/
public function helpText() {
return "This is a simple text to JSON field model.";
}
/**
* {@inheritDoc}
*/
public function generate(ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition, array $interpolatorConfig) {
// Generate the real prompt if needed.
$prompts = [];
// @phpstan-ignore-next-line
if (!empty($interpolatorConfig['mode']) && $interpolatorConfig['mode'] == 'token' && \Drupal::service('module_handler')->moduleExists('token')) {
$prompts[] = \Drupal::service('ai_interpolator.prompt_helper')->renderTokenPrompt($interpolatorConfig['token'], $entity); /* @phpstan-ignore-line */
} elseif ($this->needsPrompt()) {
// Run rule.
foreach ($entity->get($interpolatorConfig['base_field'])->getValue() as $i => $item) {
// Get tokens.
$tokens = $this->generateTokens($entity, $fieldDefinition, $interpolatorConfig, $i);
$prompts[] = \Drupal::service('ai_interpolator.prompt_helper')->renderPrompt($interpolatorConfig['prompt'], $tokens, $i); /* @phpstan-ignore-line */
}
}
// Add JSON output.
foreach ($prompts as $key => $prompt) {
$prompts[$key] = $prompt;
}
$total = [];
foreach ($prompts as $prompt) {
$values = $this->generateResponse($prompt, $interpolatorConfig, $entity, $fieldDefinition);
if (!empty($values)) {
$total[] = $values;
}
}
return $total;
}
/**
* {@inheritDoc}
*/
public function verifyValue(ContentEntityInterface $entity, $value, FieldDefinitionInterface $fieldDefinition) {
// Check so its valid JSON.
if (empty($value)) {
return FALSE;
}
$json = json_decode($value, TRUE);
if (empty($json)) {
return FALSE;
}
return TRUE;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment