1. Go to this page and download the library: Download ferry-ai/php-inference library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ferry-ai / php-inference example snippets
use FerryAI\AI;
AI::config([
'backend' => 'onnx',
'backends' => ['embedding' => ['model_path' => '/models/all-MiniLM-L6-v2-onnx']],
]);
// embed → 384d vector, then store and search
$vec = AI::embed('Hello world');
$store = AI::vector('docs');
$store->add('doc1', $vec->vector, ['title' => 'Getting Started']);
$hits = $store->search(AI::embed('semantic query')->vector, k: 5);
// similarity between any two texts
echo AI::similarity('cat', 'kitten'); // 0.79
// compose a processing pipeline
$results = AI::pipeline()
->pipe(new TransformStage(strtoupper(...)))
->pipe(new FilterStage(fn($x) => strlen($x) > 3))
->run(['hi', 'hello', 'hey']);
AI::config(['backend' => 'llama', 'backends' => ['llama' => ['model_path' => '/models/qwen.gguf']]]);
echo AI::chat('Explain PHP FFI in one sentence.'); // full reply
foreach (AI::stream('Write a haiku about ferries.') as $token) { echo $token; }
// structured output via JSON Schema → GBNF grammar
$json = AI::chat('List 3 famous bridges with year and city.', [
'grammar' => [
'type' => 'object',
'properties' => ['bridges' => [
'type' => 'array',
'items' => ['type' => 'object', 'properties' => [
'name' => ['type' => 'string'],
'year' => ['type' => 'integer'],
'city' => ['type' => 'string'],
]],
]],
],
]);
// HTTP streaming response (PSR-7 SSE/NDJSON) for web apps
return AI::streamResponse([['role' => 'user', 'content' => $prompt]]);