Skip to content

[Store] Add Neo4j #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/.env
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ QDRANT_SERVICE_API_KEY=changeMe
SURREALDB_HOST=http://127.0.0.1:8000
SURREALDB_USER=symfony
SURREALDB_PASS=symfony

# Neo4J
NEO4J_HOST=http://127.0.0.1:7474
NEO4J_DATABASE=neo4j
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=symfonyai
8 changes: 8 additions & 0 deletions examples/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ services:
SURREAL_HTTP_MAX_KEY_BODY_SIZE: 49152
ports:
- '8000:8000'

neo4j:
image: neo4j
environment:
NEO4J_AUTH: 'neo4j/${NEO4J_PASSWORD:-symfonyai}'
ports:
- '7474:7474'
- '7687:7687'
75 changes: 75 additions & 0 deletions examples/rag/neo4j.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Agent\Toolbox\AgentProcessor;
use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch;
use Symfony\AI\Agent\Toolbox\Toolbox;
use Symfony\AI\Fixtures\Movies;
use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAI\GPT;
use Symfony\AI\Platform\Bridge\OpenAI\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Store\Bridge\Neo4j\Store;
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\TextDocument;
use Symfony\AI\Store\Document\Vectorizer;
use Symfony\AI\Store\Indexer;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Uid\Uuid;

require_once dirname(__DIR__).'/bootstrap.php';

// initialize the store
$store = new Store(
httpClient: HttpClient::create(),
endpointUrl: env('NEO4J_HOST'),
username: env('NEO4J_USERNAME'),
password: env('NEO4J_PASSWORD'),
databaseName: env('NEO4J_DATABASE'),
vectorIndexName: 'Movies',
nodeName: 'movies',
);

// initialize the table
$store->initialize();

// create embeddings and documents
$documents = [];
foreach (Movies::all() as $i => $movie) {
$documents[] = new TextDocument(
id: Uuid::v4(),
content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'],
metadata: new Metadata($movie),
);
}

// create embeddings for documents
$platform = PlatformFactory::create($_SERVER['OPENAI_API_KEY']);
$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings());
$indexer = new Indexer($vectorizer, $store);
$indexer->index($documents);

$model = new GPT(GPT::GPT_4O_MINI);

$similaritySearch = new SimilaritySearch($platform, $embeddings, $store);
$toolbox = new Toolbox([$similaritySearch], logger: logger());
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, $model, [$processor], [$processor]);

$messages = new MessageBag(
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
Message::ofUser('Which movie fits the theme of technology?')
);
$response = $agent->call($messages);

echo $response->getContent().\PHP_EOL;
1 change: 1 addition & 0 deletions src/store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CHANGELOG
- Pinecone
- Qdrant
- SurrealDB
- Neo4j
* Add Retrieval Augmented Generation (RAG) support:
- Document embedding storage
- Similarity search for relevant documents
Expand Down
8 changes: 7 additions & 1 deletion src/store/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"ai",
"mongodb",
"pinecone",
"chromadb"
"chromadb",
"mariadb",
"postgres",
"meilisearch",
"surrealdb",
"qdrant",
"neo4j"
],
"license": "MIT",
"authors": [
Expand Down
4 changes: 4 additions & 0 deletions src/store/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ You can find more advanced usage in combination with an Agent using the store fo
* `Similarity Search with Pinecone (RAG)`_
* `Similarity Search with Qdrant (RAG)`_
* `Similarity Search with SurrealDB (RAG)`_
* `Similarity Search with Neo4j (RAG)`_

Supported Stores
----------------
Expand All @@ -58,6 +59,7 @@ Supported Stores
* `Postgres`_ (requires `ext-pdo`)
* `Qdrant`_
* `SurrealDB`_
* `Neo4j`_

.. note::

Expand Down Expand Up @@ -98,6 +100,7 @@ This leads to a store implementing two methods::
.. _`Similarity Search with SurrealDB (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/surrealdb.php
.. _`Similarity Search with memory storage (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/in-memory.php
.. _`Similarity Search with Qdrant (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/qdrant.php
.. _`Similarity Search with Neo4j (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/neo4j.php
.. _`Azure AI Search`: https://azure.microsoft.com/products/ai-services/ai-search
.. _`Chroma`: https://www.trychroma.com/
.. _`MariaDB`: https://mariadb.org/projects/mariadb-vector/
Expand All @@ -108,4 +111,5 @@ This leads to a store implementing two methods::
.. _`SurrealDB`: https://surrealdb.com/
.. _`InMemory`: https://www.php.net/manual/en/language.types.array.php
.. _`Qdrant`: https://qdrant.tech/
.. _`Neo4j`: https://neo4j.com/
.. _`GitHub`: https://github.com/symfony/ai/issues/16
117 changes: 117 additions & 0 deletions src/store/src/Bridge/Neo4j/Store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Store\Bridge\Neo4j;

use Symfony\AI\Platform\Vector\NullVector;
use Symfony\AI\Platform\Vector\Vector;
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\VectorDocument;
use Symfony\AI\Store\Exception\InvalidArgumentException;
use Symfony\AI\Store\InitializableStoreInterface;
use Symfony\AI\Store\VectorStoreInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Guillaume Loulier <[email protected]>
*/
final readonly class Store implements InitializableStoreInterface, VectorStoreInterface
{
public function __construct(
private HttpClientInterface $httpClient,
private string $endpointUrl,
#[\SensitiveParameter] private string $username,
#[\SensitiveParameter] private string $password,
#[\SensitiveParameter] private string $databaseName,
private string $vectorIndexName,
private string $nodeName,
private string $embeddingsField = 'embeddings',
private int $embeddingsDimension = 1536,
private string $embeddingsDistance = 'cosine',
private bool $quantization = false,
) {
}

public function add(VectorDocument ...$documents): void
{
foreach ($documents as $document) {
$this->request('POST', \sprintf('db/%s/query/v2', $this->databaseName), [
'statement' => \sprintf('CREATE (n:%s {id: $id, metadata: $metadata, %s: $embeddings}) RETURN n', $this->nodeName, $this->embeddingsField),
'parameters' => [
'id' => $document->id->toRfc4122(),
'metadata' => json_encode($document->metadata->getArrayCopy()),
'embeddings' => $document->vector->getData(),
],
]);
}
}

public function query(Vector $vector, array $options = [], ?float $minScore = null): array
{
$response = $this->request('POST', \sprintf('db/%s/query/v2', $this->databaseName), [
'statement' => \sprintf('CALL db.index.vector.queryNodes("%s", 5, $vectors) YIELD node, score RETURN node, score', $this->vectorIndexName),
'parameters' => [
'vectors' => $vector->getData(),
],
]);

return array_map($this->convertToVectorDocument(...), $response['data']['values']);
}

public function initialize(array $options = []): void
{
$this->request('POST', \sprintf('db/%s/query/v2', $this->databaseName), [
'statement' => \sprintf(
'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}}',
$this->vectorIndexName, $this->nodeName, $this->embeddingsField, $this->embeddingsDimension, $this->embeddingsDistance, $this->quantization ? 'true' : 'false',
),
]);
}

/**
* @param array<string, mixed> $payload
*
* @return array<string, mixed>
*/
private function request(string $method, string $endpoint, array $payload = []): array
{
$url = \sprintf('%s/%s', $this->endpointUrl, $endpoint);

$response = $this->httpClient->request($method, $url, [
'auth_basic' => \sprintf('%s:%s', $this->username, $this->password),
'json' => $payload,
]);

return $response->toArray();
}

/**
* @param array<string|int, mixed> $data
*/
private function convertToVectorDocument(array $data): VectorDocument
{
$payload = $data[0];

$id = $payload['properties']['id'] ?? throw new InvalidArgumentException('Missing "id" field in the document data');

$vector = !\array_key_exists($this->embeddingsField, $payload['properties']) || null === $payload['properties'][$this->embeddingsField]
? new NullVector()
: new Vector($payload['properties'][$this->embeddingsField]);

return new VectorDocument(
id: Uuid::fromString($id),
vector: $vector,
metadata: new Metadata(json_decode($payload['properties']['metadata'], true)),
score: $data[1] ?? null
);
}
}
Loading
Loading