1 import requests 2 import json 3 # https://www.kancloud.cn/turing/www-tuling123-com/718227 4 url = ‘http://openapi.tuling123.com/openapi/api/v2‘ 5 6 data = { 7 "reqType":0, 8 "perception": { 9 "inputText": { 10 "text": input(‘Hello,有什么可以帮你? ‘) 11 }, 12 "selfInfo": { 13 "location": { 14 "city": "北京", 15 "province": "北京", 16 "street": "地盛西路" 17 } 18 } 19 }, 20 "userInfo": { 21 "apiKey": "c9bd8ff4b4e846b694cad6313345beb4", 22 "userId": "434284" 23 } 24 } 25 26 res = requests.post(url,json=data) 27 print(res.json()[‘results‘][0][‘values‘][‘text‘])
执行结果:
1 D:\USERDATA\python>C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python.exe d:/USERDATA/python/tuling.py 2 Hello,有什么可以帮你? 吃的 3 问世间吃为何物,直叫人以身材相许。 4 5 6 D:\USERDATA\python>C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python.exe d:/USERDATA/python/tuling.py 7 Hello,有什么可以帮你? 吃的 8 咦,怎么又是这句话呀 9 10 11 D:\USERDATA\python>C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python.exe d:/USERDATA/python/tuling.py 12 Hello,有什么可以帮你? 吃的 13 哎呀,你好像个复读机呀
1 import requests 2 import json 3 4 5 userid = str(1) 6 # 1 可以替换成任何长度小于32的字符串哦 7 apikey = str(‘A‘) 8 # 这里的A,记得替换成你自己的apikey哦~ 9 10 11 # 创建post函数 12 def robot(content): 13 # 图灵api 14 api = r‘http://openapi.tuling123.com/openapi/api/v2‘ 15 # 创建post提交的数据 16 data = { 17 "perception": { 18 "inputText": { 19 "text": content 20 } 21 }, 22 "userInfo": { 23 "apiKey": apikey, 24 "userId": userid, 25 } 26 } 27 # 转化为json格式 28 jsondata = json.dumps(data) 29 # 发起post请求 30 response = requests.post(api, data = jsondata) 31 # 将返回的json数据解码 32 robot_res = json.loads(response.content) 33 # 提取对话数据 34 print(robot_res["results"][0][‘values‘][‘text‘]) 35 36 37 for x in range(10): 38 content = input("talk:") 39 # 输入对话内容 40 robot(content) 41 if x == 10: 42 break 43 # 十次之后就结束对话,数字可以改哦,你想几次就几次
1 while True: 2 content = input("talk:") 3 # 输入对话内容 4 robot(content) 5 if content == ‘bye‘: 6 # 设置stopwords 7 break
原文:https://www.cnblogs.com/www1707/p/10850589.html