PHP code example of aerobit / openai-agents

1. Go to this page and download the library: Download aerobit/openai-agents 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/ */

    

aerobit / openai-agents example snippets


use Aerobit\OpenaiAgents\AgentManager;

$manager = app(AgentManager::class);
$response = $manager->agent()->chat('Hello world');

$agent = $manager->agent();

// First user message
$agent->chat('Hello');

// Follow-up message uses previous context
$reply = $agent->chat('What did I just say?');

$audio = $agent->speak('Hello world');
file_put_contents('output.mp3', $audio);

use Aerobit\OpenaiAgents\Agent;
use OpenAI\OpenAI;

$client = OpenAI::factory()->withApiKey(env('OPENAI_API_KEY'))->make();
$agent = new Agent($client, [], 'You are a helpful assistant.');

use Aerobit\OpenaiAgents\Runner;

$runner = new Runner($agent, maxTurns: 3);
$runner->registerTool('echo', fn($text) => $text);
$helper = new Agent($client, [], 'Espanol agente');
$runner->registerAgent('spanish', $helper);
$reply = $runner->run('Start');
// inside the conversation use [[handoff:spanish]] to switch

$schema = ['ew Runner($agent, maxTurns: 3, tracer: null, outputType: $schema);
$result = $runner->run('Start'); // returns an associative array

$runner->registerFunctionTool('echo', fn(array $args) => $args['text'], [
    'type' => 'object',
    'properties' => ['text' => ['type' => 'string']],
    '

$fiber = $runner->runAsync('Hello');
$fiber->start();
$result = $fiber->getReturn();

foreach ($runner->runStreamed('Hello') as $chunk) {
    echo $chunk;
}

$pipeline = new VoicePipeline($client, $agent);
$audio = $pipeline->run('input.wav');
file_put_contents('reply.mp3', $audio);

return [
    // ...
    'tracing' => [
        'enabled' => true,
        'processors' => [
            fn(array $record) => logger()->info('agent trace', $record),
            new \Aerobit\OpenaiAgents\Tracing\HttpProcessor('https://example.com/trace'),
        ],
    ],
];

use Aerobit\OpenaiAgents\Guardrails\InputGuardrail;
use Aerobit\OpenaiAgents\Guardrails\OutputGuardrail;
use Aerobit\OpenaiAgents\Guardrails\OutputGuardrailException;

$runner->addInputGuardrail(new class extends InputGuardrail {
    public function validate(string $content): string
    {
        return strtoupper($content);
    }
});

$runner->addOutputGuardrail(new class extends OutputGuardrail {
    public function validate(string $content): string
    {
        if (str_contains($content, 'bad')) {
            throw new OutputGuardrailException('Bad content');
        }
        return $content;
    }
});
bash
php artisan vendor:publish --tag=config --provider="Aerobit\\OpenaiAgents\\AgentServiceProvider"
bash
php artisan agent:chat "Hello"
bash
php artisan agent:chat "Hello" --system="You are helpful" --max-turns=3