Skip to content
Open
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
3 changes: 3 additions & 0 deletions examples/.env
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,8 @@ REDIS_HOST=localhost
# Manticore Search (store)
MANTICORESEARCH_HOST=http://127.0.0.1:9308

# Elasticsearch (store)
ELASTICSEARCH_ENDPOINT=http://127.0.0.1:9201

# OpenSearch (store)
OPENSEARCH_ENDPOINT=http://127.0.0.1:9200
6 changes: 6 additions & 0 deletions examples/commands/stores.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use MongoDB\Client as MongoDbClient;
use Symfony\AI\Store\Bridge\Cache\Store as CacheStore;
use Symfony\AI\Store\Bridge\ClickHouse\Store as ClickHouseStore;
use Symfony\AI\Store\Bridge\Elasticsearch\Store as ElasticsearchStore;
use Symfony\AI\Store\Bridge\ManticoreSearch\Store as ManticoreSearchStore;
use Symfony\AI\Store\Bridge\MariaDb\Store as MariaDbStore;
use Symfony\AI\Store\Bridge\Meilisearch\Store as MeilisearchStore;
Expand Down Expand Up @@ -47,6 +48,11 @@
env('CLICKHOUSE_DATABASE'),
env('CLICKHOUSE_TABLE'),
),
'elasticsearch' => static fn (): ElasticsearchStore => new ElasticsearchStore(
http_client(),
env('ELASTICSEARCH_ENDPOINT'),
'symfony',
),
'manticoresearch' => static fn (): ManticoreSearchStore => new ManticoreSearchStore(
http_client(),
env('MANTICORESEARCH_HOST'),
Expand Down
22 changes: 22 additions & 0 deletions examples/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ services:
- '7474:7474'
- '7687:7687'

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:9.2.2
environment:
discovery.type: 'single-node'
xpack.security.enabled: false
ES_JAVA_OPTS: '-Xms512m -Xmx512m'
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65536
hard: 65536
healthcheck:
test: [ 'CMD', 'curl', '-f', 'http://127.0.0.1:9200/_cluster/health' ]
interval: 30s
start_period: 120s
timeout: 20s
retries: 3
ports:
- '9201:9200'

opensearch:
image: opensearchproject/opensearch
environment:
Expand Down
1 change: 1 addition & 0 deletions examples/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"symfony/ai-click-house-store": "@dev",
"symfony/ai-clock-tool": "@dev",
"symfony/ai-cloudflare-store": "@dev",
"symfony/ai-elasticsearch-store": "@dev",
"symfony/ai-manticore-search-store": "@dev",
"symfony/ai-maria-db-store": "@dev",
"symfony/ai-meilisearch-store": "@dev",
Expand Down
67 changes: 67 additions & 0 deletions examples/rag/elasticsearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\Bridge\SimilaritySearch\SimilaritySearch;
use Symfony\AI\Agent\Toolbox\AgentProcessor;
use Symfony\AI\Agent\Toolbox\Toolbox;
use Symfony\AI\Fixtures\Movies;
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Store\Bridge\Elasticsearch\Store;
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
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\Uid\Uuid;

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

// initialize the store
$store = new Store(
httpClient: http_client(),
endpoint: env('ELASTICSEARCH_ENDPOINT'),
indexName: 'movies',
);

// 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),
);
}

// initialize the index
$store->setup();

// create embeddings for documents
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
$vectorizer = new Vectorizer($platform, 'text-embedding-3-small', logger());
$indexer = new Indexer(new InMemoryLoader($documents), $vectorizer, $store, logger: logger());
$indexer->index($documents);

