webman / openai
OpenAI client for webman or workerman
Installs: 1 152
Dependents: 0
Suggesters: 0
Security: 0
Stars: 54
Watchers: 2
Forks: 4
Open Issues: 1
Requires
- php: >=7.2.0
- workerman/http-client: ^v2.1.1
- workerman/workerman: >=v4.1.11
README
OpenAI PHP asynchronous client for workerman and webman.
Install
composer create-project workerman/webman
cd webman
composer require webman/openai
Chat with stream
<?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) . "\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", ]); } }
Chat without stream
<?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", ]); } }
Image generations
<?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", ]); } }
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
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", ]); }
Qwen openai
public function completions(Request $request) { $connection = $request->connection; $chat = new Chat(['api' => 'https://dashscope.aliyuncs.com/compatible-mode', 'apikey' => 'xxx']); $chat->completions( [ 'model' => 'qwen-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", ]); }
Help:https://help.aliyun.com/zh/dashscope/developer-reference/compatibility-of-openai-with-dashscope