OpenAI Agents for Laravel - Fully Prepared by CLAUDE AI - Replicated from openai-agents-python
The OpenAI Agents for Laravel package is a lightweight yet powerful framework for building multi-agent AI workflows in Laravel applications. This package is a Laravel port of the OpenAI Agents Python SDK.
- Agent loop: Built-in agent loop that handles calling tools, sending results to the LLM, and looping until the LLM is done
- Laravel-first: Uses built-in Laravel features to orchestrate and chain agents
- Handoffs: A powerful feature to coordinate and delegate between multiple agents
- Guardrails: Run input validations and checks in parallel to your agents, breaking early if the checks fail
- Function tools: Turn any PHP method into a tool, with automatic schema generation
- Tracing: Built-in tracing that lets you visualize, debug and monitor your workflows
You can install the package via composer:
composer require openai/agentsThen publish the configuration file:
php artisan vendor:publish --tag="agents-config"Set your OpenAI API key in your .env file:
OPENAI_API_KEY=your-api-key
OPENAI_DEFAULT_MODEL=gpt-4o
use OpenAI\Agents\Facades\Agent;
use OpenAI\Agents\Facades\Runner;
$agent = Agent::create("Assistant", "You are a helpful assistant");
$result = Runner::runSync($agent, "Write a haiku about recursion in programming.");
echo $result->getTextOutput();
// Code within the code,
// Functions calling themselves,
// Infinite loop's dance.use OpenAI\Agents\Facades\Agent;
use OpenAI\Agents\Facades\Runner;
$spanishAgent = Agent::create(
"Spanish agent",
"You only speak Spanish."
);
$englishAgent = Agent::create(
"English agent",
"You only speak English"
);
$triageAgent = Agent::create(
"Triage agent",
"Handoff to the appropriate agent based on the language of the request."
)->withHandoffs([$spanishAgent, $englishAgent]);
$result = Runner::runSync($triageAgent, "Hola, ¿cómo estás?");
echo $result->getTextOutput();
// ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?
use OpenAI\Agents\Facades\Agent;
use OpenAI\Agents\Facades\Runner;
use OpenAI\Agents\RunContext;
use OpenAI\Agents\Tools\Tool;
$weatherTool = new Tool(
'get_weather',
'Get the weather for a city',
function (RunContext $context, array $args) {
$city = $args['city'] ?? 'Unknown';
return "The weather in {$city} is sunny.";
}
);
$agent = Agent::create(
"Hello world",
"You are a helpful agent."
)->withTools([$weatherTool]);
$result = Runner::runSync($agent, "What's the weather in Tokyo?");
echo $result->getTextOutput();
// The weather in Tokyo is sunny.For detailed documentation, visit the OpenAI Agents for Laravel documentation.
Check out the examples directory for more usage examples:
- Hello World: A simple agent example
- Handoffs: An example of agent handoffs
- Functions: An example using function tools
- Laravel Controller Example: An example Laravel controller for synchronous and streaming agent responses
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please report security vulnerabilities to [email protected].
- CLAUDE AI
- Jawad Ashraf
- All Contributors
The MIT License (MIT). Please see License File for more information.