예제 #1
0
    def test_demo(self):
        logging.info("示例程序")
        '''
        自定义词典
        '''
        # 创建自定义词典
        data = Data()
        data.customdict = CustomDict(name=customDictName, chatbotID=chatbot_id)
        resp = self.bot.postCustomDict(data)
        logging.info("postCustomDict response: %s", resp)

        # 更新自定义词典
        data = Data()
        data.chatbotID = chatbot_id
        data.customdict = CustomDict(name=customDictName, chatbotID=chatbot_id)
        data.dictword = DictWord(word="西红柿", synonyms="狼桃;柿子;番茄")
        resp = self.bot.putDictWord(data)
        logging.info("putDictWord response: %s", resp)

        # 引用系统词典
        data = Data()
        data.chatbotID = chatbot_id
        data.sysdict = SysDict(name="@TIME")
        resp = self.bot.refSysDict(data)
        logging.info("refSysDict response: %s", resp)
        '''
        意图管理
        '''
        # 创建意图
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        resp = self.bot.postIntent(data)
        logging.info("postIntent response: %s", resp)

        # 创建意图槽位: 配菜
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.slot = IntentSlot(name="vegetable",
                               requires=True,
                               question="您需要什么配菜")
        data.customdict = CustomDict(chatbotID=chatbot_id, name=customDictName)
        resp = self.bot.postSlot(data)
        logging.info("postSlot response: %s", resp)

        # 创建意图槽位: 送达时间
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.slot = IntentSlot(name="date",
                               requires=True,
                               question="您希望什么时候用餐")
        data.sysdict = SysDict(name="@TIME")
        resp = self.bot.postSlot(data)
        logging.info("postSlot response: %s", resp)

        # 创建意图槽位: 送达位置
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.slot = IntentSlot(name="location",
                               requires=True,
                               question="外卖送到哪里")
        resp = self.bot.postSlot(data)
        logging.info("postSlot response: %s", resp)

        # 添加意图说法
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.utter = IntentUtter(utterance="帮我来一份{vegetable},送到{location}")
        resp = self.bot.postUtter(data)
        data.utter = IntentUtter(utterance="我想点外卖")
        resp = self.bot.postUtter(data)
        # logging.info("postUtter response: %s", resp)
        '''
        训练机器人        
        '''
        data = Data()
        data.chatbotID = chatbot_id
        resp = self.bot.train(data)
        logging.info("train response: %s", resp)

        ## 训练是一个常时间任务,进行异步反馈
        while True:
            sleep(3)
            data = Data()
            data.chatbotID = chatbot_id
            resp = self.bot.status(data)
            if resp.rc == 0:
                break
        '''
        对话
        '''
        # 创建session
        ## 关于session的说明,参考 https://dwz.cn/wRFrELrq
        data = Data()
        data.session = ChatSession(
            chatbotID=chatbot_id,
            uid="py",  # 用户唯一的标识
            channel="testclient",  # 自定义,代表该用户渠道由字母组成
            branch="dev"  # 测试分支,有连个选项:dev, 测试分支;pro,生产分支
        )
        sessionId = self.bot.putSession(data).session.id
        logging.info("putSession response: %s", resp)

        # 对话
        data = Data()
        data.session = ChatSession(id=sessionId)
        text = "我想点外卖,来一份番茄"
        logging.info("chat human: %s", text)
        data.message = ChatMessage(textMessage=text)
        resp = self.bot.chat(data)
        logging.info("chat bot: %s \n 意图: %s", resp.message.textMessage,
                     resp.session)

        text = "我想在下午三点用餐"
        logging.info("chat human: %s", text)
        data.message = ChatMessage(textMessage=text)
        resp = self.bot.chat(data)
        logging.info("chat bot: %s \n 意图: %s", resp.message.textMessage,
                     resp.session)
