PHP对接deepseek,百度文心一言,kimi,抖音豆包ai,阿里通义千问 代码示例(非流式) - '
/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */
#content_views .toc,
/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */
#content_views.markdown_views > p:empty:has(+ .toc),
#content_views.markdown_views > .toc + p:empty,
/* 富文本旧版目录标记 */
#content_views.htmledit_views #main-toc,
#content_views.htmledit_views #hr-toc,
#content_views.htmledit_views p[id*="-toc"] {
display: none !important;
}
/* 目录去掉后,紧跟的首个标题不再多出一块上边距 */
#content_views.markdown_views > .toc + h1,
#content_views.markdown_views > .toc + h2,
#content_views.markdown_views > .toc + h3,
#content_views.markdown_views > .toc + h4,
#content_views.markdown_views > .toc + p:empty + h1,
#content_views.markdown_views > .toc + p:empty + h2,
#content_views.markdown_views > .toc + p:empty + h3,
#content_views.markdown_views > .toc + p:empty + h4 {
margin-top: 0 !important;
}
deepseek
public function apiDeepSeek($content='',$file_id='',$uid='',$id='')
{
// $msgList = self::getRecentRecords($content,$uid,$id);
$msgList=[
['role'=>'system','content'=>'你是孙牛逼AI,由deepseek AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 AI 为专有名词,不可翻译成其他语言。'],
];
$msgList[]=['role'=>'user','content'=>$content];
$data = [
"model" =>"deepseek-reasoner",
// 'stream' => true,
// 'temperature'=>0.3,
// 'user_id'=>$uid,
"messages" => $msgList
];
$url="https://api.deepseek.com/chat/completions";
//发送请求
$apiKey = ' ';
$ch = curl_init();
// $fileHandle='';
// curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
// curl_setopt($ch, CURLOPT_PROXYUSERPWD, "ayame:123Ayame"); //http代理认证帐号,username:password的格式
// curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
// curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
// 'Content-Type: application/json',
//
//
// ]); //设置请求头
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;',
'Accept: application/json',
// "Connection: close\r\n",
'Authorization: Bearer ' . $apiKey,
));
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名
curl_setopt($ch, CURLOPT_SSLVERSION, 0); //传递一个包含SSL版本的长参数。
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
// curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHand, 'callback']);
$result = curl_exec($ch);
curl_close($ch);
$result=json_decode($result, true);
$msgData = $result['choices'][0]['message']['content'];
}
百度文心一言
/**
* 使用 AK,SK 生成鉴权签名(Access Token)
* @return string 鉴权签名信息(Access Token)
*/
public function getAccessToken(){
$curl = curl_init();
$postData = array(
'grant_type' => 'client_credentials',
'client_id' =>' ',
'client_secret' => ' '
);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => http_build_query($postData)
));
$response = curl_exec($curl);
curl_close($curl);
$rtn = json_decode($response);
return $rtn->access_token;
}
public function apiBaiDu($content,$uid,$id)
{
//$msgList = self::getRecentRecords($content,$uid,$id);
$data = [
// "user_id" =>"1002",
// 'stream' => true,
'user_id'=>$uid,
"messages" => $msgList
];
$url="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token={$this->getAccessToken()}";
// dump($url);die;
//发送请求
$ch = curl_init();
// $fileHandle='';
// curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
// curl_setopt($ch, CURLOPT_PROXYUSERPWD, "ayame:123Ayame"); //http代理认证帐号,username:password的格式
// curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
// curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]); //设置请求头
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名
curl_setopt($ch, CURLOPT_SSLVERSION, 0); //传递一个包含SSL版本的长参数。
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
//curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHandler, 'callback']);
$result = curl_exec($ch);
curl_close($ch);
}
kimi
public function apiKimi($content='',$file_id='',$uid='',$id='')
{
$msgList=[
['role'=>'system','content'=>'你是 孙牛逼AI,由kimi AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 AI 为专有名词,不可翻译成其他语言。'],
];
$msgList[]=['role'=>'user','content'=>$content];
$data = [
"model" =>"moonshot-v1-128k",
//'stream' => true,
'temperature'=>0.3,
// 'user_id'=>$uid,
"messages" => $msgList
];
$url="https://api.moonshot.cn/v1/chat/completions";
//发送请求
$apiKey = '';
$ch = curl_init();
// $fileHandle='';
// curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
// curl_setopt($ch, CURLOPT_PROXYUSERPWD, "ayame:123Ayame"); //http代理认证帐号,username:password的格式
// curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
// curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
// 'Content-Type: application/json',
//
//
// ]); //设置请求头
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Bearer ' . $apiKey, // 如果 Moonshot AI 使用这种授权方式
) );
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名
curl_setopt($ch, CURLOPT_SSLVERSION, 0); //传递一个包含SSL版本的长参数。
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
// curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHand, 'callback']);
$result = curl_exec($ch);
curl_close($ch);
}
豆包
public function apiDouBao($content='',$file_id='',$uid='',$id='')
{
$apiUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
$apiKey = ""; // 替换为实际API密钥
$postData = [
'model' => 'doubao-seed-1.6-250615',
'messages' => [
[
'role' => 'user',
'content' => $content,
]
],
// 'stream' => true // 启用流式响应
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;',
'Accept: application/json',
// "Connection: close\r\n",
'Authorization: Bearer ' . $apiKey, // 如果 Moonshot AI 使用这种授权方式
));
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名
curl_setopt($ch, CURLOPT_SSLVERSION, 0); //传递一个包含SSL版本的长参数。
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
// curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHand, 'callback']);
// curl_close($ch);
$result = curl_exec($ch);
curl_close($ch);
}
通义千问
public function apiTongYi($content='',$file_id='',$uid='',$id='')
{
// 设置请求的URL
$url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions'; 拼多多帮助力入口
// 若没有配置环境变量,请用百炼API Key将下行替换为:$apiKey = "sk-xxx";
$apiKey = '';
$msgList=[
['role'=>'system','content'=>'你是孙牛逼AI,由通义千问 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 AI 为专有名词,不可翻译成其他语言。'],
];
$msgList[]=['role'=>'user','content'=>$content];
// 设置请求头
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
];
// 设置请求体
$data = [
"model" => "qwen-plus",
"messages" =>$msgList
];
// 初始化cURL会话
$ch = curl_init();
// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 执行cURL会话
$response = curl_exec($ch);
curl_close($ch);
}