TransformersPHP in PHP: Local Transformer Inference for the Backend
TransformersPHP lets you run pre-trained transformer models like BERT and DistilBERT directly in PHP applications via ONNX Runtime. The library enables local inference without Python, network APIs, or external services. It handles embedding tasks, text classification, and semantic search with predictable response times and full data control.
Key backend advantages: no latency from network calls, offline mode after model loading, data privacy. It supports standard NLP tasks—from sentiment analysis to semantic similarity.
Requirements and Environment Setup
To get started, you'll need PHP 8.1+, Composer, the FFI extension, and ONNX Runtime. JIT is recommended for better performance, along with an increased memory_limit.
Project structure:
/project/
├── app/
│ ├── demo.php
│ ├── semantic-search.php
├── docker/
│ ├── Dockerfile
├── docker-compose.yml
├── composer.json
Installation via Docker simplifies deployment. File composer.json:
{
"type": "project",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"codewithkyrian/transformers": "~0.6.2"
},
"config": {
"allow-plugins": {
"codewithkyrian/platform-package-installer": true
}
}
}
Commands to run:
docker compose build --pulldocker compose up -ddocker compose exec app composer install
Dockerfile sets up FFI, ONNX Runtime 1.17.1, and PHP extensions (zip, pdo_mysql, bcmath, ffi, and others).
First Example: Sentiment Analysis
Basic demo demonstrates the pipeline API. Create app/demo.php:
require_once __DIR__ . '/../vendor/autoload.php';
use function Codewithkyrian\Transformers\Pipelines\pipeline;
// Konveyer for sentiment-analysis
$classifier = pipeline('sentiment-analysis');
$out = $classifier(['I love transformers!']);
echo 'I love transformers!\n';
echo print_r($out, true);
$out = $classifier(['I hate transformers!']);
echo 'I hate transformers!\n';
echo print_r($out, true);
Run: docker compose exec app php app/demo.php
Output:
I love transformers!
Array (
[label] => POSITIVE
[score] => 0.99978870153427
)
I hate transformers!
Array (
[label] => NEGATIVE
[score] => 0.99863630533218
)
Pipeline abstracts model loading, tokenization, and inference. First call caches the model.
Semantic Search for Events
Real-world scenario: semantic search in an event feed. Event texts don't contain exact query phrasing (synonyms, morphology).
Example data:
- New restrictions introduced regarding technology corporations
- Countries in the region ramping up investments in satellite programs
- Escalation of conflict on political grounds
Query: “space race among countries in the region”.
Solution via embeddings: vector representations of texts, cosine similarity for ranking.
Run example: docker compose exec app php app/semantic-search.php
Code uses pipeline('feature-extraction') for embeddings and computes semantic similarity without keywords.
Architectural Role in the PHP Backend
TransformersPHP integrates as a service in a monolith or microservice:
- Embedding generation for search
- Log/request classification
- Duplicate detection
- Local NER without API
// Example integration in servis
class SemanticSearchService {
private $embedder;
public function __construct() {
$this->embedder = pipeline('feature-extraction');
}
public function findSimilar(string $query, array $documents): array {
// Embeddingi + cosine similarity
}
}
Advantages over APIs:
- Stable latency (<100ms on CPU)
- No quotas/costs
- Offline after init
- Full model control
Performance Optimization
- JIT in PHP 8.2+ speeds up inference by 20-30%
- Model caching in Redis
- Request batching
- ONNX optimizations (quantization)
Tested on DistilBERT: ~50ms for a 512-token embedding (i7, 16GB).
For production: memory monitoring (models 100-500MB), graceful fallback to API.
Key Takeaways
- TransformersPHP—for inference, not model training.
- Local inference solves 80% of backend NLP tasks without data science.
- Integrates as a Composer package, no Python infrastructure.
- Suitable for privacy-sensitive environments (GDPR).
- Scales via batching and caching.
— Editorial Team
No comments yet.