Openai 異步客戶端 支持ChatGPT Dall.E等模型

v2.0.1
版本
2025-04-22
版本更新時(shí)間
2167
安裝
63
star
簡介
傳統(tǒng)php-fpm架構(gòu)調(diào)用openai等大模型接口時(shí)只能做到阻塞調(diào)用,由于大模型接口返回速度很慢,一個(gè)php-fpm進(jìn)程一分鐘只能調(diào)用幾次,幾個(gè)人一刷系統(tǒng)就會明顯的卡頓甚至不可用狀態(tài),所以php-fpm不適合做大模型調(diào)用,而webman這類的常駐內(nèi)存類型的框架非常適合大模型應(yīng)用的開發(fā)。
webman/openai是一個(gè)異步非阻塞的openai客戶端,配合webman可以做到同一時(shí)刻支持上萬并發(fā)調(diào)用,使用簡單,返回如絲般的順滑。
安裝
composer require webman/openai
使用
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;
// https://api.openai.com 國內(nèi)訪問不到的話可以用地址 https://api.openai-proxy.com 替代
$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) {
// 當(dāng)openai接口返回?cái)?shù)據(jù)時(shí)轉(zhuǎn)發(fā)給瀏覽器
$connection->send(new Chunk(json_encode($data, JSON_UNESCAPED_UNICODE) . "\n"));
},
'complete' => function($result, $response) use ($connection) {
// 響應(yīng)結(jié)束時(shí)檢查是否有錯(cuò)誤
if (isset($result['error'])) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "\n"));
}
// 返回空的chunk代表響應(yīng)結(jié)束
$connection->send(new Chunk(''));
},
]);
// 先返回一個(gè)http頭,后面數(shù)據(jù)異步返回
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}
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, JSON_UNESCAPED_UNICODE) . "\n"));
$connection->send(new Chunk(''));
},
]);
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}
Dall.E畫圖
<?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",
]);
}
}
TTS語音
<?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",
]);
}
}
Embeddings
<?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",
]);
}
}
Azure OpenAI
如果是Azure OpenAI
接口,需要額外傳入 'isAzure' => true
選項(xiàng)
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) . "\n"));
},
'complete' => function($result, $response) use ($connection) {
if (isset($result['error'])) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "\n"));
}
$connection->send(new Chunk(''));
},
]);
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}