定制 aerobit/openai-agents 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aerobit/openai-agents

Composer 安装命令:

composer require aerobit/openai-agents

包简介

Laravel OpenAI Agents SDK

README 文档

README

This package provides a lightweight integration of the OpenAI PHP client with Laravel 12, inspired by the OpenAI Agents Python SDK.

Installation

composer require aerobit/openai-agents

Publish the configuration file:

php artisan vendor:publish --tag=config --provider="Aerobit\\OpenaiAgents\\AgentServiceProvider"

Set your OPENAI_API_KEY in the environment file or edit config/agents.php.

Usage

Send a message to the default agent:

php artisan agent:chat "Hello"

You can control the number of turns or provide a system prompt using options:

php artisan agent:chat "Hello" --system="You are helpful" --max-turns=3

You can also resolve the AgentManager service from the container to create agents programmatically.

use Aerobit\OpenaiAgents\AgentManager;

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

The returned Agent instance retains the conversation history, allowing for multi-turn chats:

$agent = $manager->agent();

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

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

The agent can also convert text responses to speech:

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

You can optionally provide a system prompt when constructing the agent:

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.');

For more advanced scenarios you can use the Runner class which loops until the agent returns a final response or a turn limit is reached. Tools and basic handoffs can be registered on the runner:

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

The runner can request structured JSON output by providing an output schema:

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

Tools may also be defined with JSON schemas for OpenAI function calling:

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

// Or let the runner derive the schema automatically
$runner->registerAutoFunctionTool('echo_auto', function (string $text) {
    return $text;
});

If you need non-blocking execution you can run the runner inside a PHP Fiber:

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

You can also stream results as they're generated:

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

Voice pipeline

Use the VoicePipeline class to handle audio transcription and text-to-speech in one step:

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

Tracing

The package includes a simple tracing system that lets you observe each turn of a Runner execution. Enable tracing in config/agents.php and register one or more processors to handle trace records:

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

When enabled, each call to Runner::run() will emit start and end span events as well as per-turn events containing the input and output.

Guardrails

Guardrails let you validate input and output during a run. They can transform the content or throw an exception to stop execution.

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

Configuration

The config/agents.php file allows you to customize the default model and parameters used when interacting with OpenAI. It also contains options to enable tracing and provide custom processors for handling trace data.

License

Released under the MIT license.

aerobit/openai-agents 适用场景与选型建议

aerobit/openai-agents 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 aerobit/openai-agents 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aerobit/openai-agents 我们能提供哪些服务?
定制开发 / 二次开发

基于 aerobit/openai-agents 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-24