Skip to content

Commit c082f1b

Browse files
committed
rebase
1 parent 35c7d6d commit c082f1b

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

examples/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ SURREALDB_USER=symfony
9292
SURREALDB_PASS=symfony
9393

9494
# Neo4J
95-
NEO4J_HOST=http://localhost:7474
95+
NEO4J_HOST=http://127.0.0.1:7474
96+
NEO4J_DATABASE=neo4j
9697
NEO4J_USERNAME=neo4j
9798
NEO4J_PASSWORD=symfonyai

examples/rag/neo4j-similarity-search.php

Whitespace-only changes.

examples/rag/neo4j.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
use Symfony\AI\Agent\Agent;
13+
use Symfony\AI\Agent\Toolbox\AgentProcessor;
14+
use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch;
15+
use Symfony\AI\Agent\Toolbox\Toolbox;
16+
use Symfony\AI\Fixtures\Movies;
17+
use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
18+
use Symfony\AI\Platform\Bridge\OpenAI\GPT;
19+
use Symfony\AI\Platform\Bridge\OpenAI\PlatformFactory;
20+
use Symfony\AI\Platform\Message\Message;
21+
use Symfony\AI\Platform\Message\MessageBag;
22+
use Symfony\AI\Store\Bridge\Neo4j\Store;
23+
use Symfony\AI\Store\Document\Metadata;
24+
use Symfony\AI\Store\Document\TextDocument;
25+
use Symfony\AI\Store\Document\Vectorizer;
26+
use Symfony\AI\Store\Indexer;
27+
use Symfony\Component\HttpClient\HttpClient;
28+
use Symfony\Component\Uid\Uuid;
29+
30+
require_once dirname(__DIR__).'/bootstrap.php';
31+
32+
// initialize the store
33+
$store = new Store(
34+
httpClient: HttpClient::create(),
35+
endpointUrl: env('NEO4J_HOST'),
36+
username: env('NEO4J_USERNAME'),
37+
password: env('NEO4J_PASSWORD'),
38+
databaseName: env('NEO4J_DATABASE'),
39+
vectorIndexName: 'Movies',
40+
nodeName: 'movies',
41+
);
42+
43+
// initialize the table
44+
$store->initialize();
45+
46+
// create embeddings and documents
47+
$documents = [];
48+
foreach (Movies::all() as $i => $movie) {
49+
$documents[] = new TextDocument(
50+
id: Uuid::v4(),
51+
content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'],
52+
metadata: new Metadata($movie),
53+
);
54+
}
55+
56+
// create embeddings for documents
57+
$platform = PlatformFactory::create($_SERVER['OPENAI_API_KEY']);
58+
$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings());
59+
$indexer = new Indexer($vectorizer, $store);
60+
$indexer->index($documents);
61+
62+
$model = new GPT(GPT::GPT_4O_MINI);
63+
64+
$similaritySearch = new SimilaritySearch($platform, $embeddings, $store);
65+
$toolbox = new Toolbox([$similaritySearch], logger: logger());
66+
$processor = new AgentProcessor($toolbox);
67+
$agent = new Agent($platform, $model, [$processor], [$processor]);
68+
69+
$messages = new MessageBag(
70+
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
71+
Message::ofUser('Which movie fits the theme of technology?')
72+
);
73+
$response = $agent->call($messages);
74+
75+
echo $response->getContent().\PHP_EOL;

src/store/tests/Bridge/Neo4j/StoreTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testStoreCanInitialize(): void
8282
$store->initialize();
8383
$store->initialize();
8484

85-
self::assertSame(2, $httpClient->getRequestsCount());
85+
$this->assertSame(2, $httpClient->getRequestsCount());
8686
}
8787

8888
public function testStoreCanAdd(): void
@@ -161,7 +161,7 @@ public function testStoreCanAdd(): void
161161
$store->add(new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3])));
162162
$store->add(new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3])));
163163

164-
self::assertSame(3, $httpClient->getRequestsCount());
164+
$this->assertSame(3, $httpClient->getRequestsCount());
165165
}
166166

167167
public function testStoreCanQuery(): void
@@ -261,7 +261,7 @@ public function testStoreCanQuery(): void
261261

262262
$results = $store->query(new Vector([0.1, 0.2, 0.3]));
263263

264-
self::assertCount(2, $results);
265-
self::assertSame(3, $httpClient->getRequestsCount());
264+
$this->assertCount(2, $results);
265+
$this->assertSame(3, $httpClient->getRequestsCount());
266266
}
267267
}

0 commit comments

Comments
 (0)