1. Go to this page and download the library: Download aisdk/core 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/ */
aisdk / core example snippets
use AiSdk\Generate;
use AiSdk\OpenAI;
$result = Generate::text()
->model(OpenAI::model('gpt-4o'))
->instructions('Write short, clear answers.')
->prompt('Explain closures in PHP.')
->run();
echo $result->text;
Generate::model(OpenAI::model('gpt-4o'));
$result = Generate::text('Explain closures in PHP.')->run();
use AiSdk\OpenAI\OpenAIOptions;
use AiSdk\OpenAI\OpenAIProvider;
use AiSdk\Support\SdkFactory;
$runtime = (new SdkFactory)
->withConnectTimeout(10)
->withTimeout(120)
->make();
$provider = new OpenAIProvider(OpenAIOptions::fromArray([
'apiKey' => $_ENV['OPENAI_API_KEY'],
'sdk' => $runtime,
]));
$sdk = $runtime->withDefaultTextModel($provider->model('gpt-4o'));
$result = $sdk->text('Explain closures in PHP.')->run();
use AiSdk\Generate;
use AiSdk\OpenAI;
use AiSdk\Schema;
$result = Generate::text()
->model(OpenAI::model('gpt-4o'))
->prompt('Extract the city and country from: Lahore, Pakistan.')
->output(Schema::object(
name: 'address',
description: 'The city and country extracted from the prompt.',
properties: [
Schema::string(name: 'city')->
use AiSdk\Generate;
use AiSdk\OpenAI;
use AiSdk\Schema;
use AiSdk\Tool;
$weather = Tool::make('weather', 'Get current weather')
->input(Schema::string(name: 'city')->)
->tool($weather)
->run();
final class WeatherTool extends Tool
{
public function __construct()
{
$this->as('weather')
->for('Get current weather')
->input(Schema::string(name: 'city')->
use AiSdk\Generate;
use AiSdk\OpenAI;
$stream = Generate::text('Tell me a story.')
->model(OpenAI::model('gpt-4o'))
->stream();
foreach ($stream->chunks() as $chunk) {
echo $chunk;
}
$result = $stream->run();
use AiSdk\Generate;
use AiSdk\OpenAI;
$result = Generate::image()
->model(OpenAI::model('gpt-image-1'))
->prompt('A clean app icon for a PHP AI SDK')
->size('1024x1024')
->run();
$result->output->save(__DIR__.'/icon.png');
$result = Generate::image('Minimal line art of Lahore Fort')
->model(OpenAI::model('gpt-image-1'))
->count(2)
->aspectRatio('1:1')
->run();
foreach ($result->images as $index => $image) {
$image->save(__DIR__."/image-{$index}.png");
}
use AiSdk\Generate;
use AiSdk\OpenAI;
$result = Generate::speech()
->model(OpenAI::model('gpt-4o-mini-tts'))
->input('Welcome to the PHP AI SDK.')
->voice('coral')
->format('mp3')
->run();
$result->output->save(__DIR__.'/welcome.mp3');
$result = Generate::speech('Read this in a warm tone.')
->model(OpenAI::model('gpt-4o-mini-tts'))
->providerOptions('openai', [
'instructions' => 'Speak clearly and warmly.',
])
->run();
use AiSdk\Content;
use AiSdk\Generate;
use AiSdk\OpenAI;
$result = Generate::transcription(Content::audio(__DIR__.'/meeting.mp3'))
->model(OpenAI::model('gpt-4o-transcribe'))
->run();
echo $result->output->text;
$call = Live::voice()
->model(OpenAI::model('gpt-realtime-2.1'))
->instructions('You are the support desk.')
->call($verifiedWebhook['data']['call_id']);
$call->accept();
$controlSession = $call->connect($applicationTransport);
// Read normalized events and answer tool calls over the control connection.
foreach ($controlSession->events() as $event) {
// ...
}
$call->hangup();