小程序模板:专业的小程序模板与静态模板分享平台
小程序
教程
搜索
当前位置 : 首页> 小程序教程> 微信小程序实现调用百度文心一言接口API

微信小程序实现调用百度文心一言接口API

要在微信小程序中调用百度文心一言接口API,你可以使用小程序的内置API wx.request 发起HTTP请求获取接口数据_微信小程序调用文心一言API

 
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);
            },
          });
      },
    });
  },
});


联系客服 意见反馈

签到成功!

已连续签到1天,签到3天将获得积分VIP1天

知道了