import jsonimport random
import requests
# client_id 為官網獲取的ak, client_secret 為官網獲取的sk
client_id = "hikc0fsxfqx17dgsbvcugyzx"
client_secret = "wlun6dvlgjulnyvxtiszqzchrhnv4o2k"
def unit_chat(chat_input, user_id="88888"):
"""parameters
----------
chat_input : str
使用者傳送天內容
user_id : str
發起聊天使用者id,可任意定義
return
----------
返回unit回覆內容
"""# 設定預設回覆內容, 一旦介面出現異常, 回覆該內容
chat_reply = "不好意思,俺們正在學習中,隨後回覆你。"
# 根據 client_id 與 client_secret 獲取access_token
url = "" % (
client_id, client_secret)
res = requests.get(url)
print(res)
access_token = eval(res.text)["access_token"]
print(access_token)
# 根據 access_token 獲取聊天機械人介面資料
unit_chatbot_url = "" + access_token
# 拼裝聊天介面對應請求傳送資料,主要是填充 query 值
post_data = ,
"session_id": "",
"service_id": "s30762",
"version": "2.0"
}# 將封裝好的資料作為請求內容, 傳送給unit聊天機械人介面, 並得到返回結果
res = requests.post(url=unit_chatbot_url, json=post_data)
# 獲取聊天介面返回資料
unit_chat_obj = json.loads(res.content)
print(unit_chat_obj)
# print(unit_chat_obj)
# 列印返回的結果
# 判斷聊天介面返回資料是否出錯 error_code == 0 則表示請求正確
if unit_chat_obj["error_code"] != 0: return chat_reply
# 解析聊天介面返回資料,找到返回文字內容 result -> response_list -> schema -> intent_confidence(>0) -> action_list -> say
unit_chat_obj_result = unit_chat_obj["result"]
unit_chat_response_list = unit_chat_obj_result["response_list"]
# 隨機選取乙個"意圖置信度"[+response_list.schema.intent_confidence]不為0的技能作為回答
unit_chat_response_obj = random.choice(
[unit_chat_response for unit_chat_response in unit_chat_response_list if
unit_chat_response["schema"]["intent_confidence"] > 0.0])
unit_chat_response_action_list = unit_chat_response_obj["action_list"]
unit_chat_response_action_obj = random.choice(unit_chat_response_action_list)
unit_chat_response_say = unit_chat_response_action_obj["say"]
return unit_chat_response_say
if __name__ == '__main__':
while true:
chat_input = input("使用者輸入 >>>")
chat_reply = unit_chat(chat_input)
print("unit回覆 >>>", chat_reply)
if chat_input == 'q' or chat_input == 'q':
break
百度Unit學習筆記
面向任務的理解與互動能力。1.對話意圖 列如 換到 臺 對話意圖就是 換台 2.問答意圖 用於圈定某乙個範圍或主題的問答對 3.詞槽 是滿足使用者對話意圖時的關鍵資訊或者限定條件,可以理解為使用者需要提供的篩選條件。列如查天氣時,詞槽就是地點和時間。例如 換到 臺 臺就是乙個 電視台詞槽 它會一定程...
小杜機械人線下店 百度AI小度機械人IP營銷
我已經不是兩歲的孩子了好嗎!小度在9月16號,他的3歲生日這天這麼跟我吼道。人機大戰 早在今年年初,我們就曾策劃小度在電視節目 最強大腦 的人機大戰中露面。三場人機大戰,既是中國電視史上的首次人機大戰,又是在 人工智慧 成為熱詞後,吸引公眾關注中國ai實際技術水平的一大契機。無論是在第 一 三場的人...
記錄 自製閒聊機械人baseline
目錄 此文僅記錄整個對話系統專案的 step1 製作乙個可以執行的baseline 之後再增添相應的功能 和完善專案整體 三 選擇處理方法 只是簡單的功能使用 具體涉及到的知識點 再開其他博文進行介紹 之後再貼鏈結於此文文末 加油吧 目標 選擇內容合適 大小合適 目的相符的語料。大致還應該考慮是否做...