1. Go to this page and download the library: Download filippo-toso/pi 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/ */
filippo-toso / pi example snippets
use FilippoToso\Pi\Facades\Pi;
$agent = Pi::create();
$answer = $agent->ask('What is the capital of France?');
echo $answer; // "The capital of France is Paris."
$agent->close();
$agent = Pi::create();
$agent->prompt('Write a haiku about Laravel');
foreach ($agent->stream() as $event) {
if ($text = $event->getTextDelta()) {
echo $text;
}
}
$agent->close();
$agent->compact();
$agent->compact(customInstructions: 'Focus on code changes');
$agent->setAutoCompaction(true);
$agent->setAutoRetry(true);
$agent->abortRetry();
// Steering: deliver all queued messages at once, or one per turn
$agent->setSteeringMode('all'); // or 'one-at-a-time'
$agent->setFollowUpMode('one-at-a-time'); // or 'all'
$commands = $agent->getCommands();
// Returns: [['name' => 'fix-tests', 'source' => 'prompt', ...], ...]
// Invoke a command via prompt
$agent->prompt('/fix-tests');
// Send any raw command
$response = $agent->sendCommand(['type' => 'get_state']);
// Fire-and-forget (no response waiting)
$agent->sendRaw(['type' => 'abort']);
// Read a single event
$event = $agent->readEvent(timeout: 5.0);
// Access the underlying connection
$connection = $agent->getConnection();
$stderrOutput = $connection->readStderr();
$agent->isRunning(); // Check if the pi process is alive
$agent->close(); // Terminate the subprocess (also called on __destruct)
$event->type; // Event type string
$event->isResponse(); // Is this a command response?
$event->isExtensionUiRequest(); // Is this an extension UI request?
$event->getTextDelta(); // Get text delta string (or null)
$event->getDelta(); // Get full assistantMessageEvent array
$event->getMessage(); // Get message from turn_end/message_start/message_end
$event->getMessages(); // Get messages from agent_end
$event->toArray(); // Get raw data array
use FilippoToso\Pi\Exceptions\PiException;
use FilippoToso\Pi\Exceptions\PiCommandException;
use FilippoToso\Pi\Exceptions\PiConnectionException;
try {
$agent = Pi::create();
$answer = $agent->ask('Hello');
} catch (PiConnectionException $e) {
// Process failed to start, pipe broken, timeout
} catch (PiCommandException $e) {
// Command returned success: false
echo $e->command; // The command type that failed
} catch (PiException $e) {
// Base exception for all pi errors
}
bash
php artisan vendor:publish --tag=pi-config
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.