- QueryEmbedder: vector of a search phrase cached 30 days (cache bin materio_ai), model given search_timeout seconds (1 by default, new setting, update 11001), left alone 60 s after a failure (ModelPausedException, not logged). - NeighbourSearch honours the enabled setting: the search called the model even when materio_ai was switched off. - materio_ai_cron() loads the model with the normal timeout: after a restart, loading (2.9 s) exceeds the search delay and Ollama drops it, so searches alone would never load it. Ultimate cron job exported. - On any model failure the search answers with the keyword results only. - QueryEmbedderTest: cache, delay, pause, cache during the pause, warm-up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
89 lines
2.5 KiB
PHP
89 lines
2.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Hooks of materio_ai: keep the vectors in step with the nodes.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Drupal\materio_ai\Exception\EmbeddingException;
|
|
use Drupal\node\NodeInterface;
|
|
use Drupal\taxonomy\TermInterface;
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_insert() for node.
|
|
*/
|
|
function materio_ai_node_insert(NodeInterface $node): void {
|
|
\Drupal::service('materio_ai.vectorizer')->enqueue($node);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_update() for node.
|
|
*
|
|
* Also covers a new translation, an unpublished node and a workflow change.
|
|
*/
|
|
function materio_ai_node_update(NodeInterface $node): void {
|
|
\Drupal::service('materio_ai.vectorizer')->enqueue($node);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_translation_delete() for node.
|
|
*/
|
|
function materio_ai_node_translation_delete(NodeInterface $translation): void {
|
|
\Drupal::service('materio_ai.embedding_storage')->delete((int) $translation->id(), $translation->language()->getId());
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_delete() for node.
|
|
*
|
|
* Done at once, even when vectorization is switched off: no model needed.
|
|
*/
|
|
function materio_ai_node_delete(NodeInterface $node): void {
|
|
\Drupal::service('materio_ai.embedding_storage')->delete((int) $node->id());
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_update() for taxonomy_term.
|
|
*
|
|
* A renamed tag, thesaurus term or company changes the text of its nodes.
|
|
*/
|
|
function materio_ai_taxonomy_term_update(TermInterface $term): void {
|
|
\Drupal::service('materio_ai.vectorizer')->enqueueForTerm($term);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_translation_delete() for taxonomy_term.
|
|
*/
|
|
function materio_ai_taxonomy_term_translation_delete(TermInterface $translation): void {
|
|
\Drupal::service('materio_ai.vectorizer')->enqueueForTerm($translation, TRUE);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_predelete() for taxonomy_term.
|
|
*
|
|
* Predelete: the node references still exist, so the nodes can be found.
|
|
*/
|
|
function materio_ai_taxonomy_term_predelete(TermInterface $term): void {
|
|
\Drupal::service('materio_ai.vectorizer')->enqueueForTerm($term, TRUE);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_cron().
|
|
*
|
|
* Loads the model: a search gives it 1 s, too short to load it after a
|
|
* restart of Ollama (H8).
|
|
*/
|
|
function materio_ai_cron(): void {
|
|
$embedder = \Drupal::service('materio_ai.query_embedder');
|
|
if (!$embedder->isEnabled()) {
|
|
return;
|
|
}
|
|
try {
|
|
$embedder->warmUp();
|
|
}
|
|
catch (EmbeddingException $e) {
|
|
\Drupal::logger('materio_ai')->warning('Model warm-up failed: @message', ['@message' => $e->getMessage()]);
|
|
}
|
|
}
|