上期用Laravel AI SDK搭了知识库问答(RAG),让AI能读你的文档。
今天聊个更厉害的东西:Agent(智能体) 。
一句话说清RAG和Agent的区别:
RAG:AI会“查资料”,但只会张嘴回答
Agent:AI会“动手做”——查天气、算账、写文件、调API,啥都能干
今天带你用Neuron-AI框架跑通第一个Agent,给AI装上“手脚”。
一、一句话说清原理
Agent = 大模型 + 工具
传统大模型只会“说”,Agent先理解你的需求,再决定调用什么工具去完成。你可以给它配多个工具,它自己判断用哪个。
举个生活例子:你问“今天深圳天气怎么样,适合出门吗?”
普通AI会胡编一个答案
Agent会先调用天气API查实时数据,再结合数据回答你“今天26°C,晴天,适合出门”
这就是Agent的“行动力”。
二、准备工作
Neuron-AI是一个PHP原生框架,专门用来构建AI Agent,支持工具调用、对话记忆和RAG等功能。
安装只需要一行:
composer require neuron-core/neuron-ai要求 PHP 8.1+。
然后创建一个Agent类:
./vendor/bin/neuron make:agent App\\Neuron\\MyAgent三、跑通第一个Agent
创建一个简单Agent,回答“你是谁”:
<?phpnamespace App\Neuron;use NeuronAI\Agent\Agent;use NeuronAI\Providers\DeepSeek\DeepSeek;classMyAgentextendsAgent{protected functionprovider(){// 使用DeepSeek,国内直接可用return new DeepSeek(apiKey: '你的DeepSeek API Key',model: 'deepseek-chat');}public functioninstructions(): string{return "你是一个友好的AI助手,用中文回答问题。";}}使用Agent:$agent = MyAgent::make();$response = $agent->chat('你是谁?');echo $response;
是不是跟RAG那篇文章很像?原理一样,但Neuron-AI更强的是Tool Calling(工具调用) 。
四、给Agent装上工具
用Tool Calling实现“查天气”功能。创建一个工具类:
./vendor/bin/neuron make:tool App\\Neuron\\Tools\\WeatherTool编写工具逻辑:
<?phpnamespace App\Neuron\Tools;use NeuronAI\Tools\Tool;use NeuronAI\Tools\ToolParameters;classWeatherToolextendsTool{public function__construct(){$this->name = 'get_weather';$this->description = '获取指定城市的实时天气';$this->parameters = ToolParameters::create(['city' => 'string,城市名称,例如深圳']);}protected functionhandle(array$params, callable$sendUpdate = null){$city = $params['city'];// 调用天气API,这里简化返回模拟数据return "{$city},晴天,26°C";}}
把工具注册到Agent:
class MyAgent extends Agent{// ... provider和instructions部分同上 ...public function tools(): array{return [new WeatherTool()];}}
现在你问“深圳天气怎么样”,Agent会先调用WeatherTool拿到“深圳,晴天,26°C”,再组织语言告诉你。
五、让Agent一次调用多个工具
Agent不仅能调用一个工具,还能根据需求串联多个工具。
创建一个“计算器”工具:
./vendor/bin/neuron make:tool App\\Neuron\\Tools\\CalculatorToolclassCalculatorToolextendsTool{public function__construct(){$this->name = 'calculate';$this->description = '执行数学计算';$this->parameters = ToolParameters::create(['expression' => 'string,数学表达式,例如 1+1']);}protected functionhandle(array$params, callable$sendUpdate = null){$result = eval('return ' . $params['expression'] . ';');return "计算结果:{$result}";}}
注册两个工具后,用户问“深圳明天会比今天热多少?”,Agent可能会:
调用get_weather查今天温度
调用get_weather查明天温度
调用calculate算温差
汇总后回复你
一个Agent配一套工具,就是你的专属AI助理。
六、进阶思路
Neuron-AI还支持对话记忆(Chat History),你可以把对话存到数据库、文件或任何存储中。如果不想从零造轮子,也可以直接用PocketFlow这个极简框架(约400行代码),用Node和Flow编排复杂任务。
Agent是2026年AI应用的大方向,先从今天这个“查天气+算数”的Agent跑起,后续再加更多工具——查数据库、发邮件、生成PDF,都可以。
七、下期预告
Neuron-AI + RAG + 多工具协作,做一个“能读文档又能干活”的超级Agent
夜雨聆风