
2026 年了,用 PHP 写 AI Agent,可能比 Python 更香。
Neuron 是面向 PHP 的「Agentic 框架」,用来创建与编排 AI Agent,并提供 LLM 接口、数据加载、多 Agent 编排、RAG、监控与调试等完整生命周期支持。 说白了,它就是 PHP 的「LangChain/LlamaIndex 级存在」,但专为 PHP 工程师设计。
方案 | 学习成本 | 生态兼容 | 维护成本 |
|---|---|---|---|
自己封装 LLM + 工具调用 | 高 | 一般 | 高 |
直接用 Python LangChain | 低 | 与 PHP 业务割裂 | 高 |
Neuron(PHP) | 低 | 与 PHP 代码同仓库同部署 | 中低 |
我们选 Neuron 的理由很简单:
webman 是基于 Workerman 的高性能 PHP Web 框架,支持 HTTP / WebSocket / TCP / UDP,并采用常驻内存、协程、连接池等技术,把 PHP 的性能天花板抬高了不止一个档次。 关键特性:
下面这个结构是你可以直接照抄的“最小可用项目骨架”。
php-neuron-agent/
├── app/
│ ├── controller/
│ │ └── AgentController.php # HTTP 入口
│ ├── agent/
│ │ ├── Orchestrator.php # 多 Agent 编排/调度
│ │ └── agents/
│ │ ├── CustomerServiceAgent.php
│ │ ├── DataAnalysisAgent.php
│ │ └── ToolsAgent.php
│ └── middleware/
├── config/
│ ├── neuron.php
│ ├── database.php
│ └── redis.php
├── public/
│ └── index.php
├── process/
│ └── AgentMonitor.php # webman 自定义进程(可选:Agent 监控/队列)
├── composer.json
├── Dockerfile
└── docker-compose.yml
composer require neuron-core/neuron-ai
Neuron 提供了一个 Agent 基类,你只需要继承并实现:
provider():指定 LLM 提供商和模型instructions():设置系统提示(角色、背景、规则)tools():注册可用工具/工具包// app/agent/agents/CustomerServiceAgent.php
namespace app\agent\agents;
use NeuronAI\Agent;
use NeuronAI\Chat\Messages\UserMessage;
use NeuronAI\SystemPrompt;
use NeuronAI\Providers\Anthropic\Anthropic;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Tools\Toolkits\CalculatorToolkit;
class CustomerServiceAgent extends Agent
{
protectedfunction provider(): AIProviderInterface
{
// 一行切换 Anthropic/OpenAI/Gemini 等
returnnew Anthropic(
env('ANTHROPIC_API_KEY'),
'claude-4-5-sonnet'
);
}
publicfunction instructions(): string
{
return (string) new SystemPrompt(
background: [
'你是一个友好的客服助手。'.
'基于公司知识库与订单数据回答问题,语气专业且简洁。'
]
);
}
publicfunction tools(): array
{
return [
CalculatorToolkit::make(), // 计算类工具
// OrderToolkit::make(), // 自定义订单查询工具
// KnowledgeBaseToolkit::make(), // 自定义 RAG 工具
];
}
}
// app/controller/AgentController.php
namespace app\controller;
use support\Request;
use app\agent\agents\CustomerServiceAgent;
use NeuronAI\Chat\Messages\UserMessage;
class AgentController
{
publicfunction chat(Request $request)
{
$q = $request->input('question', '');
$agent = CustomerServiceAgent::make();
$response = $agent->chat(new UserMessage($q));
return json([
'answer' => $response->getMessage()->getContent(),
]);
}
}
如果你愿意,也可以通过 webman 的自定义进程,把 Agent 调度做成后台长期服务(监听队列、定时触发等),与 HTTP 服务共享同一套基础设施。
当业务变复杂时,通常需要多个“专职 Agent + 编排层”。
┌───────────────────────────────────────────────────┐
│ Claude Code(主 Agent) │
│ - 任务拆解 │
│ - 最终审核 & 输出 │
└──────┬───────────────────────────────┬─────────────┘
│ │
┌──────▼──────────┐ ┌─────────▼──────────┐
│ DataAnalysis │ │ CustomerService │
│ Agent │ │ Agent │
│ - SQL 查询 │ │ - 订单查询 │
│ - 报表生成 │ │ - 知识库问答 │
└─────────────────┘ └────────────────────┘
│ │
└───────────────┬───────────────┘
│
┌────────▼─────────┐
│ Tools Agent │
│ - 通用计算 │
│ - 外部 API │
└──────────────────┘
// app/agent/Orchestrator.php
namespace app\agent;
use app\agent\agents\DataAnalysisAgent;
use app\agent\agents\CustomerServiceAgent;
use NeuronAI\Chat\Messages\UserMessage;
class Orchestrator
{
publicfunction run(string $userRequest): array
{
// 1)用主 Agent 拆解任务(可用 Claude/GPT 等)
// 2)根据任务类型,分发给不同子 Agent
// 3)并行/串行执行
// 4)汇总结果,交给主 Agent 再做最终审核 & 输出
// 示例:简单串行
$serviceAgent = CustomerServiceAgent::make();
$analysisAgent = DataAnalysisAgent::make();
$serviceRes = $serviceAgent->chat(new UserMessage($userRequest));
$analysisRes = $analysisAgent->chat(new UserMessage($userRequest));
return [
'customer_service' => $serviceRes->getMessage()->getContent(),
'data_analysis' => $analysisRes->getMessage()->getContent(),
];
}
}
真实项目里,你可以用 webman 的异步/队列能力把子任务做成并行,以提升整体吞吐。
用户输入:
帮我统计最近 30 天的每日销售额,并找出销量 Top 3 的商品。
执行流程:
用户输入:
我的订单 #12345 发货了吗?如果没发,帮我催一下。
执行流程:
指标 | PHP | Python | 优势 |
|---|---|---|---|
启动时间 | 常驻内存,几无冷启动 | 依依赖数量,通常数百毫秒级 | PHP 更稳 |
内存占用 | 常驻几十 MB 级别 | 通常上百 MB,GIL 与 GC 抖动 | PHP 更省 |
并发请求 | 事件驱动 + 自定义进程 | asyncio / Uvicorn | 两者皆可 |
部署复杂度 | 单进程/多进程,复用现有运维 | 需要虚拟环境/依赖管理 | PHP 更简单 |
业务集成 | 与现有 PHP 代码同栈 | 需跨语言/服务调用 | PHP 更紧密 |
热重载 | 不如 Python 方便 | 改完代码即刻生效 | Python 更方便 |
坦白讲,Python 在“原型验证 & 生态丰富度”上依旧有优势。但在生产环境,尤其是已有大量 PHP 业务资产的团队,PHP + webman + Neuron 明显更舒服。