怎么在 webman command 使用 webman/openai 调用AI接口

chen

控制器里这么用

<?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 国内访问不到的话可以用地址 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) {
                // 当openai接口返回数据时转发给浏览器
                $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"));
                }
                // 返回空的chunk代表响应结束
                $connection->send(new Chunk(''));
            },
        ]);
        // 先返回一个http头,后面数据异步返回
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
        ]);
    }
}

命令行里怎么获取 $connection 对象

194 2 0
2个回答

nitron

use Workerman\Connection\TcpConnection;

https://www.workerman.net/doc/workerman/tcp-connection.html

  • chen 7天前

    大神,怎么 new 这个对象呢

    public function __construct(EventInterface $eventLoop, $socket, string $remoteAddress = '')

    这三个参数不知道传什么

  • nitron 7天前

    你命令行里不需要connection对象啊,这个是写http response的,不是输出stdout的

  • chen 7天前
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            $chat = new Chat(['apikey' => 'sk-xxxx', 'api' => 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions']);
            $chat->completions(
                [
                    'model' => 'qwen-max',
                    'stream' => true,
                    'messages' => [['role' => 'user', 'content' => 'hello']],
                ], [
                'stream' => function($data) {
                    echo " ---------- data start ----------";
                    print($data);
                    echo " ---------- data end ----------";
                },
                'complete' => function($result, $response) {
                    echo " ---------- result start ----------";
                    print_r($result);
                    echo " ---------- result end ----------";
                },
            ]);
            return self::SUCCESS;
        }

    哦我理解了,不在控制器里用的时候,用不到 $connection 对象

    我在 command 里的代码类似这样,但是报错:
    [RuntimeException]
    Timer can only be used in workerman running environment

  • nitron 6天前

    字面意义, Timer要运行在Workerman环境下,你不如直接跑Webman,命令行只是用Http去请求这个webman的接口

  • chen 6天前

    哦哦,好的,谢谢大神

webman/openai 不支持webman command

  • chen 6天前

    好的,我先暂时用别的客户端吧

🔝