Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency #473

Closed
NinoSkopac opened this issue Sep 7, 2024 · 3 comments
Closed

Concurrency #473

NinoSkopac opened this issue Sep 7, 2024 · 3 comments

Comments

@NinoSkopac
Copy link

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
Copy link
Author

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
Copy link

I would go with multi curl

@ondraasek
Copy link

or dispatch laravel jobs, depends on your logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants