手把手做一个公众号GPT智能客服(七)GPT 接入微信机器人

简介: 手把手做一个公众号GPT智能客服(七)GPT 接入微信机器人

第七课:GPT 接入微信机器人

OpenAI API 调动控制器代码

// /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
        //...
      }
      //...
  }
}
//...

语音识别接入

  1. 开启语音识别能力

  2. 解析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 –

目录
相关文章
|
6月前
|
Kubernetes 安全 机器人
私密离线聊天新体验!llama-gpt聊天机器人:极速、安全、搭载Llama 2,尽享Code Llama支持!
私密离线聊天新体验!llama-gpt聊天机器人:极速、安全、搭载Llama 2,尽享Code Llama支持!
私密离线聊天新体验!llama-gpt聊天机器人:极速、安全、搭载Llama 2,尽享Code Llama支持!
|
4月前
|
自然语言处理
手把手做一个公众号GPT智能客服(六)GPT 调用
手把手做一个公众号GPT智能客服(六)GPT 调用
51 0
|
4月前
|
自然语言处理 数据可视化 NoSQL
手把手做一个公众号GPT智能客服(五)免费云数据库
手把手做一个公众号GPT智能客服(五)免费云数据库
46 0
|
4月前
|
自然语言处理
手把手做一个公众号GPT智能客服(四)公众号自定义菜单
手把手做一个公众号GPT智能客服(四)公众号自定义菜单
25 0
|
4月前
|
存储 自然语言处理 开发者
手把手做一个公众号GPT智能客服(三)客服消息
手把手做一个公众号GPT智能客服(三)客服消息
42 0
|
4月前
|
自然语言处理 网络协议 中间件
手把手做一个公众号GPT智能客服【二】实现微信公众号回复(订阅送源码!)
手把手做一个公众号GPT智能客服【二】实现微信公众号回复(订阅送源码!)
55 0
手把手做一个公众号GPT智能客服【二】实现微信公众号回复(订阅送源码!)
|
4月前
|
缓存 自然语言处理 JavaScript
手把手做一个公众号GPT智能客服【一】准备工作
手把手做一个公众号GPT智能客服【一】准备工作
157 0
|
4月前
|
人工智能 JavaScript 前端开发
使用 Node.js、Socket.IO 和 GPT-4 构建 AI 聊天机器人
使用 Node.js、Socket.IO 和 GPT-4 构建 AI 聊天机器人
99 0
|
8月前
|
机器学习/深度学习 人工智能 自然语言处理
像GPT-4一样能看懂图文,李飞飞等人的具身AI给机器人造了个多模态对话框
像GPT-4一样能看懂图文,李飞飞等人的具身AI给机器人造了个多模态对话框
113 0
|
11月前
|
自然语言处理 达摩院 机器人
带你读《达摩院智能客服知识运营白皮书》——4.3 处理知识语料,提升机器人问答效果
带你读《达摩院智能客服知识运营白皮书》——4.3 处理知识语料,提升机器人问答效果
93 0