腾讯ai开放平台(智能闲聊)sdk----- python3.X

想试下聊天ai,由于图灵有限制,故改用腾讯 智能闲聊。腾讯闲聊只提供api接口,没有SDK,写下python的SDK。

腾讯ai开放平台(智能闲聊)sdk----- python3.X_第1张图片

 

sdk文件:

import hashlib
import urllib
import urllib.request
import base64
import json
import time
#接口api
url_preffix='https://api.ai.qq.com/fcgi-bin/'

def setParams(array, key, value):
    array[key] = value

def genSignString(parser):
    uri_str = ''
    for key in sorted(parser.keys()):
        if key == 'app_key':
            continue
        uri_str += "%s=%s&" % (key,urllib.parse.quote(str(parser[key]), safe = ''))
    sign_str = uri_str + 'app_key=' + parser['app_key']

    hash_md5 = hashlib.md5(sign_str.encode("latin1"))
    return hash_md5.hexdigest().upper()


class AiPlat(object):
    def __init__(self, app_id, app_key):
        self.app_id = app_id
        self.app_key = app_key
        self.data = {}

    def invoke(self, params):
        self.url_data = urllib.parse.urlencode(params).encode(encoding='utf-8')
        req = urllib.request.Request(self.url, self.url_data)
        try:
            rsp = urllib.request.urlopen(req)
            str_rsp = rsp.read()
            dict_rsp = json.loads(str_rsp.decode('utf-8'))
            return dict_rsp
        except urllib.error.URLError as e:
            dict_error = {}
            if hasattr(e, "code"):
                dict_error = {}
                dict_error['ret'] = -1
                dict_error['httpcode'] = e.code
                dict_error['msg'] = "sdk http post err"
                return dict_error
            if hasattr(e,"reason"):
                dict_error['msg'] = 'sdk http post err'
                dict_error['httpcode'] = -1
                dict_error['ret'] = -1
                return dict_error
        else:
            dict_error = {}
            dict_error['ret'] = -1
            dict_error['httpcode'] = -1
            dict_error['msg'] = "system error"
            return dict_error


    def getNlpTextChat(self, session, question):
        self.url = url_preffix + 'nlp/nlp_textchat'
        setParams(self.data, 'app_id', self.app_id)
        setParams(self.data, 'app_key', self.app_key)
        setParams(self.data, 'time_stamp', int(time.time()))
        setParams(self.data, 'nonce_str', int(time.time()))
        setParams(self.data, 'session', session)
        setParams(self.data, 'question', question)
        sign_str = genSignString(self.data)
        setParams(self.data, 'sign', sign_str)
        return self.invoke(self.data)

 测试一下

import optparse
import time
import apiutil#这里我上端代码独立生成一个文件“apiutil.py",所以要导入一下
import json
app_key = 'XXXXXXXXXX'
app_id = '110XXXX996'
questionS= '在吗?'
def anso(questionS):
    str_question = questionS
    session = 10000
    ai_obj = apiutil.AiPlat(app_id, app_key)

    rsp = ai_obj.getNlpTextChat(session,str_question)
    if rsp['ret'] == 0:
        print('............................................................')
        ask = (rsp['data'])['answer']
        print(ask)
    else:
        print(json.dumps(rsp, ensure_ascii=False, sort_keys=False, indent=4))
if __name__ == '__main__':
    anso(questionS)

 

 

 

 

你可能感兴趣的:(sdk)