Skip to content
Snippets Groups Projects

Resolve #3373585 "Drupal calls should"

17 files
+ 362
152
Compare changes
  • Side-by-side
  • Inline
Files
17
+ 66
21
@@ -2,13 +2,15 @@
namespace Drupal\chat_ai\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\FocusFirstCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\chat_ai\ChatAIService;
use Drupal\chat_ai\Embeddings;
use Drupal\chat_ai\Supabase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@@ -16,6 +18,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class ChatBlockForm extends FormBase {
/**
* The messages array for the Chat AI form.
*
* @var array
*/
private static $messages = [];
/**
@@ -33,15 +41,37 @@ class ChatBlockForm extends FormBase {
protected $embeddings;
/**
* Constructs new FieldBlockDeriver.
* The chat AI service.
*
* @var \Drupal\chat_ai\ChatAIService
*/
protected $chatAIService;
/**
* The Supabase service.
*
* @var \Drupal\chat_ai\Supabase
*/
protected $supabase;
/**
* Constructs new ChatBlockForm.
*
* @param \Drupal\chat_ai\Embeddings $embeddings
* The embeddings service.
* @param \Drupal\chat_ai\ChatAIService $chatAIService
* The chat AI service.
* @param \Drupal\chat_ai\Supabase $supabase
* The Supabase service.
*/
public function __construct(
Embeddings $embeddings
) {
Embeddings $embeddings,
ChatAIService $chatAIService,
Supabase $supabase,
) {
$this->embeddings = $embeddings;
$this->chatAIService = $chatAIService;
$this->supabase = $supabase;
}
/**
@@ -49,10 +79,19 @@ class ChatBlockForm extends FormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('chat_ai.embeddings')
$container->get('chat_ai.embeddings'),
$container->get('chat_ai.service'),
$container->get('chat_ai.supabase')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'chat_ai_block';
}
/**
* {@inheritdoc}
*/
@@ -96,13 +135,6 @@ class ChatBlockForm extends FormBase {
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
@@ -126,26 +158,39 @@ class ChatBlockForm extends FormBase {
// Debug
// $message = "<p>{$form_state->getValue('message')}</p>";
// $choices = "<p class='chat-gpt'>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took.</p>";
// $choices = "<p class='chat-gpt'>Lorem Ipsum is simply dummy text of the
// printing and typesetting industry. "Lorem Ipsum has been the industry's
// standard dummy text ever since the 1500s, when an unknown printer took.
// </p>";.
// $response = new AjaxResponse();
// $response->addCommand(new AppendCommand('.edit-chat-container', $message));
// $response->addCommand(new AppendCommand('.edit-chat-container', $choices));
// $response->addCommand(
// new AppendCommand(
// '.edit-chat-container',
// $message
// )
// );
// $response->addCommand(
// new AppendCommand(
// '.edit-chat-container',
// $choices
// )
// );
// $response->addCommand(new InvokeCommand('#edit-message', 'val', ['']));
// $response->addCommand(new FocusFirstCommand('#edit-message'));
// return $response;
// ....
$message = "<p>{$form_state->getValue('message')}</p>";
// @todo Add DI
$chat = \Drupal::service('chat_ai.service');
$chat = $this->chatAIService;
$history = $chat->chatHistoryRetrieve(strip_tags(trim($message)));
if (is_array($history) && !empty($history)) {
$choices = reset($history);
}
else {
$context = \Drupal::service('chat_ai.supabase')->getMatchingChunks($message);
$context = $this->supabase->getMatchingChunks($message);
$context = implode('\n', $context);
// $choices = \Drupal::service('chat_ai.service')->completion($message, $context);
$choices = \Drupal::service('chat_ai.service')->chat($message, $context);
// $choices = $this->chatAIService->completion($message, $context);
$choices = $this->chatAIService->chat($message, $context);
// @todo Split in separate answers
$choices = implode('<br />', $choices);
$choices = "<p class='chat-gpt'>{$choices}</p>";
Loading