composer require syncfly/python-in-phpYou need to answer "yes" when prompted to activate the plugin.
You can find the documentation here: (coming soon)
The Python-in-PHP Package Manager is a built-in package manager based on uv that allows you to install and manage Python packages.
composer pip install <package-name>
composer pip uninstall <package-name>
composer pip install --upgrade <package-name>
Look at an example of using transformers and torch in PHP for running an AI model:
<?php
use py\transformers;
use py\torch;
$model_name = 'google/gemma-3-4b-it';
$tokenizer = transformers\AutoTokenizer::from_pretrained($model_name);
$model = transformers\AutoModelForCausalLM::from_pretrained(
$model_name,
torch_dtype: torch::$bfloat16,
device_map: "auto"
);
$messages = [
['role' => 'user', 'content' => 'Why PHP is great?']
];
$input_ids = $tokenizer->apply_chat_template(
$messages,
return_tensors: 'pt',
add_generation_prompt: true
);
$outputs = $model->generate(
$input_ids,
max_new_tokens: 2048
);
$result = $tokenizer->decode($outputs[0], skip_special_tokens: true);Or simplier with transformers pipeline:
<?php
use py\transformers;
$pipe = transformers\pipeline(
'text-generation',
model: 'google/gemma-3-4b-it',
torch_dtype: torch::$bfloat16,
device_map: 'auto'
);
$messages = [
['role' => 'user', 'content' => 'Why PHP is great?']
];
$output = $pipe($messages, max_new_tokens: 2048);
$generated = $output[0]['generated_text'];
$result = end($generated)['content'];This project is distributed under a source-available license.
- Using the package in your projects, including commercial ones ✅
- Making changes and submitting pull requests to this repository
- Creating public forks or distributing the project under your own name
- Uploading the code (modified or original) anywhere else
✅ All contributions are accepted through pull requests to the official repository
- Attribution notice is required for software with publicly available source code
See LICENSE.md for full details.
