讯飞星火大模型3.0接口测试(nodejs)

        昨天是10.24程序员节,适逢讯飞星火大模型3.0接口上线,看了一下接口文档,没有找到nodejs能直接用的demo。下面就和我一起,参照官方文档进行逐步的对接。部分代码的写法有参考接口文档上的JavaScript demo。

一、通用鉴权URL生成

文档在此

 文档上用的是python代码,我们使用nodejs进行改写

const crypto = require('crypto');
const ws = require("ws");

const dateString = new Date().toGMTString(); //Wed, 25 Oct 2023 08:57:45 GMT
const host = 'spark-api.xf-yun.com';
const path = '/v3.1/chat';
let tmp = `host: ${host}
date: ${dateString}
GET ${path} HTTP/1.1`;//后两行代码不能缩进,必须顶格,否则会输出401错误
const APISecret = 'MjlmNzkzNmZkMDQ2OTc0ZDdmNGE2ZTZi'; //需改成自己的APISecret

let signature = crypto.createHmac('sha256', APISecret)
	.update(tmp)
	.digest('base64');

console.log('signature', signature);


const APIKey = 'addd2272b6d8b7c8abdd79531420ca3b'; //需改成自己的APIKey
const authorization_origin =
	`api_key="${APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${signature}"`;

let buff = Buffer.from(authorization_origin);
const authorization = buff.toString('base64');
console.log('authorization', authorization);
console.log('--------------');
const signUrl =
	`wss://${host}${path}?authorization=${authorization}&date=${encodeURIComponent(dateString)}&host=${host}`
console.log('signUrl', signUrl);


let sock = new ws(signUrl);
sock.on("open", function() {
	console.log("connect success !!!!");
});
sock.on("error", function(err) {
	console.log("error: ", err);
});
sock.on("close", function() {
	console.log("close");
});
sock.on("message", function(data) {
	console.log("message", data.toString());
});

上面的代码可以直接运行,最终会输出connect success !!!!

注意这两行代码不能缩进,要完全顶格,否则会输出401错误。

讯飞星火大模型3.0接口测试(nodejs)_第1张图片 

二、发送聊天内容 

文档在此 

我们对原来的代码进行调整:

sock.on("open", function() {
		console.log("connect success !!!!");

		sock.send(JSON.stringify({
			"header": {
				"app_id": '5b0278f7',//替换成自己的app_id
				// "uid": "fd3f47e4-d",//最大长度32	每个用户的id,用于区分不同用户
			},
			"parameter": {
				"chat": {
					"domain": "generalv3",
					"temperature": 0.5,
					"max_tokens": 1024, //模型回答的tokens的最大长度
				}
			},
			"payload": {
				"message": {
					"text": [{
						"role": "user",
						"content": "1加1等于几"
					}, ]
				}
			}
		}))
	});

上面的代码如果直接执行,会报错: 

{"header":{"code":10005,"message":"InvalidParamError:(17:15:17.428) app_id is not same to kong app_id","sid":"cid000b592e@dx18b661de604b81d550","status":2}}

必须将APISecret、APIKey、app_id改为自己的。

改完这些值,执行代码,正确的输出为:

connect success !!!!
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":0},"payload":{"choices":{"status":0,"seq":0,"text":[{"content":"1","role":"assistant","index":0}]}}}
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":1,"text":[{"content":"加1","role":"assistant","index":0}]}}}
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":3,"text":[{"content":"解答过程如下:","role":"assistant","index":0}]}}}
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":4,"text":[{"content":"\n\n","role":"assistant","index":0}]}}}
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":5,"text":[{"content":"我们有两个数,一个是1,另一个也是1。","role":"assistant","index":0}]
}}}
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":6,"text":[{"content":"我们要将这两个数相加。\n\n","role":"assistant","index":0}]}}}      
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":7,"text":[{"content":"加法的定义是将两个数的值组合在一起,得到一个新的数。","role":"assis
tant","index":0}]}}}
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":8,"text":[{"content":"在这个例子中,我们将1与1组合在一起。\n\n","role":"assistant","index
":0}]}}}
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":1},"payload":{"choices":{"status":1,"seq":9,"text":[{"content":"根据加法规则,$1+1$ $=2$。\n\n","role":"assistant","index":0}]}}}  
message {"header":{"code":0,"message":"Success","sid":"cht000b5d4d@dx18b661fc098b81d550","status":2},"payload":{"choices":{"status":2,"seq":10,"text":[{"content":"所以,1加1等于2。","role":"assistant","index":0}]},"usage":{"text"
:{"question_tokens":14,"prompt_tokens":14,"completion_tokens":116,"total_tokens":130}}}}
close

可以看到,这些是流式输出,我们如果有开发前端应用,需要将其拼接好统一返回。

你可能感兴趣的:(chatgpt,nodejs,javascript,前端,开发语言,chatgpt)