Python小度

这只是一个对话器!还不能听歌(反正我也没在UNIT平台配置听歌的功能)!

反正最近也不知怎么的,就想做一个AI对话器
语音识别和语音输出都不要,input()和print()就行
本来准备用小爱的,但要实名,身份证找不到
还是百度良心,不用实名认证,开始喽

代码

 1 import requests
 2 import random
 3 import datetime
 4 
 5 helpword='''
 6 Python小度 帮助
 7 本地帮助
 8 $help 获取帮助
 9 $quit 退出
10 $get_acc_token 获取access token
11 $test 测试网络连接
12 '''
13 
14 def localCommand(word):
15     if word=='$help':
16         print(helpword)
17     elif word=='$quit':
18         exit()
19     elif word=='$get_acc_token':
20         print(get_acc_token())
21     elif word=='$test':
22         print(requests.get("https://www.baidu.com"))
23 
24 def get_acc_token():
25     dic={'grant_type':'client_credentials','client_id':'OgyQl5Pc3zN9kFGs4UHqrnxf','client_secret':'cEh2zkvbPuxi4me5jj068VaRGg00Vf06'}
26     res=requests.post("https://aip.baidubce.com/oauth/2.0/token",data=dic)
27     req=res.json()
28     acc_token=req['access_token']
29     return acc_token
30 
31 def chat(word):
32     skill_ids=['1036063','1036064','1036060','1036062','1036059','1036061']
33     dic={
34          'log_id':str(random.random()),
35          'request':{
36                     'query':word,
37                     'user_id':str(random.randint(0,16384))
38                    },
39          'session':'',
40          'skill_ids':skill_ids,
41          'service_id':'S31885',
42          'version':'2.0'}
43     res=requests.post("https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token="+get_acc_token(),json=dic)
44     req=res.json()
45     unit_chat_obj_result = req["result"]
46     unit_chat_response_list = unit_chat_obj_result["response_list"]
47     # 随机选取一个"意图置信度"[+response_list[].schema.intent_confidence]不为0的技能作为回答
48     unit_chat_response_obj = unit_chat_response_list[0]
49     unit_chat_response_action_list = unit_chat_response_obj["action_list"]
50     unit_chat_response_action_obj = random.choice(unit_chat_response_action_list)
51     unit_chat_response_say = unit_chat_response_action_obj["say"]
52     return unit_chat_response_say
53 
54 print(get_acc_token())
55 print('2020 By 人工智障')
56 print('调用本地功能请在关键词前面输入$,更多帮助请输入$help')
57 print('==================================================')
58 
59 while True:
60     curr_time=datetime.datetime.now()
61     print(''+datetime.datetime.strftime(curr_time,'%Y-%m-%d %H:%M:%S'))
62     word=input('')
63     print('')
64     localCommand(word)
65     curr_time=datetime.datetime.now()
66     print('小度 '+datetime.datetime.strftime(curr_time,'%Y-%m-%d %H:%M:%S'))
67     print(chat(word))
68     print('')

虽然我把代码贴出来了,但希望你不要在我不知道的情况下拿我的api key和api secret去干别的,如果真的要拿去做别的事,就跟我说一声。

下载

名称 Python小度
链接 https://n802.com/dir/27256477-40136798-aa8542
py文件
exe文件
一键安装库 提供
原文地址:https://www.cnblogs.com/TotoWang/p/py_duer.html