materio_ai: embedding table and storage service (V3)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\materio_ai\Storage;
|
||||
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\Core\Database\Connection;
|
||||
|
||||
/**
|
||||
* Reads and writes the vectors of the materio_ai_embedding table.
|
||||
*
|
||||
* Vectors are stored as little-endian 32-bit floats: 4 KB for 1 024 values.
|
||||
*/
|
||||
final class EmbeddingStorage {
|
||||
|
||||
private const TABLE = 'materio_ai_embedding';
|
||||
|
||||
public function __construct(
|
||||
private readonly Connection $database,
|
||||
private readonly TimeInterface $time,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Inserts or replaces the vector of a node translation.
|
||||
*
|
||||
* @param float[] $vector
|
||||
* The vector.
|
||||
*/
|
||||
public function save(int $nid, string $langcode, string $model, string $text_hash, array $vector): void {
|
||||
$this->database->merge(self::TABLE)
|
||||
->keys(['nid' => $nid, 'langcode' => $langcode])
|
||||
->fields([
|
||||
'model' => $model,
|
||||
'text_hash' => $text_hash,
|
||||
'vector' => self::pack($vector),
|
||||
'changed' => $this->time->getRequestTime(),
|
||||
])
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the stored row of a node translation, with the vector unpacked.
|
||||
*
|
||||
* @return array{nid: int, langcode: string, model: string, text_hash: string, vector: float[], changed: int}|null
|
||||
* The row, or NULL when the translation has no vector.
|
||||
*/
|
||||
public function load(int $nid, string $langcode): ?array {
|
||||
$row = $this->database->select(self::TABLE, 'e')
|
||||
->fields('e')
|
||||
->condition('nid', $nid)
|
||||
->condition('langcode', $langcode)
|
||||
->execute()
|
||||
->fetchAssoc();
|
||||
return $row ? self::row($row) : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text hashes of a node, keyed by langcode.
|
||||
*
|
||||
* Lets the queue skip texts that did not change, without loading vectors.
|
||||
*
|
||||
* @return array<string, array{model: string, text_hash: string}>
|
||||
* The hashes and models.
|
||||
*/
|
||||
public function loadHashes(int $nid): array {
|
||||
return $this->database->select(self::TABLE, 'e')
|
||||
->fields('e', ['langcode', 'model', 'text_hash'])
|
||||
->condition('nid', $nid)
|
||||
->execute()
|
||||
->fetchAllAssoc('langcode', \PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the vectors of a node, or of one of its translations.
|
||||
*/
|
||||
public function delete(int $nid, ?string $langcode = NULL): int {
|
||||
$query = $this->database->delete(self::TABLE)->condition('nid', $nid);
|
||||
if ($langcode !== NULL) {
|
||||
$query->condition('langcode', $langcode);
|
||||
}
|
||||
return (int) $query->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the stored vectors, optionally for one model.
|
||||
*/
|
||||
public function count(?string $model = NULL): int {
|
||||
$query = $this->database->select(self::TABLE, 'e');
|
||||
if ($model !== NULL) {
|
||||
$query->condition('model', $model);
|
||||
}
|
||||
return (int) $query->countQuery()->execute()->fetchField();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash of a text, as stored in text_hash.
|
||||
*/
|
||||
public static function hash(string $text): string {
|
||||
return hash('sha256', $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Packs a vector as little-endian 32-bit floats.
|
||||
*
|
||||
* @param float[] $vector
|
||||
* The vector.
|
||||
*/
|
||||
public static function pack(array $vector): string {
|
||||
return pack('g*', ...array_values($vector));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpacks a vector stored by pack().
|
||||
*
|
||||
* @return float[]
|
||||
* The vector, keys starting at 0.
|
||||
*/
|
||||
public static function unpack(string $blob): array {
|
||||
return array_values(unpack('g*', $blob));
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts a database row and unpacks its vector.
|
||||
*/
|
||||
private static function row(array $row): array {
|
||||
return [
|
||||
'nid' => (int) $row['nid'],
|
||||
'langcode' => $row['langcode'],
|
||||
'model' => $row['model'],
|
||||
'text_hash' => $row['text_hash'],
|
||||
'vector' => self::unpack($row['vector']),
|
||||
'changed' => (int) $row['changed'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user