Python调用Unit闲聊对话API的应用

简介:利用百度大脑平台(百度智能对话定制与服务平台,即UNIT)提供的API接口,实现可以完成闲聊功能的智能对话机器人

1 机器人的创建

访问百度大脑网址:https://ai.baidu.com/unit/home
点击进入UNIT
在这里插入图片描述
然后登陆百度账号,进入到以下页面,点击我的机器人:
在这里插入图片描述
点击“+”号创建机器人,我们设置机器人名称为“闲聊机器人”,描述里写“闲聊”,然后创建:
在这里插入图片描述
点击新创建好的机器人界面,点击技能管理界面:
在这里插入图片描述
然后点击蓝字“技能管理页”,再点击“添加预置技能”,拉到最下面,找到闲聊功能并创建
在这里插入图片描述
然后新建我的闲聊技能,命名为“闲聊机器人”
在这里插入图片描述
然后回到我们的“我的机器人”界面中的技能管理,添加技能,我们可以看到我们创建的“闲聊机器人”功能,将其添加至机器人即可。
在这里插入图片描述
然后点击“发布上线”中的“研发/生产环境”,点击蓝色按钮“获取API Key/Secret Key
在这里插入图片描述
我们可以看到如下界面,其中有两个重要的参数——API Key和Secret Key
在这里插入图片描述
以上过程我们创建好了闲聊机器人,下面我们需要做的就是在Python中调用该机器人的API接口,实现闲聊功能。

2 Python调用API的代码实现

导入相关包:

import json
import random
import requests

设置好相关参数。这里的id对应的即上面的API Key,secret对应的是上面的Secret Key

client_id = "byKLF3DzDpYGQDPifrymq14j"
client_secret = "dZGTviCbBKr8FWzo2HjL7Qs5LMGFAki1"

编写函数unit_chat。其功能是调用百度大脑UNIT接口。包括两个参数:chat_input代表的是用户输入的内容,user_id表示用户的id号,我们默认为“88888”;函数的返回值是闲聊机器人的回复内容。

def unit_chat(chat_input, user_id="88888"):
    # 设置默认回复
    chat_reply = "不好意思,我正在学习中,随后回复你"
	# 固定的url格式
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"%(client_id, client_secret)
    res = requests.get(url)
    access_token = eval(res.text)["access_token"]
    unit_chatbot_url = "https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=" + access_token
    # 拼装聊天接口对应请求
    post_data = {
                    "log_id": str(random.random()),  #登陆的id,是什么不重要,我们用随机数生成一个id即可
                    "request": {
                        "query": chat_input,  #用户输入的内容
                        "user_id": user_id  #用户id
                    },
                    "session_id": "",
                    "service_id": "S53842",  #!!!!这个很重要,必须对应我们创建的机器人的id号,id号在百度大脑中我们创建的闲聊机器人中可见
                    "version": "2.0"
                }
    # 将聊天接口对应请求数据转为json数据
    res = requests.post(url=unit_chatbot_url, json=post_data)
    # 获取聊天接口返回数据
    unit_chat_obj = json.loads(res.content)
    # 判断聊天接口返回数据是否出错(error_code == 0则表示请求正确)
    if unit_chat_obj["error_code"] != 0:
        return chat_reply
    # 解析聊天接口返回数据,找到返回文本内容 result -> response_list -> schema -> intent_confidence(>0) -> action_list -> say
    unit_chat_obj_result = unit_chat_obj["result"]
    unit_chat_response_list = unit_chat_obj_result["response_list"]
    # 随机选取一个"意图置信度"[+response_list[].schema.intent_confidence]不为0的技能作为回答
    unit_chat_response_obj = random.choice(
        [unit_chat_response for unit_chat_response in unit_chat_response_list if
         unit_chat_response["schema"]["intent_confidence"] > 0.0])
    unit_chat_response_action_list = unit_chat_response_obj["action_list"]
    unit_chat_response_action_obj = random.choice(unit_chat_response_action_list)
    unit_chat_response_say = unit_chat_response_action_obj["say"]
    return unit_chat_response_say

测试我们的闲聊机器人。我们建立一个while循环,当用户输入Bye或bye后退出循环。

if __name__ == "__main__":
    while True:
        chat_input = input("请输入:")
        if chat_input == 'Bye' or chat_input == "bye":
            break
        chat_reply = unit_chat(chat_input)
        print("Unit:", chat_reply)

测试结果如下:
在这里插入图片描述

  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值