const dayjs = require('dayjs')
const crypto = require('crypto');
const WebSocket = require('ws')
const APIKey = 'f0a2be46b8bdasdadasdasdasdasdasdcb'
const APISecret = 'OWU5NTA2MIHSADHZiYmM5NzI5OGUxYmUx'
const APPID = 'asda123xas'
const UID = 'test'
const host = 'spark-api.xf-yun.com'
const req_path = '/v1.1/chat'
const gmtDate = new Date().getTime() + ((new Date().getTimezoneOffset() / 60) * 60 * 60 * 1000)
const date = dayjs(gmtDate).format('ddd[,] DD MMM YYYY HH:mm:ss [GMT]')
// 利用上方的date动态拼接生成字符串tmp
const tmp = `host: ${host}\ndate: ${date}\nGET ${req_path} HTTP/1.1`
// 利用hmac-sha256算法结合APISecret对上一步的tmp签名,获得签名后的摘要tmp_sha
const tmp_sha = crypto.createHmac('sha256', APISecret).update(tmp).digest();
// 将上方的tmp_sha进行base64编码生成signature
const signature = tmp_sha.toString('base64')
// 利用上面生成的signature,拼接下方的字符串生成authorization_origin
const authorization_origin = `api_key="${APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${signature}"`
// 最后再将上方的authorization_origin进行base64编码,生成最终的authorization
const authorization = Buffer.from(authorization_origin).toString('base64')
// 将鉴权参数组合成最终的键值对
const url = `wss://${host}${req_path}?authorization=${encodeURIComponent(authorization)}&date=${encodeURIComponent(date).replace(new RegExp('%20','g'), '+')}&host=${encodeURIComponent(host)}`
const ws = new WebSocket(url)
ws.on('error', (err) => {
console.log(err, '错误');
});
ws.on('open', function open() {
ws.send(JSON.stringify({
header: {
app_id: APPID,
uid: UID
},
parameter: {
chat: {
domain: "general",
temperature: 0.5,
max_tokens: 4096,
}
},
payload: {
message: {
text: [
{ role: 'user', 'content': '长白山适合什么时候去游玩,且有那可以景点推荐呢?' }
]
}
}
}));
});
ws.on('message', function message(data) {
const obj = JSON.parse(data)
const texts = obj.payload.choices.text
texts.forEach(item => {
console.log(item.content);
})
});
NodeJS生成讯飞星火认知大模型api通用鉴权URL
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 微软Azure平台的语音合成(TTS)技术确实神乎其技,这一点在之前的一篇:含辞未吐,声若幽兰,史上最强免费人工智...