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'); } }