|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import json |
| 3 | +import time |
| 4 | +import sys |
| 5 | +import requests |
| 6 | +import random |
| 7 | +import string |
| 8 | +from hashlib import md5 |
| 9 | + |
| 10 | +App_ID = "replace your tencent ai plat appid" |
| 11 | +App_Key = "replace your tencent ai plat appkey" |
| 12 | + |
| 13 | +if sys.version_info.major == 2: |
| 14 | + from urllib import urlencode |
| 15 | + from urllib import quote |
| 16 | +else: |
| 17 | + from urllib.parse import urlencode |
| 18 | + from urllib.parse import quote |
| 19 | + |
| 20 | + |
| 21 | +class AiTencentBase(object): |
| 22 | + """ |
| 23 | + 腾讯ai SDK |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, appId, appKey): |
| 27 | + """ |
| 28 | + 初始化AiTencentBase(appId, apiKey) |
| 29 | + """ |
| 30 | + self._appId = appId.strip() |
| 31 | + self._appKey = appKey.strip() |
| 32 | + self.__version = '1_0_0' |
| 33 | + |
| 34 | + def getVersion(self): |
| 35 | + """ |
| 36 | + version |
| 37 | + """ |
| 38 | + return self.__version |
| 39 | + |
| 40 | + def _getReqSign(self, params): |
| 41 | + appkey = self._appKey |
| 42 | + params = self._proccessParams(params) |
| 43 | + if not isinstance(params, dict): |
| 44 | + return {"ret": -1, "msg": '参数不正确'} |
| 45 | + data = dict(sorted(params.items(), key=lambda item: item[0])) |
| 46 | + strparams = urlencode(data) |
| 47 | + strparams += '&app_key=' + quote(appkey) |
| 48 | + m = md5() |
| 49 | + m.update(strparams.encode("utf8")) |
| 50 | + sign = m.hexdigest().upper() |
| 51 | + return sign |
| 52 | + |
| 53 | + def _doHttpPost(self, params, url): |
| 54 | + data = params |
| 55 | + sign = self._getReqSign(params) |
| 56 | + data['sign'] = sign |
| 57 | + header = {'Content-Type': 'application/x-www-form-urlencoded'} |
| 58 | + try: |
| 59 | + r = requests.post(url=url, headers=header, data=data) |
| 60 | + obj = self._proccessResult(r.content) |
| 61 | + except (requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout) as e: |
| 62 | + return { |
| 63 | + 'ret': '-1000', |
| 64 | + 'msg': 'connection or read data timeout', |
| 65 | + } |
| 66 | + |
| 67 | + return obj |
| 68 | + |
| 69 | + def _proccessResult(self, content): |
| 70 | + """ |
| 71 | + 返回结果格式化处理 |
| 72 | + """ |
| 73 | + if sys.version_info.major == 2: |
| 74 | + return json.loads(content) or {} |
| 75 | + else: |
| 76 | + return json.loads(content.decode()) or {} |
| 77 | + |
| 78 | + def _proccessParams(self, params): |
| 79 | + """ |
| 80 | + 参数处理,所有接口公共参数 |
| 81 | + """ |
| 82 | + params['app_id'] = self._appId |
| 83 | + params['time_stamp'] = int(time.time()) # 秒级时间戳 |
| 84 | + params['nonce_str'] = ''.join(random.choice( |
| 85 | + string.ascii_letters) for x in range(12)) # 12位随机字符串最多支持32位字符长度 |
| 86 | + |
| 87 | + return params |
| 88 | + |
| 89 | + |
| 90 | +aiClient = AiTencentBase(App_ID, App_Key) |
| 91 | + |
| 92 | + |
| 93 | +def TencentAI(msg: str): |
| 94 | + session = str(random.random())[2:9] |
| 95 | + params = {'session': session, 'question': msg} |
| 96 | + |
| 97 | + url = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat' |
| 98 | + |
| 99 | + result = aiClient._doHttpPost(params, url) |
| 100 | + if not result.get('ret') and result.get('msg') == 'ok': |
| 101 | + data = result.get('data') |
| 102 | + if session == data.get('session'): |
| 103 | + return data.get('answer') |
0 commit comments