PHP code example of larva / laravel-ai-volc

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

    

larva / laravel-ai-volc example snippets


'providers' => [
    // ...

    'volc' => [
        'driver' => 'volc',
        'key' => env('VOLC_ARK_KEY'),
        'url' => env('VOLC_ARK_URL', 'https://ark.cn-beijing.volces.com/api/v3'),

        // 可选:默认模型与网关参数
        'models' => [
            'text' => [
                'default' => env('VOLC_TEXT_MODEL', 'doubao-seed-2-1-lite-260915'), // 默认文本模型
            ],
            'embeddings' => [
                'default' => env('VOLC_EMBEDDINGS_MODEL', 'doubao-embedding-vision-251215'), // 默认向量模型
                'dimensions' => env('VOLC_EMBEDDINGS_DIMENSIONS', 2048), // 向量维度:支持传入dimensions参数降维1024使用
            ],
            'audio' => [
                'default' => env('VOLC_AUDIO_MODEL', 'doubao-seed-tts-2.0'),   // 默认 TTS 模型
                'voice' => env('VOLC_AUDIO_VOICE', 'zh_female_gaolengyujie_uranus_bigtts'), // 默认女声
                'male_voice' => env('VOLC_AUDIO_MALE_VOICE', 'zh_male_qingshuangnanda_mars_bigtts'), // 默认男声
                'response_format' => env('VOLC_AUDIO_RESPONSE_FORMAT', 'mp3'), // mp3 / wav / pcm / ogg_opus
                'speed' => env('VOLC_AUDIO_SPEED', 1.0), // 语速 0.5 ~ 2.0
            ],
            'image' => [
                'default' => env('VOLC_IMAGE_MODEL', 'doubao-seedream-5-0-pro-260628'), // 默认图像模型
                'watermark' => false, // 是否添加水印
            ],
            'video' => [
                'default' => env('VOLC_VIDEO_MODEL', 'doubao-seedance-2-5-260628'), // 默认视频模型
            ],
        ],
    ],
],

use Laravel\Ai\Ai;

$embeddings = Ai::embeddingProvider('volc')
    ->embeddings(['火山引擎是字节跳动旗下的云服务平台'], model: 'doubao-embedding-large');

use Laravel\Ai\Ai;

$audio = Ai::audioProvider('volc')->audio('你好,欢迎来到火山方舟。');

// 保存为 mp3
$audio->store('greeting.mp3', 'public');

$audio = Ai::audioProvider('volc')->audio(
    '今天天气真不错。',
    voice: 'zh_female_wanwanxiaohe_moon_bigtts',
);

use Laravel\Ai\Ai;

$image = Ai::imageProvider('volc')->image(
    '一只在草地上奔跑的柴犬,阳光明媚,摄影风格',
    size: '3:2',
);

$image->store('shiba.png', 'public');

use Laravel\Ai\Files\RemoteImage;

// 图生图 / 多图参考:支持 RemoteImage / LocalImage / StoredImage / Base64Image
$image = Ai::imageProvider('volc')->image(
    '参考这张图的构图重新绘制',
    [new RemoteImage('https://example.com/ref.png')],
);

use Laravel\Ai\Ai;

/** @var \Larva\Ai\Volc\Providers\VolcProvider $provider */
$provider = Ai::instance('volc');

// 1. 创建任务(文生视频)
$task = $provider->createVideoTask('一只猫在草地上奔跑,镜头缓慢推进', [
    'duration' => 5,          // 时长(秒)
    'resolution' => '1080p',  // 480p / 720p / 1080p
    'ratio' => '16:9',        // 16:9 / 9:16 / 1:1 / 4:3 / 3:4 / 21:9
    'watermark' => false,
]);

// 图生视频:首帧 / 尾帧 / 多模态参考图
$task = $provider->createVideoTask('让画面动起来', [
    'images' => [
        ['url' => 'https://example.com/first.png', 'role' => 'first_frame'],
        ['url' => 'https://example.com/last.png', 'role' => 'last_frame'],
    ],
]);

// 2. 轮询任务状态
$result = $provider->getVideoResult($task['id']);

// status:queued / running / succeeded / failed / expired
if ($result['status'] === 'succeeded') {
    $videoUrl = $result['content']['video_url']; // URL 有效期 24 小时
}