1、首先得获得你自己的API_key
先进入文心千帆大模型平台,然后点击左边一列的“应用接入”,然后点创建应用,这时你就获得了API_key了。

2、第二部就直接上这段代码就ok,不过记得要换成自己的API_key和Secret_key。
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_tokenconst url_chat = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + access_tokenconst 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使用教程 - 知乎


















