openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.78k stars 491 forks source link

Concurrency #473

Open NinoSkopac opened 1 week ago

NinoSkopac commented 1 week ago

Aka how to do this for 10 different inputs in the same time:

    $result = $client->audio()->speech([
        'model' => 'tts-1',
        'input' => 'Hello, how are you?',
        'voice' => 'alloy',
    ]);
NinoSkopac commented 1 week ago

Doesn't work with spatie/async.

<?php
use Spatie\Async\Pool;

require __DIR__ . '/vendor/autoload.php';

$input = [
    "Sentence 1.",
    "Sentence 2."
];
$pool = Pool::create();

$yourApiKey = 'get your own';
$client = OpenAI::client($yourApiKey);

foreach ($input as $index => $text) {
    $pool->add(function () use ($client, $text, $index) {
        $audio = $client->audio()->speech([
            'model' => 'tts-1',
            'input' => $text,
            'voice' => 'alloy',
            'response_format' => 'mp3',
        ]); // audio file content as string

        file_put_contents("output_$index.mp3", $audio);
    });
}

$pool->wait();

error

Fatal error: Uncaught TypeError: Cannot assign Laravel\SerializableClosure\Serializers\Native to property OpenAI\Transporters\HttpTransporter::$streamHandler of type Closure in /Users/onin/dev/tests/openai/tts/vendor/laravel/serializable-closure/src/Serializers/Native.php:513
Stack trace:
#0 /Users/onin/dev/tests/openai/tts/vendor/laravel/serializable-closure/src/Serializers/Native.php(513): ReflectionProperty->setValue(Object(OpenAI\Transporters\HttpTransporter), Object(Laravel\SerializableClosure\Serializers\Native))
#1 /Users/onin/dev/tests/openai/tts/vendor/laravel/serializable-closure/src/Serializers/Native.php(510): Laravel\SerializableClosure\Serializers\Native->mapByReference(Object(OpenAI\Transporters\HttpTransporter))
#2 /Users/onin/dev/tests/openai/tts/vendor/laravel/serializable-closure/src/Serializers/Native.php(440): Laravel\SerializableClosure\Serializers\Native->mapByReference(Object(OpenAI\Client))
#3 /Users/onin/dev/tests/openai/tts/vendor/laravel/serializable-closure/src/Serializers/Native.php(143): Laravel\SerializableClosure\Serializers\Native->mapByReference(Array)
#4 [internal function]: Laravel\SerializableClosure\Serializers\Native->__serialize()
#5 /Users/onin/dev/tests/openai/tts/vendor/spatie/async/src/Runtime/ParentRuntime.php(88): serialize(Object(Laravel\SerializableClosure\SerializableClosure))
#6 /Users/onin/dev/tests/openai/tts/vendor/spatie/async/src/Runtime/ParentRuntime.php(70): Spatie\Async\Runtime\ParentRuntime::encodeTask(Object(Laravel\SerializableClosure\SerializableClosure), 100000)
#7 /Users/onin/dev/tests/openai/tts/vendor/spatie/async/src/Pool.php(149): Spatie\Async\Runtime\ParentRuntime::createProcess(Object(Closure), NULL, '/opt/homebrew/C...', 100000)
#8 /Users/onin/dev/tests/openai/tts/app.php(17): Spatie\Async\Pool->add(Object(Closure))
#9 {main}
  thrown in /Users/onin/dev/tests/openai/tts/vendor/laravel/serializable-closure/src/Serializers/Native.php on line 513
ondraasek commented 3 days ago

I would go with multi curl

ondraasek commented 3 days ago

or dispatch laravel jobs, depends on your logic