|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Store\Bridge\Neo4j; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Vector\NullVector; |
| 15 | +use Symfony\AI\Platform\Vector\Vector; |
| 16 | +use Symfony\AI\Store\Document\Metadata; |
| 17 | +use Symfony\AI\Store\Document\VectorDocument; |
| 18 | +use Symfony\AI\Store\Exception\InvalidArgumentException; |
| 19 | +use Symfony\AI\Store\InitializableStoreInterface; |
| 20 | +use Symfony\AI\Store\VectorStoreInterface; |
| 21 | +use Symfony\Component\Uid\Uuid; |
| 22 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Guillaume Loulier <[email protected]> |
| 26 | + */ |
| 27 | +final readonly class Store implements InitializableStoreInterface, VectorStoreInterface |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private HttpClientInterface $httpClient, |
| 31 | + private string $endpointUrl, |
| 32 | + #[\SensitiveParameter] private string $username, |
| 33 | + #[\SensitiveParameter] private string $password, |
| 34 | + #[\SensitiveParameter] private string $databaseName, |
| 35 | + private string $vectorIndexName, |
| 36 | + private string $nodeName, |
| 37 | + private string $embeddingsField = 'embeddings', |
| 38 | + private int $embeddingsDimension = 1536, |
| 39 | + private string $embeddingsDistance = 'cosine', |
| 40 | + private bool $quantization = false, |
| 41 | + ) { |
| 42 | + } |
| 43 | + |
| 44 | + public function add(VectorDocument ...$documents): void |
| 45 | + { |
| 46 | + foreach ($documents as $document) { |
| 47 | + $this->request('POST', \sprintf('db/%s/query/v2', $this->databaseName), [ |
| 48 | + 'statement' => \sprintf('CREATE (n:%s {id: $id, metadata: $metadata, %s: $embeddings}) RETURN n', $this->nodeName, $this->embeddingsField), |
| 49 | + 'parameters' => [ |
| 50 | + 'id' => $document->id->toRfc4122(), |
| 51 | + 'metadata' => json_encode($document->metadata->getArrayCopy()), |
| 52 | + 'embeddings' => $document->vector->getData(), |
| 53 | + ], |
| 54 | + ]); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function query(Vector $vector, array $options = [], ?float $minScore = null): array |
| 59 | + { |
| 60 | + $response = $this->request('POST', \sprintf('db/%s/query/v2', $this->databaseName), [ |
| 61 | + 'statement' => \sprintf('CALL db.index.vector.queryNodes("%s", 5, $vectors) YIELD node, score RETURN node, score', $this->vectorIndexName), |
| 62 | + 'parameters' => [ |
| 63 | + 'vectors' => $vector->getData(), |
| 64 | + ], |
| 65 | + ]); |
| 66 | + |
| 67 | + return array_map($this->convertToVectorDocument(...), $response['data']['values']); |
| 68 | + } |
| 69 | + |
| 70 | + public function initialize(array $options = []): void |
| 71 | + { |
| 72 | + $this->request('POST', \sprintf('db/%s/query/v2', $this->databaseName), [ |
| 73 | + 'statement' => \sprintf( |
| 74 | + 'CREATE VECTOR INDEX %s IF NOT EXISTS FOR (n:%s) ON n.%s OPTIONS { indexConfig: {`vector.dimensions`: %d, `vector.similarity_function`: "%s", `vector.quantization.enabled`: %s}}', |
| 75 | + $this->vectorIndexName, $this->nodeName, $this->embeddingsField, $this->embeddingsDimension, $this->embeddingsDistance, $this->quantization ? 'true' : 'false', |
| 76 | + ), |
| 77 | + ]); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param array<string, mixed> $payload |
| 82 | + * |
| 83 | + * @return array<string, mixed> |
| 84 | + */ |
| 85 | + private function request(string $method, string $endpoint, array $payload = []): array |
| 86 | + { |
| 87 | + $url = \sprintf('%s/%s', $this->endpointUrl, $endpoint); |
| 88 | + |
| 89 | + $response = $this->httpClient->request($method, $url, [ |
| 90 | + 'auth_basic' => \sprintf('%s:%s', $this->username, $this->password), |
| 91 | + 'json' => $payload, |
| 92 | + ]); |
| 93 | + |
| 94 | + return $response->toArray(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param array<string|int, mixed> $data |
| 99 | + */ |
| 100 | + private function convertToVectorDocument(array $data): VectorDocument |
| 101 | + { |
| 102 | + $payload = $data[0]; |
| 103 | + |
| 104 | + $id = $payload['properties']['id'] ?? throw new InvalidArgumentException('Missing "id" field in the document data'); |
| 105 | + |
| 106 | + $vector = !\array_key_exists($this->embeddingsField, $payload['properties']) || null === $payload['properties'][$this->embeddingsField] |
| 107 | + ? new NullVector() |
| 108 | + : new Vector($payload['properties'][$this->embeddingsField]); |
| 109 | + |
| 110 | + return new VectorDocument( |
| 111 | + id: Uuid::fromString($id), |
| 112 | + vector: $vector, |
| 113 | + metadata: new Metadata(json_decode($payload['properties']['metadata'], true)), |
| 114 | + score: $data[1] ?? null |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments