UNPKG

bdwx-nodejs

Version:

#### 介绍 文心一言 node sdk

68 lines (66 loc) 2.75 kB
const { getWXTk, botChatFn } = require('../utils/index'); class BdwxClient { options = { secretKey: '', APIKey: '' }; access_token = ''; constructor(myoptions) { this.options = Object.assign(this.options, myoptions); if (!this.options.APIKey || !this.options.secretKey) { return console.error('secretKey or APIKey is not set'); }; this.init(); }; async init() { this.access_token = await getWXTk(this.options.secretKey, this.options.APIKey); if (!this.access_token) { return console.error('init get access_token error'); }; }; turboChat(messages = '', stream = false, response = '', nodostream = false) { return this.comChat(messages, stream, response, 'turboChat', nodostream); }; botChat(messages = '', stream = false, response = '', nodostream = false) { return this.comChat(messages, stream, response, 'botChat', nodostream); }; bot8kChat(messages = '', stream = false, response = '', nodostream = false) { return this.comChat(messages, stream, response, 'bot8kChat', nodostream); }; bot7bChat(messages = '', stream = false, response = '', nodostream = false) { return this.comChat(messages, stream, response, 'bot7bChat', nodostream); }; bot4TurboChat(messages = '', stream = false, response = '', nodostream = false) { return this.comChat(messages, stream, response, 'bot4TurboChat', nodostream); }; bot4Chat(messages = '', stream = false, response = '', nodostream = false) { return this.comChat(messages, stream, response, 'bot4Chat', nodostream); }; comChat(messages = '', stream = false, response = '', model = 'turboChat', nodostream = false) { return new Promise(async (resolve, reject) => { botChatFn({ question: messages, stream, nodostream, response, secretKey: this.options.secretKey, APIKey: this.options.APIKey }, model, result => { let resObj = { model, stream, tips: stream ? '当前是steam模式,已直接进行response.write,当前返回数据可以用于上下文对话' : '当前是json模式,当前返回数据可自行响应给客户端', result } // console.log("turboChat result:", result); resolve && resolve(resObj); }, error => { // console.log("turboChat err:", error); reject && reject(error); }) }); } } module.exports = { BdwxClient }