$similaritySearch = new SimilaritySearch($vectorizer, $store);
$toolbox = new Toolbox([$similaritySearch], logger: logger());
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, 'gpt-4o-mini', [$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?')
);
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;
1 change: 1 addition & 0 deletions splitsh.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"ai-click-house-store": "src/store/src/Bridge/ClickHouse",
"ai-cloudflare-store": "src/store/src/Bridge/Cloudflare",
"ai-chroma-db-store": "src/store/src/Bridge/ChromaDb",
"ai-elasticsearch-store": "src/store/src/Bridge/Elasticsearch",
"ai-manticore-search-store": "src/store/src/Bridge/ManticoreSearch",
"ai-maria-db-store": "src/store/src/Bridge/MariaDb",
"ai-meilisearch-store": "src/store/src/Bridge/Meilisearch",
Expand Down
21 changes: 21 additions & 0 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,27 @@
->end()
->end()
->end()
->arrayNode('elasticsearch')
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->stringNode('endpoint')->cannotBeEmpty()->end()
->stringNode('index_name')->end()
->stringNode('vectors_field')
->defaultValue('_vectors')
->end()
->integerNode('dimensions')
->defaultValue(1536)
->end()
->stringNode('similarity')
->defaultValue('cosine')
->end()
->stringNode('http_client')
->defaultValue('http_client')
->end()
->end()
->end()
->end()
->arrayNode('opensearch')
->useAttributeAsKey('name')
->arrayPrototype()
Expand Down
28 changes: 28 additions & 0 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
use Symfony\AI\Store\Bridge\ChromaDb\Store as ChromaDbStore;
use Symfony\AI\Store\Bridge\ClickHouse\Store as ClickHouseStore;
use Symfony\AI\Store\Bridge\Cloudflare\Store as CloudflareStore;
use Symfony\AI\Store\Bridge\Elasticsearch\Store as ElasticsearchStore;
use Symfony\AI\Store\Bridge\ManticoreSearch\Store as ManticoreSearchStore;
use Symfony\AI\Store\Bridge\MariaDb\Store as MariaDbStore;
use Symfony\AI\Store\Bridge\Meilisearch\Store as MeilisearchStore;
Expand Down Expand Up @@ -1396,6 +1397,33 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
}
}

if ('elasticsearch' === $type) {
if (!ContainerBuilder::willBeAvailable('symfony/ai-elasticsearch-store', ElasticsearchStore::class, ['symfony/ai-bundle'])) {
throw new RuntimeException('Elasticsearch store configuration requires "symfony/ai-elasticsearch-store" package. Try running "composer require symfony/ai-elasticsearch-store".');
}

foreach ($stores as $name => $store) {
$definition = new Definition(ElasticsearchStore::class);
$definition
->setLazy(true)
->setArguments([
new Reference($store['http_client']),
$store['endpoint'],
$store['index_name'] ?? $name,
$store['vectors_field'],
$store['dimensions'],
$store['similarity'],
])
->addTag('proxy', ['interface' => StoreInterface::class])
->addTag('proxy', ['interface' => ManagedStoreInterface::class])
->addTag('ai.store');

$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $type.'_'.$name);
}
}

if ('opensearch' === $type) {
if (!ContainerBuilder::willBeAvailable('symfony/ai-open-search-store', OpenSearchStore::class, ['symfony/ai-bundle'])) {
throw new RuntimeException('OpenSearch store configuration requires "symfony/ai-open-search-store" package. Try running "composer require symfony/ai-open-search-store".');
Expand Down
1 change: 1 addition & 0 deletions src/store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CHANGELOG
- ChromaDB
- ClickHouse
- Cloudflare
- Elasticsearch
- Manticore Search
- MariaDB
- Meilisearch
Expand Down
1 change: 1 addition & 0 deletions src/store/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"chromadb",
"clickhouse",
"cloudflare",
"elasticsearch",
"mariadb",
"meilisearch",
"milvus",
Expand Down
3 changes: 3 additions & 0 deletions src/store/src/Bridge/Elasticsearch/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.git* export-ignore
4 changes: 4 additions & 0 deletions src/store/src/Bridge/Elasticsearch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
composer.lock
phpunit.xml
.phpunit.result.cache
19 changes: 19 additions & 0 deletions src/store/src/Bridge/Elasticsearch/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2025-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading
Loading