AI大模型教程之千帆大模型平台API调用

AI大模型教程之千帆大模型平台API调用视频详解 :

 

【应用开发】分别用nodejs python golang Opne Ai调用 ChatGLM 智谱AI大模型的Api

文本介绍:

 

1. 创建千帆应用 。根据实际需求创建千帆应用,创建成功后,获取 AppID API Key Secret Key 等信息。如果已有千帆应用,可以查看已有应用的API Key Secret Key 等信息。

 

2. API 授权 。应用创建成功后,千帆平台默认为应用开通所有 API 调用权限,无需申请授权。

 

3. 获取接口访问凭证 access_token 。根据第 1 步获取的 API Key Secret Key ,调用获取
access_token 接口获取 access_token ,通过 access_token 鉴权调用者身份。

 

4. 调用 API 接口 。例如调用 ERNIE 相关接口

 

千帆大模型官网地址 https://qianfan.cloud.baidu.com/

 

进入控制台可以找到文档

 

 

创建千帆应用  

 

获取接口访问凭证 access_token

 

https://console.bce.baidu.com/support/#/api?product=QIANFAN&project=%E5%8D%83%E5%B8%
86%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%B9%B3%E5%8F%B0&parent=%E9%89%B4%E6%9
D%83%E8%AE%A4%E8%AF%81%E6%9C%BA%E5%88%B6&api=oauth/2.0/token&method=post

 

 

 

官方示例调用API接口  

 

 

 

Golang  

package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
const API_KEY = "改成自己的"
const SECRET_KEY = "改成自己的"
func main() {

url :=
"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?
access_token=" + GetAccessToken()
payload := strings.NewReader(`{"messages":[{"role":"user","content":"为什么说
AI是第四次工业革
命"}],"temperature":0.95,"top_p":0.8,"penalty_score":1,"disable_search":false,"en
able_citation":false,"response_format":"text"}`)
client := &http.Client{}
req, err := http.NewRequest("POST", url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
/**
* 使用 AK,SK 生成鉴权签名(Access Token)
* @return string 鉴权签名信息(Access Token)
*/
func GetAccessToken() string {
url := "https://aip.baidubce.com/oauth/2.0/token"
postData :=
fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s",
API_KEY, SECRET_KEY)
resp, err := http.Post(url, "application/x-www-form-urlencoded",
strings.NewReader(postData))
if err != nil {
fmt.Println(err)
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return ""
}
accessTokenObj := map[string]any{}
_ = json.Unmarshal([]byte(body), &accessTokenObj)
return accessTokenObj["access_token"].(string)
}

 

 

 

Nodejs python 

const request = require('request')
const AK = "puXep1jkUrzWWpOsekNhxFCx"
const SK = "Q5SXOiJtWw1q9hbaYi71yiTZG2Xquho2"
async function main() {
var options = {
'method': 'POST',
'url':
'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?
access_token=' + await getAccessToken(),
'headers': {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"messages": [
{
"role": "user",
"content": "你叫什么"
}
],
"temperature": 0.95,
"top_p": 0.8,
"penalty_score": 1,
"disable_search": false,
"enable_citation": false,
"response_format": "text"
})
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
}
/**
* 使用 AK,SK 生成鉴权签名(Access Token)
* @return string 鉴权签名信息(Access Token)
*/
function getAccessToken() {
let options = {
'method': 'POST',
'url': 'https://aip.baidubce.com/oauth/2.0/token?
grant_type=client_credentials&client_id=' + AK + '&client_secret=' + SK,
}
return new Promise((resolve, reject) => {
request(options, (error, response) => {
if (error) { reject(error) }
else { resolve(JSON.parse(response.body).access_token) }
})
})
}
main();
其他语言参考官方 demo

 

https://console.bce.baidu.com/support/#/api?product=QIANFAN&project=%E5%8D%83%E5%B8%
86%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%B9%B3%E5%8F%B0&parent=%E9%89%B4%E6%9
D%83%E8%AE%A4%E8%AF%81%E6%9C%BA%E5%88%B6&api=oauth/2.0/token&method=post

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值