Skip to content

jawadashraf/openai-agents-laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI Agents for Laravel - Fully Prepared by CLAUDE AI - Replicated from openai-agents-python

Latest Version on Packagist GitHub Tests Action Status GitHub Lint Action Status Total Downloads

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.

Features

  • 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

Installation

You can install the package via composer:

composer require openai/agents

Then publish the configuration file:

php artisan vendor:publish --tag="agents-config"

Configuration

Set your OpenAI API key in your .env file:

OPENAI_API_KEY=your-api-key
OPENAI_DEFAULT_MODEL=gpt-4o

Basic Usage

Hello world example

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.

Handoffs example

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?

Functions example

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.

Documentation

For detailed documentation, visit the OpenAI Agents for Laravel documentation.

Examples

Check out the examples directory for more usage examples:

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please report security vulnerabilities to [email protected].

Credits

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages