An open API service indexing awesome lists of open source software.

https://github.com/webman-php/openai

OpenAI PHP asynchronous client for workerman and webman.
https://github.com/webman-php/openai

chatgpt client openai openai-php-sdk webman workerman

Last synced: 25 days ago
JSON representation

OpenAI PHP asynchronous client for workerman and webman.

Awesome Lists containing this project

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']],
], [
'timeout' => 600, //可选参数,默认600s
'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",
]);
}
```
### Qwen openai

```php
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