diff --git a/web/modules/custom/materio_ai/materio_ai.services.yml b/web/modules/custom/materio_ai/materio_ai.services.yml index ec2a571e..397ce80d 100644 --- a/web/modules/custom/materio_ai/materio_ai.services.yml +++ b/web/modules/custom/materio_ai/materio_ai.services.yml @@ -2,3 +2,8 @@ services: logger.channel.materio_ai: parent: logger.channel_base arguments: ['materio_ai'] + + materio_ai.embedding_client: + class: Drupal\materio_ai\Embedding\OllamaEmbeddingClient + arguments: ['@http_client', '@config.factory'] + Drupal\materio_ai\Embedding\EmbeddingClientInterface: '@materio_ai.embedding_client' diff --git a/web/modules/custom/materio_ai/src/Embedding/EmbeddingClientInterface.php b/web/modules/custom/materio_ai/src/Embedding/EmbeddingClientInterface.php new file mode 100644 index 00000000..16f04e95 --- /dev/null +++ b/web/modules/custom/materio_ai/src/Embedding/EmbeddingClientInterface.php @@ -0,0 +1,48 @@ +config(); + $timeout = max(1, (int) $config->get('timeout')); + $url = rtrim((string) $config->get('url'), '/') . '/api/embed'; + + try { + $response = $this->httpClient->request('POST', $url, [ + 'json' => [ + 'model' => $this->getModel(), + 'input' => $text, + // Ollama counts the cores of the host, not the CPU limit of its + // container: without this option it starts too many threads. + 'options' => ['num_thread' => max(1, (int) $config->get('num_thread'))], + ], + 'timeout' => $timeout, + 'connect_timeout' => min($timeout, 3), + ]); + } + catch (GuzzleException $e) { + throw new EmbeddingException(sprintf('Embedding service %s failed: %s', $url, $e->getMessage()), 0, $e); + } + + try { + $data = json_decode((string) $response->getBody(), TRUE, 512, JSON_THROW_ON_ERROR); + } + catch (\JsonException $e) { + throw new EmbeddingException(sprintf('Embedding service %s returned invalid JSON.', $url), 0, $e); + } + + $vector = $data['embeddings'][0] ?? NULL; + if (!is_array($vector)) { + throw new EmbeddingException(sprintf('Embedding service %s returned no vector.', $url)); + } + $dimension = $this->getDimension(); + if (count($vector) !== $dimension) { + throw new EmbeddingException(sprintf('Expected a vector of %d values, got %d (model %s).', $dimension, count($vector), $this->getModel())); + } + foreach ($vector as $value) { + if (!is_int($value) && !is_float($value)) { + throw new EmbeddingException('The vector contains a non numeric value.'); + } + } + return array_map('floatval', $vector); + } + + /** + * {@inheritdoc} + */ + public function embedMultiple(array $texts): array { + $vectors = []; + foreach ($texts as $key => $text) { + $vectors[$key] = $this->embed($text); + } + return $vectors; + } + + /** + * {@inheritdoc} + */ + public function getModel(): string { + return (string) $this->config()->get('model'); + } + + /** + * {@inheritdoc} + */ + public function getDimension(): int { + return (int) $this->config()->get('dimension'); + } + + /** + * Returns the module settings. + */ + private function config(): ImmutableConfig { + return $this->configFactory->get('materio_ai.settings'); + } + +} diff --git a/web/modules/custom/materio_ai/src/Exception/EmbeddingException.php b/web/modules/custom/materio_ai/src/Exception/EmbeddingException.php new file mode 100644 index 00000000..6ce6f39e --- /dev/null +++ b/web/modules/custom/materio_ai/src/Exception/EmbeddingException.php @@ -0,0 +1,15 @@ +