1 Star 2 Fork 0

walkor / openai

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

openai

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",
    ]);
}
MIT License Copyright (c) 2024 webman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于webman的PHP异步非阻塞openai客户端,支持高并发,如丝般顺滑 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/walkor/openai.git
git@gitee.com:walkor/openai.git
walkor
openai
openai
main

搜索帮助