configFactory->get('materio_ai.settings')->get('enabled'); } /** * Loads the model with the normal timeout, and ends the pause. * * Uses the client options of the searches (num_thread): with other options * Ollama would load the model again. * * @throws \Drupal\materio_ai\Exception\EmbeddingException * When the model fails. */ public function warmUp(): void { $this->client->embed('materiO'); $this->cache->delete('pause:' . $this->client->getModel()); } /** * Returns the vector of a phrase. * * @return float[] * The vector. * * @throws \Drupal\materio_ai\Exception\EmbeddingException * When the model fails or is too slow; ModelPausedException when it * failed less than PAUSE seconds ago. */ public function embed(string $phrase): array { $model = $this->client->getModel(); $cid = 'query:' . $model . ':' . hash('sha256', $phrase); if ($cached = $this->cache->get($cid)) { return $cached->data; } $pause_cid = 'pause:' . $model; if ($this->cache->get($pause_cid)) { throw new ModelPausedException(sprintf('Model %s paused after a failure.', $model)); } $timeout = (float) ($this->configFactory->get('materio_ai.settings')->get('search_timeout') ?? 1.0); try { $vector = $this->client->embed($phrase, max(0.1, $timeout)); } catch (EmbeddingException $e) { $this->cache->set($pause_cid, TRUE, $this->time->getRequestTime() + self::PAUSE); throw $e; } $this->cache->set($cid, $vector, $this->time->getRequestTime() + self::LIFETIME); return $vector; } }