百度文心一言介绍:
文心一言(英文名:ERNIE Bot)是百度全新一代知识增强大语言模型,文心大模型家族的新成员,能够与人对话互动、回答问题、协助创作,高效便捷地帮助人们获取信息、知识和灵感。文心一言从数万亿数据和数千亿知识中融合学习,得到预训练大模型,在此基础上采用有监督精调、人类反馈强化学习、提示等技术,具备知识增强、检索增强和对话增强的技术优势。
以下为PHP下实现接口鉴权及简单对话输出
<?php
// 获取访问令牌
function get_access_token() {
$url = "https://aip.baidubce.com/oauth/2.0/token";
$params = [
'grant_type' => 'client_credentials',
'client_id' => 'YOUR_API_KEY', // 替换为您的API Key
'client_secret' => 'YOUR_SECRET_KEY' // 替换为您的Secret Key
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($params)
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false) {
die('请求失败');
}
$result = json_decode($response, true);
return $result['access_token'];
}
// 调用文心一言聊天完成接口
function call_wenxin_api($token, $message) {
$url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions";
$data = [
'messages' => [[
'role' => 'user',
'content' => $message
]]
];
$options = [
'http' => [
'header' => "Content-type: application/json\r\nAuthorization: Bearer " . $token,
'method' => 'POST',
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false) {
die('请求失败');
}
return json_decode($response, true);
}
$token = get_access_token();
$result = call_wenxin_api($token, "介绍一下你自己");
print_r($result);
?>
周涛博客







评论前必须登录!
注册