Skip to content

Commit a63f7a7

Browse files
committed
refactor(store): surrealDB store improved && tested
1 parent 77fdbc5 commit a63f7a7

File tree

3 files changed

+260
-55
lines changed

3 files changed

+260
-55
lines changed

src/store/doc/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ You can find more advanced usage in combination with an Agent using the store fo
4141
* `Similarity Search with MongoDB (RAG)`_
4242
* `Similarity Search with Pinecone (RAG)`_
4343
* `Similarity Search with Meilisearch (RAG)`_
44+
* `Similarity Search with SurrealDB (RAG)`_
4445
* `Similarity Search with memory storage (RAG)`_
4546

4647
Supported Stores
@@ -53,6 +54,7 @@ Supported Stores
5354
* `Pinecone`_ (requires `probots-io/pinecone-php` as additional dependency)
5455
* `Postgres`_ (requires `ext-pdo`)
5556
* `Meilisearch`_
57+
* `SurrealDB`_
5658
* `InMemory`_
5759

5860
.. note::
@@ -91,6 +93,7 @@ This leads to a store implementing two methods::
9193
.. _`Similarity Search with MongoDB (RAG)`: https://github.com/symfony/ai/blob/main/examples/store/mongodb-similarity-search.php
9294
.. _`Similarity Search with Pinecone (RAG)`: https://github.com/symfony/ai/blob/main/examples/store/pinecone-similarity-search.php
9395
.. _`Similarity Search with Meilisearch (RAG)`: https://github.com/symfony/ai/blob/main/examples/store/meilisearch-similarity-search.php
96+
.. _`Similarity Search with SurrealDB (RAG)`: https://github.com/symfony/ai/blob/main/examples/store/surrealdb-similarity-search.php
9497
.. _`Similarity Search with memory storage (RAG)`: https://github.com/symfony/ai/blob/main/examples/store/memory-similarity-search.php
9598
.. _`Azure AI Search`: https://azure.microsoft.com/products/ai-services/ai-search
9699
.. _`Chroma`: https://www.trychroma.com/
@@ -99,5 +102,6 @@ This leads to a store implementing two methods::
99102
.. _`Pinecone`: https://www.pinecone.io/
100103
.. _`Postgres`: https://www.postgresql.org/about/news/pgvector-070-released-2852/
101104
.. _`Meilisearch`: https://www.meilisearch.com/
105+
.. _`SurrealDB`: https://surrealdb.com/
102106
.. _`InMemory`: https://www.php.net/manual/en/language.types.array.php
103107
.. _`GitHub`: https://github.com/symfony/ai/issues/16

src/store/src/Bridge/SurrealDB/Store.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
namespace Symfony\AI\Store\Bridge\SurrealDB;
1313

14-
use Symfony\AI\AIBundle\Exception\InvalidArgumentException;
1514
use Symfony\AI\Platform\Vector\NullVector;
1615
use Symfony\AI\Platform\Vector\Vector;
1716
use Symfony\AI\Store\Document\Metadata;
1817
use Symfony\AI\Store\Document\VectorDocument;
18+
use Symfony\AI\Store\Exception\InvalidArgumentException;
1919
use Symfony\AI\Store\Exception\RuntimeException;
2020
use Symfony\AI\Store\InitializableStoreInterface;
2121
use Symfony\AI\Store\VectorStoreInterface;
@@ -53,7 +53,7 @@ public function add(VectorDocument ...$documents): void
5353

5454
foreach ($documents as $document) {
5555
if (self::MAXIMUM_EMBEDDINGS_DIMENSIONS < $document->vector->getDimensions()) {
56-
throw new RuntimeException(\sprintf('The SurrealDB HTTP API does not support embeddings with more than %d dimensions, found %d', self::MAXIMUM_EMBEDDINGS_DIMENSIONS, $document->vector->getDimensions()));
56+
throw new InvalidArgumentException(\sprintf('The SurrealDB HTTP API does not support embeddings with more than %d dimensions, found %d', self::MAXIMUM_EMBEDDINGS_DIMENSIONS, $document->vector->getDimensions()));
5757
}
5858

5959
$this->request('POST', \sprintf('key/%s', $this->table), $this->convertToIndexableArray($document), [
@@ -66,11 +66,17 @@ public function add(VectorDocument ...$documents): void
6666

6767
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
6868
{
69+
if (self::MAXIMUM_EMBEDDINGS_DIMENSIONS < $vector->getDimensions()) {
70+
throw new InvalidArgumentException(\sprintf('The dimensions of the vector must be less than or equal to %d, found %d', self::MAXIMUM_EMBEDDINGS_DIMENSIONS, $vector->getDimensions()));
71+
}
72+
6973
$authenticationToken = $this->authenticate($options);
7074

75+
$vectors = json_encode($vector->getData());
76+
7177
$results = $this->request('POST', 'sql', \sprintf(
7278
'SELECT id, %s, _metadata, vector::similarity::%s(%s, %s) AS distance FROM %s WHERE %s <|2|> %s;',
73-
$this->vectorFieldName, $this->strategy, $this->vectorFieldName, json_encode($vector->getData()), $this->table, $this->vectorFieldName, json_encode($vector->getData()),
79+
$this->vectorFieldName, $this->strategy, $this->vectorFieldName, $vectors, $this->table, $this->vectorFieldName, $vectors,
7480
), [
7581
'Surreal-NS' => $this->namespace,
7682
'Surreal-DB' => $this->database,
@@ -94,6 +100,12 @@ public function initialize(array $options = []): void
94100
]);
95101
}
96102

103+
/**
104+
* @param array<string, mixed>|string $payload
105+
* @param array<string, mixed> $extraHeaders
106+
*
107+
* @return array<string|int, mixed>
108+
*/
97109
private function request(string $method, string $endpoint, array|string $payload, array $extraHeaders = []): array
98110
{
99111
$url = \sprintf('%s/%s', $this->endpointUrl, $endpoint);
@@ -154,14 +166,19 @@ private function convertToVectorDocument(array $data): VectorDocument
154166
);
155167
}
156168

169+
/**
170+
* @param array{
171+
* namespacedUser?: bool
172+
* } $options The namespacedUser option is used to determine if the user is root or not, if not, both the namespace and database must be specified
173+
*/
157174
private function authenticate(array $options): string
158175
{
159176
$authenticationPayload = [
160177
'user' => $this->user,
161178
'pass' => $this->password,
162179
];
163180

164-
if (\array_key_exists('namespacedUser', $options) && '' !== $options['namespacedUser']) {
181+
if (\array_key_exists('namespacedUser', $options) && !$options['namespacedUser']) {
165182
$authenticationPayload['ns'] = $this->namespace;
166183
$authenticationPayload['db'] = $this->database;
167184
}

0 commit comments

Comments
 (0)