请登录后探索更多精彩内容!
百度AI千帆官方没有提供PHP代码,下面是自己写的PHP版本:
<?php
//error_reporting(0);
header('Content-type: application/json');
$ak = '自己的AK';
$sk = '自己的SK';
if (isset($_POST['info']) && trim($_POST['info']) !== '') {
$info = $_POST['info'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='.$ak.'&client_secret='.$sk);
curl_setopt($curl, CURLOPT_TIMEOUT,5);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($curl);
curl_close($curl);
$response = json_decode($data, true);
if (json_last_error() !== JSON_ERROR_NONE || !isset($response['access_token']) || empty($response['access_token'])) {
echo json_encode('access_token');exit;
}
$accessToken = $response['access_token'];
$url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" . urlencode($accessToken);
// 要发送的JSON数据
$jsonData = '{
"messages": [{
"role": "user",
"content": $info,
"key": "1"
}],
"temperature": 0.8,
"top_p": 0.8,
"penalty_score": 1,
"disable_search": false,
"enable_citation": false,
"collapsed": true
}';
// 初始化cURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($curl);
//print_r($response);exit;
$response = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE || !isset($response['result']) || empty($response['result'])) {
echo json_encode('获取result失败');exit;
}else{
echo json_encode($response['result']);exit;
} 其中$_POST['info']是提交的问题或者对话内容,$response['result']是接口返回的内容。
暂无评论
请先登录后发表评论!
暂无评论