当前位置:首页>php>PHP Agentic 实战02〡环境准备与框架安装

PHP Agentic 实战02〡环境准备与框架安装

  • 2026-09-14 14:04:23
PHP Agentic 实战02〡环境准备与框架安装

本章节导读

指导如何在您的 webman 应用中安装 Neuron 并创建一个 Agent。

要求

  • PHP: ^8.4
  • Composer: ^2.0

安装

运行以下 Composer 命令安装最新版本:

安装 webman 框架

官方文档:https://www.workerman.net/doc/webman/install.html

# 默认使用交互式安装向导composer create-project workerman/webman:~2.0# 禁用交互式安装向导(本章节)composer create-project workerman/webman:~2.0 --no-interaction  neuron.ai.tinywan.com

安装 Neuron 框架

composer require neuron-core/neuron-ai

创建一个智能体

你可以使用下面的命令轻松创建你的第一个智能体:

./vendor/bin/neuron make:agent app\\neuron\\ZaiAgent

生成的智能体类示例:

<?php/** * @desc ZaiAgent * @author Tinywan(ShaoBo Wan) */declare(strict_types=1);namespace app\neuron;use NeuronAI\Agent\Agent;use NeuronAI\Agent\SystemPrompt;use NeuronAI\Providers\AIProviderInterface;use NeuronAI\Providers\ZAI\ZAI;classZaiAgentextendsAgent{protectedfunctionprovider(): AIProviderInterface{returnnew ZAI(            key: "sk-xxxx",            model: "glm-5",            parameters: [], // Add custom params (temperature, logprobs, etc)        );    }publicfunctioninstructions(): string{return (string)new SystemPrompt(            background: ["你是由开源技术小栈开发的 Agent。",            ],        );    }}

API Key 获取地址 https://bigmodel.cn/usercenter/proj-mgmt/apikeys

与智能体对话

向智能体发送提示,获取底层大语言模型(LLM)的响应:

<?php/** * @desc ZaiAgent * @author Tinywan(ShaoBo Wan) */declare(strict_types=1);namespace app\controller;use support\Request;use NeuronAI\Chat\Messages\UserMessage;classIndexController{publicfunctionindex(Request $request){        $message = \app\neuron\ZaiAgent::make()            ->chat(new UserMessage('你是是大模型?'))            ->getMessage();return $message->getContent();    }}

最新文章

随机文章