ChatBot闲聊---腾讯自然语言处理 NLP

https://cloud.tencent.com/document/product/271/39416

  1. 接口描述
    接口请求域名: nlp.tencentcloudapi.com 。

闲聊服务基于腾讯领先的NLP引擎能力、数据运算能力和千亿级互联网语料数据的支持,同时集成了广泛的知识问答能力,可实现上百种自定义属性配置,以及儿童语言风格及说话方式,从而让聊天变得更睿智、简单和有趣。

pip install itchat
pip install tencentcloud-sdk-python
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

import itchat
import json
from turtle import clear
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.nlp.v20190408 import nlp_client, models
def get_response(questionS): #AI回复信息
    SecretId = 'XXXX'
    SecretKey = 'XXXXXX'
    try:
        cred = credential.Credential(SecretId, SecretKey)
        #cred = credential.Credential("SecretId", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "nlp.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = nlp_client.NlpClient(cred, "ap-guangzhou", clientProfile)

        req = models.ChatBotRequest()
        params = {
            "Query": questionS
        }
        req.from_json_string(json.dumps(params))
        
        resp = client.ChatBot(req)
        print(resp.to_json_string())
        j = json.loads(resp.to_json_string())
        return [j['Reply']]
    except TencentCloudSDKException as err:
        print(err)

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
    if msg['Type'] == 'Text':
        answer = ''.join(get_response(msg['Content']))
    return answer
   
if __name__ == '__main__':
    itchat.auto_login(hotReload=True)
    itchat.run()

在这里插入图片描述

你可能感兴趣的:(python,自然语言处理,腾讯云,人工智能)