• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

微信小程序实现调用百度文心一言接口API可直接运行代码

武飞扬头像
Achau
帮助1

直接贴出运行结果

学新通

1、首先得获得你自己的API_key

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

学新通

2、进入文心千帆大模型平台

进入文心千帆大模型平台,开通四个服务,如下,开通是免费的,付费随时可以中止。

学新通

3、上代码运行

然后,就直接上这段代码就ok,不过记得要换成自己的API_key和Secret_key。

Page({
  onLoad: function () {
    this.getToken();
  },
 
  getToken(){
    const url_token = "https://aip.百度bce.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.百度bce.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);
            },
          });
      },
    });
  },
});

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkgcka
系列文章
更多 icon
同类精品
更多 icon
继续加载