symfony/ai-s3vectors-store
Symfony AI Store integration for AWS S3 Vectors. Store embeddings in S3 vector buckets and run similarity queries via the S3 Vectors API (PutVectors/QueryVectors). Useful for retrieval and semantic search using managed AWS infrastructure.
Install the Package:
composer require symfony/ai-s3vectors-store
Enable S3 Vectors in Your Bucket: Ensure your S3 bucket has the S3 Vectors feature enabled via AWS CLI or Console:
aws s3api put-bucket-vector --bucket your-bucket-name --region us-east-1
Configure Symfony AI:
Add the store configuration to your config/packages/ai.yaml:
framework:
ai:
stores:
s3_vectors:
type: 'S3VectorsStore'
bucket: 'your-bucket-name'
region: 'us-east-1'
# Optional: Custom AWS credentials/config
aws:
credentials:
key: '%env(AWS_ACCESS_KEY_ID)%'
secret: '%env(AWS_SECRET_ACCESS_KEY)%'
region: 'us-east-1'
First Use Case: Inject and use the store in a service or controller:
use Symfony\AI\Store\StoreInterface;
class VectorService {
public function __construct(private StoreInterface $s3VectorsStore) {}
public function storeVector(array $vector): void {
$this->s3VectorsStore->putVectors([$vector]);
}
public function queryVectors(array $queryVector, int $limit = 10): array {
return $this->s3VectorsStore->queryVectors($queryVector, $limit);
}
}
Register the Service:
Bind the store to Symfony’s DI container in services.yaml:
services:
App\Service\VectorService:
arguments:
$s3VectorsStore: '@ai.store.s3_vectors'
Vector Ingestion: Batch vectors for efficient uploads to S3:
$batch = [];
foreach ($rawVectors as $vector) {
$batch[] = [
'id' => $vector['id'],
'vector' => $vector['embedding'],
'metadata' => $vector['metadata'] ?? null,
];
if (count($batch) >= 100) { // Optimal batch size
$store->putVectors($batch);
$batch = [];
}
}
if (!empty($batch)) {
$store->putVectors($batch);
}
Querying Vectors:
Use queryVectors for similarity search with optional filters:
$results = $store->queryVectors(
$queryVector,
limit: 5,
filter: ['category' => 'electronics'] // If using metadata
);
Hybrid Workflows: Combine with other Symfony AI components (e.g., embeddings):
$embedding = $ai->embedding->create($text);
$similarItems = $store->queryVectors($embedding->getEmbedding());
RAG (Retrieval-Augmented Generation):
$context = $store->queryVectors($queryEmbedding, 3);
$response = $ai->llm->generate($prompt . "\nContext: " . $context);
Recommendation Systems:
$recommendations = $store->queryVectors($userProfileEmbedding, 10);
Semantic Search:
$searchResults = $store->queryVectors($searchQueryEmbedding);
Error Handling: Wrap S3 operations in try-catch blocks to handle AWS-specific exceptions:
try {
$store->putVectors($vectors);
} catch (AwsException $e) {
// Log and retry or fallback
$this->logger->error('S3 Vectors error: ' . $e->getMessage());
}
Metadata Management:
Use the metadata field in vectors for filtering (if supported by your S3 Vectors setup):
$vector = [
'id' => 'doc123',
'vector' => $embedding,
'metadata' => ['category' => 'tech', 'source' => 'blog'],
];
Testing: Use a dedicated test bucket and mock AWS credentials in tests:
$store = new S3VectorsStore('test-bucket', 'us-west-2', [
'credentials' => [
'key' => 'test-key',
'secret' => 'test-secret',
],
'region' => 'us-west-2',
'version' => 'latest',
]);
Performance Optimization:
PutVectors.Monitoring: Track S3 Vectors metrics via AWS CloudWatch:
PutVectors latency and success rates.QueryVectors throughput and errors.S3 Vectors Feature Not Enabled:
InvalidArgumentException or AWS API errors.Vector Size Limits:
AWS Credentials:
AccessDenied or UnauthorizedOperation.s3:PutBucketVector, s3:GetBucketVector, and s3:PutObject.Region Mismatch:
InvalidBucketName errors.Metadata Limitations:
Cold Starts:
Enable AWS SDK Debugging:
Add this to your config/packages/dev/ai.yaml:
framework:
ai:
stores:
s3_vectors:
aws:
debug: true
Logs will appear in var/log/dev.log.
Check S3 Vectors API Calls:
Use AWS CloudTrail to audit PutVectors and QueryVectors calls.
Validate Vector Format: Ensure vectors are in the correct format (e.g., float arrays):
$validVector = [0.1, 0.5, ..., 0.9]; // Must be numeric and consistent length
Default Credentials: The package uses Symfony’s AWS messenger configuration by default. Override if needed:
framework:
ai:
stores:
s3_vectors:
aws:
credentials:
key: '%env(AWS_ACCESS_KEY_ID)%'
secret: '%env(AWS_SECRET_ACCESS_KEY)%'
Bucket Ownership: Ensure your IAM user/role owns the bucket or has full S3 permissions.
CORS Issues: If accessing S3 from a browser, configure CORS on the bucket:
aws s3api put-bucket-cors --bucket your-bucket-name --cors-configuration '{
"CORSRules": [
{
"AllowedHeaders": ["*"],
"AllowedMethods": ["GET", "PUT", "POST", "DELETE"],
"AllowedOrigins": ["*"],
"ExposeHeaders": []
}
]
}'
Custom Vector Serialization: Extend the store to handle custom vector formats:
use Symfony\AI\Store\S3VectorsStore;
class CustomS3VectorsStore extends S3VectorsStore {
protected function serializeVector(array $vector): string {
// Custom logic (e.g., base64 encoding)
return base64_encode(serialize($vector));
}
protected function deserializeVector(string $data): array {
return unserialize(base64_decode($data));
}
}
Query Augmentation:
Add pre/post-processing to queryVectors:
$store->queryVectors($query
How can I help you explore Laravel packages today?