PHP code example of zherooo / ai-client

1. Go to this page and download the library: Download zherooo/ai-client 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/ */

    

zherooo / ai-client example snippets


use AiClient\Factory\AiFactory;
use AiClient\Request\ChatRequest;

// 创建客户端
$ai = AiFactory::make('volcengine', [
    'api_key' => 'your-api-key',
    'model' => 'deepseek-v3',  // 设置默认模型
]);

// 创建请求
$request = new ChatRequest();
$request->prompt = '写一首诗';

// 发送请求
$response = $ai->chat($request);
echo $response->content;

$ai->chatStream($request, function (array $data) {
    echo $data['choices'][0]['delta']['content'] ?? '';
});

$request = new ChatRequest();
$request->prompt = '你好';
$request->model = 'qwen-max';  // 覆盖默认模型

$response = $ai->chat($request);

$request = new ChatRequest();
$request->messages = [
    ['role' => 'system', 'content' => '你是一个助手'],
    ['role' => 'user', 'content' => '你好'],
];

$response = $ai->chat($request);