
Symfony AI 的核心目标很简单:让 PHP 开发者能够轻松地将 AI 能力集成到应用中,而无需依赖其他语言的复杂桥接。
2025 年 12 月 24 日,Symfony 社区迎来了一份特别的圣诞礼物——Symfony AI v0.1.0 的首个正式标记版本发布。
Symfony AI V1.0 正式发布,让 PHP 再次伟大!
主要包括:
Webman 框架 是基于 Workerman 的高性能常驻内存框架,不依赖 Symfony 的完整内核(如 HttpKernel、Bundle 系统),但高度兼容 Composer 生态,可以直接复用许多 Symfony 组件(如 Console、RateLimiter、Lock 等)。
由于 Symfony AI 的核心是 Composer 包,且不强制依赖完整的 Symfony 框架,你可以直接在 Webman 项目中安装并使用其底层组件。AI Bundle 部分(依赖 Symfony 配置系统)可能无法直接使用,但 Platform、Store 等组件完全可用。
这里基于谷歌 DeepMind 开发的多模态大型语言模型 Gemini 模型为案例进行安装和使用
composer require symfony/ai-platform # 统一 AI 平台接口(核心)
composer require symfony/ai-gemini-platform # Gemini 专用桥接包<?php
/**
* @desc GeminiController.php 描述信息
* @author Tinywan(ShaoBo Wan)
*/
declare(strict_types=1);
namespace app\controller;
use support\Request;
use support\Response;
use Symfony\AI\Platform\Bridge\Gemini\PlatformFactory;
use Symfony\AI\Platform\Exception\ExceptionInterface;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\Component\HttpClient\HttpClient;
class GeminiController
{
/**
* @desc chat
* @param Request $request
* @return Response
* @throws ExceptionInterface
* @author Tinywan(ShaoBo Wan)
*/
public function chat(Request $request):Response
{
// 从环境变量或 config 获取 API Key
$apiKey = getenv('GOOGLE_API_KEY') ?: config('app.google_api_key');
// 初始化 Gemini 平台(通过 Google Factory)
$platform = PlatformFactory::create(
$apiKey, // API Key
HttpClient::create() // 默认 HttpClient,支持自定义
);
$userMessage = $request->post('message', '你好,介绍一下自己');
// 构建消息
$messages = new MessageBag(
Message::forSystem('你好!介绍一下webman 框架'),
Message::ofUser($userMessage)
);
// 调用模型(同步)
$response = $platform->invoke(
'gemini-2.5-flash',
$messages,
[
'temperature' => 0.7,
]
);
return json([
'reply' => $response->getResult()->getContent(), // 获取响应内容
]);
}
}访问问webman接口
http://127.0.0.1:8787/gemini/chat
响应输出内容如下
{
"reply": "好的,很高兴为您介绍 **Webman 框架**。
Webman 是一个 **高性能、轻量级的 PHP 微服务框架**。
它基于 [Workerman](https://www.workerman.net/) 开发,
旨在提供媲美 Go 语言的性能体验,让 PHP 也能轻松构建高并发、低延迟的服务。。"
}
这样,你就能在 Webman 高性能环境下利用 Symfony AI 构建聊天机器人、RAG 搜索、智能代理等功能。如果遇到具体错误,可提供更多细节进一步排查。