예제 #2
0
    def test_demo(self):
        logging.info("示例程序")
        '''
        自定义词典
        '''
        # 创建自定义词典(词表类型)
        data = Data()
        data.customdict = CustomDict(name=customDictVocab,
                                     chatbotID=chatbot_id,
                                     type="vocab")
        resp = self.bot.postCustomDict(data)
        logging.info("postCustomDict response: %s", resp)

        # 更新自定义词典
        data = Data()
        data.chatbotID = chatbot_id
        data.customdict = CustomDict(name=customDictVocab,
                                     chatbotID=chatbot_id)
        data.dictword = DictWord(word="西红柿", synonyms="狼桃;柿子;番茄")
        resp = self.bot.putDictWord(data)
        logging.info("putDictWord response: %s", resp)

        # 创建自定义词典(正则表达式类型)
        data = Data()
        data.customdict = CustomDict(name=customDictPattern,
                                     chatbotID=chatbot_id,
                                     type="regex")
        resp = self.bot.postCustomDict(data)
        logging.info("postCustomDict response: %s", resp)

        # 定义正则表达式词典
        data = Data()
        data.customdict = CustomDict(name=customDictPattern,
                                     chatbotID=chatbot_id)
        data.dictpattern = DictPattern(patterns=[customDictPatternDef])
        resp = self.bot.putDictPattern(data)
        logging.info("putDictPattern response: %s", resp)

        # 测试正则表达式词典
        data = Data()
        data.customdict = CustomDict(name=customDictPattern,
                                     chatbotID=chatbot_id)
        data.patterncheck = DictPatternCheck(input="我的订单是OR222,请送到清华西门。")
        resp = self.bot.checkDictPattern(data)
        logging.info("checkDictPattern response: %s", resp)

        # 引用系统词典
        data = Data()
        data.chatbotID = chatbot_id
        data.sysdict = SysDict(name="@TIME")
        resp = self.bot.refSysDict(data)
        logging.info("refSysDict response: %s", resp)

        data = Data()
        data.chatbotID = chatbot_id
        data.sysdict = SysDict(name="@LOC")
        resp = self.bot.refSysDict(data)
        logging.info("refSysDict response: %s", resp)
        '''
        意图管理
        '''
        # 创建意图
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        resp = self.bot.postIntent(data)
        logging.info("postIntent response: %s", resp)

        # 创建意图槽位: 配菜
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.slot = IntentSlot(name="vegetable",
                               requires=True,
                               question="您需要什么配菜")
        data.customdict = CustomDict(chatbotID=chatbot_id,
                                     name=customDictVocab)
        resp = self.bot.postSlot(data)
        logging.info("postSlot response: %s", resp)

        # 创建意图槽位:订单ID
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.slot = IntentSlot(name="orderId",
                               requires=True,
                               question="您的订单ID是什么?")
        data.customdict = CustomDict(chatbotID=chatbot_id,
                                     name=customDictPattern)
        resp = self.bot.postSlot(data)
        logging.info("postSlot response: %s", resp)

        # 创建意图槽位: 送达时间
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.slot = IntentSlot(name="date",
                               requires=False,
                               question="您希望什么时候用餐")
        data.sysdict = SysDict(name="@TIME")
        resp = self.bot.postSlot(data)
        logging.info("postSlot response: %s", resp)

        # 创建意图槽位: 送达位置
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.slot = IntentSlot(name="location",
                               requires=True,
                               question="外卖送到哪里")
        data.sysdict = SysDict(name="@LOC")
        resp = self.bot.postSlot(data)
        logging.info("postSlot response: %s", resp)

        # 添加意图说法
        data = Data()
        data.intent = Intent(chatbotID=chatbot_id, name=intent_name)
        data.utter = IntentUtter(utterance="帮我来一份{vegetable},送到{location}")
        resp = self.bot.postUtter(data)
        data.utter = IntentUtter(utterance="我想点外卖")
        resp = self.bot.postUtter(data)
        data.utter = IntentUtter(utterance="我的订单是{orderId},帮我送到{location}")
        resp = self.bot.postUtter(data)
        # logging.info("postUtter response: %s", resp)
        '''
        训练机器人        
        '''
        data = Data()
        data.chatbotID = chatbot_id
        resp = self.bot.train(data)
        logging.info("train response: %s", resp)

        if resp.rc == 22:
            sys.exit()

        ## 训练是一个长时间任务,进行异步反馈
        while True:
            sleep(3)
            data = Data()
            data.chatbotID = chatbot_id
            resp = self.bot.status(data)
            logging.info("train response: %s", resp)
            if resp.rc == 0:
                break
        '''
        对话
        '''
        # 创建session
        ## 关于session的说明,参考 https://dwz.cn/wRFrELrq
        data = Data()
        data.session = ChatSession(
            chatbotID=chatbot_id,
            uid="py",  # 用户唯一的标识
            channel="testclient",  # 自定义,代表该用户渠道由字母组成
            branch="dev"  # 测试分支,有连个选项:dev, 测试分支;pro,生产分支
        )
        sessionId = self.bot.putSession(data).session.id
        logging.info("putSession response: %s", resp)

        # 对话
        data = Data()
        data.session = ChatSession(id=sessionId)

        text = "我想点外卖,来一份番茄"
        logging.info("chat human: %s", text)
        data.message = ChatMessage(textMessage=text)
        resp = self.bot.chat(data)
        logging.info("chat bot: %s \n 意图: %s", resp.message.textMessage,
                     resp.session)

        text = "我的订单是OR666,帮我送到知春路10号"
        logging.info("chat human: %s", text)
        data.message = ChatMessage(textMessage=text)
        resp = self.bot.chat(data)
        logging.info("chat bot: %s \n 意图: %s", resp.message.textMessage,
                     resp.session)