直接贴出运行结果
总共有三个步骤,过程都很简单。
1、首先得获得你自己的API_key
先进入文心千帆大模型平台,然后点击左边一列的“应用接入”,然后点创建应用,这时你就获得了API_key了。
2、进入文心千帆大模型平台,开通四个服务
如下,开通是免费的,付费随时可以中止。
3、然后,就直接上这段代码就ok
不过记得要换成自己的API_key和Secret_key。
(如果报错 “链接不在以下 request 合法域名” ,可以参考这篇文章开发微信小程序时,提示不在以下 request 合法域名列表中怎么办_微信小程序不合法域名_代码搬运媛的博客-CSDN博客)
Page({ onLoad: function () { this.getToken(); }, getToken(){ const url_token = "https://aip.baidubce.com/oauth/2.0/token?client_id="(这里填API_Key)"&client_secret="(这里填Secret_key)"&grant_type=client_credentials"; wx.request({ url: url_token, method: "POST", header: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, success:res=>{ const access_token = res.data.access_token const url_chat = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + access_token const payload = { "messages": [ { "role": "user", "content": "你好" } ] }; wx.request({ url: url_chat, method: "POST", data: payload, header: { 'Content-Type': 'application/json' }, success: function (res) { console.log(res.data); }, }); }, }); }, });
其它不懂得的可以参考这篇文章百度文心一言API申请和python/Go使用教程 - 知乎