Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
108 lines
3.0 KiB
PHP
108 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Drupal\materio_ai\Embedding;
|
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface;
|
|
use Drupal\Core\Config\ImmutableConfig;
|
|
use Drupal\materio_ai\Exception\EmbeddingException;
|
|
use GuzzleHttp\ClientInterface;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
/**
|
|
* Embedding client for an Ollama service (POST /api/embed).
|
|
*/
|
|
final class OllamaEmbeddingClient implements EmbeddingClientInterface {
|
|
|
|
public function __construct(
|
|
private readonly ClientInterface $httpClient,
|
|
private readonly ConfigFactoryInterface $configFactory,
|
|
) {}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function embed(string $text): array {
|
|
$text = trim($text);
|
|
if ($text === '') {
|
|
throw new EmbeddingException('Cannot vectorize an empty text.');
|
|
}
|
|
$config = $this->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');
|
|
}
|
|
|
|
}
|