# openai **Repository Path**: walkor/openai ## Basic Information - **Project Name**: openai - **Description**: 基于webman的PHP异步非阻塞openai客户端,支持高并发,如丝般顺滑 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: main - **Homepage**: https://www.workerman.net/plugin/157 - **GVP Project**: No ## Statistics - **Stars**: 7 - **Forks**: 3 - **Created**: 2024-04-09 - **Last Updated**: 2025-05-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: openAI, webman, WorkerMan ## README # openai OpenAI PHP asynchronous client for [workerman](https://github.com/walkor/workerman) and [webman](https://github.com/walkor/webman). # Install ``` composer create-project workerman/webman cd webman composer require webman/openai ``` ### Chat with stream ```php 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 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 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 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 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 ```php 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", ]); } ```