赞
踩
// /controller/openai.js const { Configuration, OpenAIApi } = require("openai") const apiKey = "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" const configuration = new Configuration({ apiKey }) const openAIApi = new OpenAIApi(configuration) /** * 保存用户发送到chatgpt的消息信息 * prompts 数据结构: * { * wxopenid: [ * { role: "user", content: Content } * ] * } */ const prompts = {} // 处理给chatgpt的数据保存记忆体 function savePrompt(fromusername, content) { if (prompts[fromusername]) { // 存在添加数据 prompts[fromusername].push( { role: 'user', content } ) } else { // 不存在就新建数据 prompts[fromusername] = [ { role: 'user', content } ] } return prompts[fromusername] } async function getAnswer(fromusername, prompt) { const messages = savePrompt(fromusername, prompt) const completion = await openAIApi.createChatCompletion({ model: "gpt-3.5-turbo", messages: messages, }) return completion.data.choices[0].message.content } module.exports = getAnswer
// /controller/switcher.js const { customerController } = require('./customer') // +++++++++++++++++++ const getAnswer = require('./openai') // +++++++++++++++++++ const { getModel, decreaseBalance, getBalance } = require('../model/users') const { formatMsg } = require('../utils/format') async function messageController(req, res, next) { // 接收到微信方发来的消息并处理 const msg = formatMsg(req.body) // 暂存用户发送来的问题 const content = msg['content'] // 回复信息 const { fromusername } = msg msg['createtime'] = Math.floor((new Date().getTime()) / 1000) // 生成问题答案逻辑 const model = await getModel(fromusername) if (model && model === 'chatgpt') { const balance = await getBalance(fromusername) if (balance > 0) { msg['content'] = '答案正在准备中...' res.render('reply', msg) // 扣除账户金额 1 await decreaseBalance(fromusername) // +++++++++++++++++++ // 获取 chatgpt 的回答 const answer = await getAnswer(fromusername, content) // +++++++++++++++++++ // 发送客服消息 await customerController(fromusername, answer) } else { msg['content'] = '您的账户余额不足,请充值~' res.render('reply', msg) } } else { // 转人工服务 } } module.exports = messageController
1、添加设置 model
的方法
// /model/users.js //... // 更改用户模式 async function changeModel(wxOpenId, model) { const result = await userModel.findOneAndUpdate({ wxOpenId }, { model}) return result } //... module.exports = { //... changeModel }
2、语言模型切换
// /controller/swither.js //... const { getBalance, rechargeAccount, getUser, createAccount, changeModel } = require('../model/users') async function switcherController(req, res) { //... switch (requestBody.msgtype) { case 'event': //... switch (requestBody.eventkey) { //... case 'model_chatgpt': await changeModel(fromusername, 'chatgpt') res.render('reply', { ...requestBody, content: '你的身份已经切换为 ChatGPT 模式,机器人为您提供答疑服务 ☕️' }) break case 'model_human': await changeModel(fromusername, 'human') res.render('reply', { ...requestBody, content: '你的身份已经切换为人工服务模式,很高兴为你答疑解惑 ☕️' }) break //... } //... } } //...
开启语音识别能力
解析voice数据 和 文本相同处理
(1)添加 voice
类型入口
//...
async function switcherController(req, res) {
//...
switch (requestBody.msgtype) {
case 'voice':
case 'text':
//...
}
}
//...
(2)处理语音信息
// /controller/message.js
//...
async function messageController(req, res, next) {
//...
// 暂存用户发送来的问题
const content = msg['recognition'] ? msg['recognition'] : msg['content']
//...
}
//...
– THE END –
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/100519
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。