Artificial Intelligence 6 min read
How to Build Async OpenAI PHP Clients with Workerman & Webman
This guide shows how to install the OpenAI PHP async client and implement streaming and non‑streaming chat, image generation, audio speech, and embedding features using Workerman and Webman, including Azure OpenAI support, with complete code examples.
Open Source Tech Hub
Open Source Tech Hub
Installation
composer require webman/openaiStreaming Chat
<?php
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
use Workerman\Protocols\Http\Chunk;
class ChatController
{
public function completions(Request $request)
{
$connection = $request->connection;
$chat = new Chat(['apikey' => 'sk-xx', 'api' => 'https://api.openai.com']);
$chat->completions(
[
'model' => 'gpt-3.5-turbo',
'stream' => true,
'messages' => [['role' => 'user', 'content' => 'hello']],
],
[
'stream' => function ($data) use ($connection) {
$connection->send(new Chunk(json_encode($data, JSON_UNESCAPED_UNICODE) . "
"));
},
'complete' => function ($result, $response) use ($connection) {
if (isset($result['error'])) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "
"));
}
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}Non‑Streaming Chat
<?php
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
use Workerman\Protocols\Http\Chunk;
class ChatController
{
public function completions(Request $request)
{
$connection = $request->connection;
$chat = new Chat(['apikey' => 'sk-xxx', 'api' => 'https://api.openai.com']);
$chat->completions(
[
'model' => 'gpt-3.5-turbo',
'messages' => [['role' => 'user', 'content' => 'hello']],
],
[
'complete' => function ($result, $response) use ($connection) {
$connection->send(new Chunk(json_encode($result) . "
"));
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}Image Generation
<?php
namespace app\controller;
use support\Request;
use Webman\Openai\Image;
use Workerman\Protocols\Http\Chunk;
class ImageController
{
public function generations(Request $request)
{
$connection = $request->connection;
$image = new Image(['apikey' => 'sk-xxx', 'api' => 'https://api.openai.com']);
$image->generations(
[
'model' => 'dall-e-3',
'prompt' => 'a dog',
'n' => 1,
'size' => "1024x1024",
],
[
'complete' => function ($result) use ($connection) {
$connection->send(new Chunk(json_encode($result)));
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
"Content-Type" => "application/json",
"Transfer-Encoding" => "chunked",
]);
}
}Audio Speech
<?php
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
use Workerman\Protocols\Http\Chunk;
class AudioController
{
public function speech(Request $request)
{
$connection = $request->connection;
$audio = new Audio(['apikey' => 'sk-xxx', 'api' => 'https://api.openai.com']);
$audio->speech(
[
'model' => 'tts-1',
'input' => '你好,有什么可以帮您?',
'voice' => 'echo',
],
[
'stream' => function ($buffer) use ($connection) {
$connection->send(new Chunk($buffer));
},
'complete' => function ($result, $response) use ($connection) {
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
"Content-Type" => "audio/mpeg",
"Transfer-Encoding" => "chunked",
]);
}
}Embedding Vectors
<?php
namespace app\controller;
use support\Request;
use Webman\Openai\Embedding;
use Workerman\Protocols\Http\Chunk;
class EmbeddingController
{
public function create(Request $request)
{
$connection = $request->connection;
$embedding = new Embedding(['apikey' => 'sk-xxx', 'api' => 'https://api.openai.com']);
$embedding->create(
[
'model' => 'text-embedding-ada-002',
'input' => 'Some words',
'encodding_format' => 'float',
],
[
'complete' => function ($result) use ($connection) {
$connection->send(new Chunk(json_encode($result)));
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
"Content-Type" => "application/json",
"Transfer-Encoding" => "chunked",
]);
}
}Microsoft Azure OpenAI (Azure OpenAI Service)
public function completions(Request $request)
{
$connection = $request->connection;
$chat = new Chat([
'api' => 'https://xxx.openai.azure.com',
'apikey' => 'xxx',
'isAzure'=> true,
]);
$chat->completions(
[
'model' => 'gpt-3.5-turbo',
'stream' => true,
'messages' => [['role' => 'user', 'content' => 'hello']],
],
[
'stream' => function ($data) use ($connection) {
$connection->send(new Chunk(json_encode($data, JSON_UNESCAPED_UNICODE) . "
"));
},
'complete' => function ($result, $response) use ($connection) {
if (isset($result['error'])) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "
"));
}
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}Original Source
Signed-in readers can open the original source through BestHub's protected redirect.
Republication Notice
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Written by
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
0 followers
Reader feedback
How this landed with the community
Rate this article
Was this worth your time?
Discussion
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
