php中使用hyperf框架调用文心千帆大模型实现国内版chatgpt功能示例
swoole  /  管理员 发布于 1年前   694
文心千帆大模型平台是面向企业开发者的一站式大模型开发及服务运行平台。
文心千帆不仅提供了包括文心一言底层模型(ERNIE-Bot)和第三方开源大模型,还提供了各种AI开发工具和整套开发环境,方便客户轻松使用和开发大模型应用。
文心千帆申请平台
https://cloud.baidu.com/product/wenxinworkshop
百度ai接口调试中心
https://console.bce.baidu.com/tools/#/index
必备:
hyperf2.2环境架构;
文心千帆应用申请好,拿到key secret;
ps:
目前申请还给20元优惠券呢。
示例代码:
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\DbConnection\Db;
use App\Utils\Cache;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\HttpServer\Annotation\AutoController;
/**
* @AutoController()
*/
class WxqfController extends BaseController
{
private $config = [
'appkey' => 'xxx',
'appSecret' => 'xxx'
];
/**
* @var \Hyperf\Guzzle\ClientFactory
*/
private $clientFactory;
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
public function index(ResponseInterface $response,RequestInterface $request)
{
//tk 30day - 1000m
$tks = $this->getwxqftk();
$url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token={$tks['access_token']}";
// $client 为协程化的 GuzzleHttp\Client 对象
$client = $this->clientFactory->create([]);
$res = $client->request('POST', $url,[
'headers' => ['content-type' => 'application/json'],
'body' => '{"messages":[{"role":"user","content":"你好"}]}',
]);
//转数组
$res = json_decode((string) $res->getBody(),true);
var_dump($res);
return $res;
}
public function getwxqftk()
{
$wxqftk = Cache::get('wxqftk');
if (!Cache::has('wxqftk')) {
$url = "https://aip.baidubce.com/oauth/2.0/token?client_id={$this->config['appkey']}&client_secret={$this->config['appSecret']}&grant_type=client_credentials";
// $options 等同于 GuzzleHttp\Client 构造函数的 $config 参数
$options = [];
// $client 为协程化的 GuzzleHttp\Client 对象
$client = $this->clientFactory->create($options);
$res = $client->request('POST', $url,[
'headers' => ['content-type' => 'application/json', 'accept' => 'application/json']
]);
//转数组
$wxqftk = json_decode((string) $res->getBody(),true);
Cache::getIns()->set('wxqftk', $wxqftk , 2591000);//写缓存 30day - 1000m
}
return $wxqftk;
}
}
看看效果
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号