Skip to content
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
4 changes: 2 additions & 2 deletions examples/.env
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ MAPBOX_ACCESS_TOKEN=
MONGODB_URI=mongodb://symfony:symfony@127.0.0.1:27017

# For using Pinecone (store)
PINECONE_API_KEY=
PINECONE_HOST=
PINECONE_API_KEY=pclocal
PINECONE_HOST=http://127.0.0.1:5080

# For using Postgres (store)
POSTGRES_URI=pdo-pgsql://postgres:postgres@127.0.1:5432/my_database
Expand Down
6 changes: 6 additions & 0 deletions examples/commands/stores.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
use MongoDB\Client as MongoDbClient;
use Probots\Pinecone\Client as PineconeClient;
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;
Expand All @@ -24,6 +25,7 @@
use Symfony\AI\Store\Bridge\MongoDb\Store as MongoDbStore;
use Symfony\AI\Store\Bridge\Neo4j\Store as Neo4jStore;
use Symfony\AI\Store\Bridge\OpenSearch\Store as OpenSearchStore;
use Symfony\AI\Store\Bridge\Pinecone\Store as PineconeStore;
use Symfony\AI\Store\Bridge\Postgres\Store as PostgresStore;
use Symfony\AI\Store\Bridge\Qdrant\Store as QdrantStore;
use Symfony\AI\Store\Bridge\Redis\Store as RedisStore;
Expand Down Expand Up @@ -99,6 +101,10 @@
// env('OPENSEARCH_ENDPOINT'),
// 'symfony',
// ),
// 'pinecone' => static fn (): PineconeStore => new PineconeStore(
// new PineconeClient(env('PINECONE_API_KEY'), env('PINECONE_HOST')),
// 'symfony',
// ),
'postgres' => static fn (): PostgresStore => PostgresStore::fromDbal(
DriverManager::getConnection((new DsnParser())->parse(env('POSTGRES_URI'))),
'my_table',
Expand Down
9 changes: 9 additions & 0 deletions examples/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ services:
ports:
- '9201:9200'

pinecone:
image: ghcr.io/pinecone-io/pinecone-local:latest
platform: linux/amd64
environment:
PORT: 5080
PINECONE_HOST: localhost
ports:
- '5080-5090:5080-5090'

opensearch:
image: opensearchproject/opensearch
environment:
Expand Down
2 changes: 1 addition & 1 deletion examples/rag/pinecone.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
require_once dirname(__DIR__).'/bootstrap.php';

// initialize the store
$store = new Store(Pinecone::client(env('PINECONE_API_KEY'), env('PINECONE_HOST')));
$store = new Store(Pinecone::client(env('PINECONE_API_KEY'), env('PINECONE_HOST')), 'symfony');

// create embeddings and documents
$documents = [];
Expand Down
1 change: 1 addition & 0 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@
->cannotBeEmpty()
->defaultValue(PineconeClient::class)
->end()
->stringNode('index_name')->isRequired()->end()
->stringNode('namespace')->end()
->arrayNode('filter')
->scalarPrototype()
Expand Down
4 changes: 3 additions & 1 deletion src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1519,19 +1519,21 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
foreach ($stores as $name => $store) {
$arguments = [
new Reference($store['client']),
$store['index_name'],
$store['namespace'] ?? $name,
$store['filter'],
];

if (\array_key_exists('top_k', $store)) {
$arguments[3] = $store['top_k'];
$arguments[4] = $store['top_k'];
}

$definition = new Definition(PineconeStore::class);
$definition
->setLazy(true)
->setArguments($arguments)
->addTag('proxy', ['interface' => StoreInterface::class])
->addTag('proxy', ['interface' => ManagedStoreInterface::class])
->addTag('ai.store');

$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
Expand Down
99 changes: 15 additions & 84 deletions src/ai-bundle/tests/DependencyInjection/AiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2370,48 +2370,13 @@ public function testOpenSearchStoreWithCustomHttpClientCanBeConfigured()
}

public function testPineconeStoreCanBeConfigured()
{
$container = $this->buildContainer([
'ai' => [
'store' => [
'pinecone' => [
'my_pinecone_store' => [],
],
],
],
]);

$this->assertTrue($container->hasDefinition('ai.store.pinecone.my_pinecone_store'));

$definition = $container->getDefinition('ai.store.pinecone.my_pinecone_store');
$this->assertSame(PineconeStore::class, $definition->getClass());

$this->assertTrue($definition->isLazy());
$this->assertCount(3, $definition->getArguments());
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
$this->assertSame(PineconeClient::class, (string) $definition->getArgument(0));
$this->assertSame('my_pinecone_store', $definition->getArgument(1));
$this->assertSame([], $definition->getArgument(2));

$this->assertTrue($definition->hasTag('proxy'));
$this->assertSame([['interface' => StoreInterface::class]], $definition->getTag('proxy'));
$this->assertTrue($definition->hasTag('ai.store'));

$this->assertTrue($container->hasAlias('.Symfony\AI\Store\StoreInterface $my_pinecone_store'));
$this->assertTrue($container->hasAlias('Symfony\AI\Store\StoreInterface $myPineconeStore'));
$this->assertTrue($container->hasAlias('.Symfony\AI\Store\StoreInterface $pinecone_my_pinecone_store'));
$this->assertTrue($container->hasAlias('Symfony\AI\Store\StoreInterface $pineconeMyPineconeStore'));
$this->assertTrue($container->hasAlias('Symfony\AI\Store\StoreInterface'));
}

public function testPineconeStoreWithCustomNamespaceCanBeConfigured()
{
$container = $this->buildContainer([
'ai' => [
'store' => [
'pinecone' => [
'my_pinecone_store' => [
'namespace' => 'my_namespace',
'index_name' => 'my_index',
],
],
],
Expand All @@ -2424,14 +2389,15 @@ public function testPineconeStoreWithCustomNamespaceCanBeConfigured()
$this->assertSame(PineconeStore::class, $definition->getClass());

$this->assertTrue($definition->isLazy());
$this->assertCount(3, $definition->getArguments());
$this->assertCount(4, $definition->getArguments());
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
$this->assertSame(PineconeClient::class, (string) $definition->getArgument(0));
$this->assertSame('my_namespace', $definition->getArgument(1));
$this->assertSame([], $definition->getArgument(2));
$this->assertSame('my_index', $definition->getArgument(1));
$this->assertSame('my_pinecone_store', $definition->getArgument(2));
$this->assertSame([], $definition->getArgument(3));

$this->assertTrue($definition->hasTag('proxy'));
$this->assertSame([['interface' => StoreInterface::class]], $definition->getTag('proxy'));
$this->assertSame([['interface' => StoreInterface::class], ['interface' => ManagedStoreInterface::class]], $definition->getTag('proxy'));
$this->assertTrue($definition->hasTag('ai.store'));

$this->assertTrue($container->hasAlias('.Symfony\AI\Store\StoreInterface $my_pinecone_store'));
Expand All @@ -2441,14 +2407,14 @@ public function testPineconeStoreWithCustomNamespaceCanBeConfigured()
$this->assertTrue($container->hasAlias('Symfony\AI\Store\StoreInterface'));
}

public function testPineconeStoreWithCustomClientCanBeConfigured()
public function testPineconeStoreWithCustomIndexNameCanBeConfigured()
{
$container = $this->buildContainer([
'ai' => [
'store' => [
'pinecone' => [
'my_pinecone_store' => [
'client' => 'foo',
'index_name' => 'custom_index',
'namespace' => 'my_namespace',
],
],
Expand All @@ -2461,54 +2427,16 @@ public function testPineconeStoreWithCustomClientCanBeConfigured()
$definition = $container->getDefinition('ai.store.pinecone.my_pinecone_store');
$this->assertSame(PineconeStore::class, $definition->getClass());

$this->assertTrue($definition->isLazy());
$this->assertCount(3, $definition->getArguments());
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
$this->assertSame('foo', (string) $definition->getArgument(0));
$this->assertSame('my_namespace', $definition->getArgument(1));
$this->assertSame([], $definition->getArgument(2));

$this->assertTrue($definition->hasTag('proxy'));
$this->assertSame([['interface' => StoreInterface::class]], $definition->getTag('proxy'));
$this->assertTrue($definition->hasTag('ai.store'));

$this->assertTrue($container->hasAlias('.Symfony\AI\Store\StoreInterface $my_pinecone_store'));
$this->assertTrue($container->hasAlias('Symfony\AI\Store\StoreInterface $myPineconeStore'));
$this->assertTrue($container->hasAlias('.Symfony\AI\Store\StoreInterface $pinecone_my_pinecone_store'));
$this->assertTrue($container->hasAlias('Symfony\AI\Store\StoreInterface $pineconeMyPineconeStore'));
$this->assertTrue($container->hasAlias('Symfony\AI\Store\StoreInterface'));
}

public function testPineconeStoreWithTopKCanBeConfigured()
{
$container = $this->buildContainer([
'ai' => [
'store' => [
'pinecone' => [
'my_pinecone_store' => [
'namespace' => 'my_namespace',
'top_k' => 100,
],
],
],
],
]);

$this->assertTrue($container->hasDefinition('ai.store.pinecone.my_pinecone_store'));

$definition = $container->getDefinition('ai.store.pinecone.my_pinecone_store');
$this->assertSame(PineconeStore::class, $definition->getClass());

$this->assertTrue($definition->isLazy());
$this->assertCount(4, $definition->getArguments());
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
$this->assertSame(PineconeClient::class, (string) $definition->getArgument(0));
$this->assertSame('my_namespace', $definition->getArgument(1));
$this->assertSame([], $definition->getArgument(2));
$this->assertSame(100, $definition->getArgument(3));
$this->assertSame('custom_index', $definition->getArgument(1));
$this->assertSame('my_namespace', $definition->getArgument(2));
$this->assertSame([], $definition->getArgument(3));

$this->assertTrue($definition->hasTag('proxy'));
$this->assertSame([['interface' => StoreInterface::class]], $definition->getTag('proxy'));
$this->assertSame([['interface' => StoreInterface::class], ['interface' => ManagedStoreInterface::class]], $definition->getTag('proxy'));
$this->assertTrue($definition->hasTag('ai.store'));

$this->assertTrue($container->hasAlias('.Symfony\AI\Store\StoreInterface $my_pinecone_store'));
Expand Down Expand Up @@ -7317,15 +7245,18 @@ private function getFullConfig(): array
],
'pinecone' => [
'my_pinecone_store' => [
'index_name' => 'my_index',
'namespace' => 'my_namespace',
'filter' => ['category' => 'books'],
'top_k' => 10,
],
'my_pinecone_store_with_filter' => [
'index_name' => 'my_index',
'namespace' => 'my_namespace',
'filter' => ['category' => 'books'],
],
'my_pinecone_store_with_top_k' => [
'index_name' => 'my_index',
'namespace' => 'my_namespace',
'filter' => ['category' => 'books'],
'top_k' => 10,
Expand Down
38 changes: 37 additions & 1 deletion src/store/src/Bridge/Pinecone/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,53 @@
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\ManagedStoreInterface;
use Symfony\AI\Store\StoreInterface;
use Symfony\Component\Uid\Uuid;

/**
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
final class Store implements StoreInterface
final class Store implements ManagedStoreInterface, StoreInterface
{
/**
* @param array<string, mixed> $filter
*/
public function __construct(
private readonly Client $pinecone,
private readonly string $indexName,
private readonly ?string $namespace = null,
private readonly array $filter = [],
private readonly int $topK = 3,
) {
}

/**
* @param array{
* dimension?: int,
* metric?: string,
* cloud?: string,
* region?: string,
* } $options
*/
public function setup(array $options = []): void
{
if (false === isset($options['dimension'])) {
throw new InvalidArgumentException('The "dimension" option is required.');
}

$this->pinecone
->control()
->index($this->indexName)
->createServerless(
$options['dimension'],
$options['metric'] ?? null,
$options['cloud'] ?? null,
$options['region'] ?? null,
);
}

public function add(VectorDocument ...$documents): void
{
$vectors = [];
Expand Down Expand Up @@ -73,6 +101,14 @@ public function query(Vector $vector, array $options = []): iterable
}
}

public function drop(array $options = []): void
{
$this->pinecone
->control()
->index($this->indexName)
->delete();
}

private function getVectors(): VectorResource
{
return $this->pinecone->data()->vectors();
Expand Down
Loading