AWS S3 Vectors Bridge
The AWS S3 Vectors bridge provides vector storage and similarity search integration using AWS S3 Vectors.
Requirements
- An AWS account with access to S3 Vectors
- The AsyncAws S3Vectors client
Basic Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
use AsyncAws\S3Vectors\S3VectorsClient;
use Symfony\AI\Store\Bridge\S3Vectors\Store;
$client = new S3VectorsClient([
'region' => 'us-east-1',
]);
$store = new Store(
client: $client,
vectorBucketName: 'my-vector-bucket',
indexName: 'my-index',
filter: [], // Optional: default filter for queries
topK: 3, // Optional: default number of results
dimensions: 1536, // Optional: dimensions of the index, default: 1536
metric: \AsyncAws\S3Vectors\Enum\DistanceMetric::COSINE, // Optional
dataType: \AsyncAws\S3Vectors\Enum\DataType::FLOAT_32, // Optional
kmsKeyId: 'your-kms-key-id', // Optional: encrypts the bucket and the index
tags: ['env' => 'production'], // Optional
);
Setup the Vector Store
Before using the store, create the vector bucket and the index:
1
$store->setup();
When using the AI Bundle, run ai:store:setup to create the vector bucket and index:
1 2 3 4 5 6 7 8 9 10 11
# config/packages/ai.yaml
ai:
store:
s3vectors:
my_store:
vector_bucket_name: 'my-vector-bucket'
index_name: 'my-index'
configuration:
region: 'us-east-1'
dimensions: 1536 # default: 1536
metric: cosine # cosine (default) or euclidean
Add Documents
1 2 3 4 5 6 7 8 9 10 11
use Symfony\AI\Platform\Vector\Vector;
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\VectorDocument;
use Symfony\Component\Uid\Uuid;
$document = new VectorDocument(
id: Uuid::v4()->toRfc4122(),
vector: new Vector([0.1, 0.2, 0.3, ...]),
metadata: new Metadata(['title' => 'My Document'])
);
$store->add($document);
Query Similar Vectors
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use Symfony\AI\Platform\Vector\Vector;
use Symfony\AI\Store\Query\VectorQuery;
$results = $store->query(
query: new VectorQuery(new Vector([0.1, 0.2, 0.3, ...])),
options: [
'topK' => 5,
'filter' => ['category' => 'documentation'],
]
);
foreach ($results as $result) {
echo $result->getMetadata()['title'] . ' (score: ' . $result->getScore() . ')' . PHP_EOL;
}
Remove Documents
1
$store->remove(['id1', 'id2']);
Drop the Store
1 2
// Drop the index and bucket
$store->drop();
Features
- Full CRUD operations for vector documents
- Similarity search with configurable distance metrics (cosine, euclidean)
- Metadata filtering support
- KMS encryption support
- Tag management
- Batch operations
Distance Metrics
The bridge supports the following distance metrics:
COSINE- Cosine distance (default)EUCLIDEAN- Euclidean distance
Data Types
The bridge supports the following data types for vectors:
FLOAT_32- 32-bit floating point (default)
This work, including the code samples, is licensed under a
Creative Commons BY-SA 3.0 license.