PHP code example of snowman / ai
1. Go to this page and download the library: Download snowman/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/ */
snowman / ai example snippets
use SnowmanNunu\Ai\Laravel\Facades\Ai;
$response = Ai::chat([
['role' => 'user', 'content' => '用 PHP 写一个冒泡排序'],
]);
echo $response->content; // 模型回复文本
echo $response->usage->totalTokens; // 总 token 用量
echo $response->model; // 实际模型名
'drivers' => [
'kimi' => [
'api_key' => env('KIMI_API_KEY'),
'model' => 'kimi-coding',
// 代理
'proxy' => 'http://127.0.0.1:7890',
// 自定义 Guzzle HandlerStack(用于日志、签名等)
'handler' => $myHandlerStack,
// 重试
'retry' => 3,
'retry_delay' => 1000, // 首次重试等待毫秒
'retry_multiplier' => 2.0, // 指数退避倍数
'retry_on_status' => [429, 500, 502, 503, 504],
],
],
use SnowmanNunu\Ai\Support\Tool;
$tool = Tool::define('get_weather', '获取指定城市天气', [
'type' => 'object',
'properties' => [
'city' => ['type' => 'string', 'description' => '城市名'],
],
' echo $call->name; // get_weather
echo $call->arguments; // {"city":"北京"}
}
}
use SnowmanNunu\Ai\Support\VisionMessage;
$messages = [
['role' => 'user', 'content' => [
VisionMessage::text('描述这张图片'),
VisionMessage::image('https://example.com/cat.jpg'),
// 或传 base64
// VisionMessage::image('data:image/png;base64,xxx', 'high'),
]],
];
$response = Ai::chat($messages);
$messages = [
['role' => 'user', 'content' => [
VisionMessage::text('描述这张图片'),
VisionMessage::claudeImage($base64String, 'image/png'),
]],
];
$response = Ai::chat([['role' => 'user', 'content' => '9.11 和 9.8 哪个大?']]);
echo $response->content; // 最终答案
echo $response->reasoningContent; // 模型内部推理文本(可能为 null)
foreach (Ai::stream($messages) as $chunk) {
echo $chunk->reasoningContent ?? '';
echo $chunk->content;
if ($chunk->toolCalls) {
foreach ($chunk->toolCalls as $call) {
echo $call->name; // get_weather
echo $call->arguments; // {"city":"北京"}
}
}
}
Ai::chat(array $messages, array $options = []): AiResponse
Ai::stream(array $messages, array $options = []): Generator<StreamChunk>
$response = Ai::driver('openai')->chat([...]);
$response = Ai::driver('deepseek')->chat([...]);
Ai::extend('my-model', function (array $config) {
return new MyCustomDriver($config);
});
Ai::driver('my-model')->chat([...]);
use SnowmanNunu\Ai\Exceptions\AiRateLimitException;
use SnowmanNunu\Ai\Exceptions\AiException;
try {
$response = Ai::chat([...]);
} catch (AiRateLimitException $e) {
// 限流:等待后重试
sleep(5);
} catch (AiException $e) {
// 其他 AI 异常
logger()->error('AI error', ['message' => $e->getMessage()]);
}
use SnowmanNunu\Ai\ThinkPHP\Facades\Ai;
// 门面
$response = Ai::chat([
['role' => 'user', 'content' => '用 PHP 写一个冒泡排序'],
]);
// 容器
$response = app('ai')->chat([
['role' => 'user', 'content' => '用 PHP 写一个冒泡排序'],
]);
use SnowmanNunu\Ai\AiManager;
$manager = new AiManager([
'default' => 'claude',
'drivers' => [
'claude' => ['api_key' => 'sk-ant-xxxxxxxx'],
],
]);
$response = $manager->chat([['role' => 'user', 'content' => 'Hello']]);
bash
# 发布配置文件
php artisan vendor:publish --tag=ai-config
bash
# 使用默认驱动测试连通性
php artisan ai:test
# 指定驱动和提示词
php artisan ai:test --driver=kimi --prompt="你好"
# 流式输出(可观察 reasoning_content 增量)
php artisan ai:test --driver=kimi --stream
bash
composer 件会自动发布到 config/ai.php
# 如需手动执行:
php think vendor:publish
bash
# 使用默认驱动测试连通性
php think ai:test
# 指定驱动和提示词
php think ai:test --driver=kimi --prompt="你好"
# 流式输出(可观察 reasoning_content 增量)
php think ai:test --driver=kimi --stream