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?');
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
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;
}
});