PHP code example of hbb / cmer-ai

1. Go to this page and download the library: Download hbb/cmer-ai library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hbb / cmer-ai example snippets



ssages = [
    ['role' => 'user', 'content' => '写100字的文案']
];

# 请求体 - 负责组装请求参数
$payload = new \Hbb\CmerAi\models\ChatModel($messages);

# 更换默认参数
$payload->model = 'gpt-3.5-turbo-0125';  # 默认gpt-3.5-turbo-1106

# 配置网关授权参数
$apikey = 'xxxxxxxxxx';
$X_CmerApi_Host = 'xxxxxxxxxx';
# 网关授权参数配置OK

# 请求启动器 - 负责发起请求
$payload = new \Hbb\CmerAi\CmerAi($apikey, $X_CmerApi_Host);

# 发起请求,函数名:openai_chat 对应接口文档中的路由: /v1/openai/chat
$response = $cmerai->openai_chat($payload);
var_dump($response->getBody()->getContents());

# 流式响应
$response = $cmerai->openai_chat_stream($payload);
if ($response->getStatusCode() == 200)
{
    $body = $response->getBody();
    if ($body instanceof \GuzzleHttp\Psr7\Stream)
    {
        // 从流中逐行读取数据
        while (!$body->eof()) {
            echo $body->read(1024); // 读取流中的数据
        }
    }

}