以青雲客和圖靈機器人接口示范python創建個人聊天機器人教程
一、以青雲客聊天機器人為例示范get請求
官方網址:http://api.qingyunke.com/
1、接入指引
請求地址 http://api.qingyunke.com/api.php 請求方式 GET 字符編碼 utf-8 請求示例 http://api.qingyunke.com/api.php?key=free&appid=0&msg=你好
參數 示例 說明 key free 必需,固定值 appid 0 可選,0表示智能識別 msg 你好 必需,關鍵詞,提交前請先經過 urlencode 處理
返回結果 {"result":0,"content":"你好,我就開心了"}
☆ 返回結果中{br}表示換行,請自行替換成需要的代碼。
☆ 為保證接口穩定,調用頻率請控制在200次/10分鍾內,我們正在努力提供更穩定的服務
完整請求URL示例: http://api.qingyunke.com/api.php?key=free&appid=0&msg=你好
其他內容示例:
1 天氣:msg=天氣深圳 2 中英翻譯:msg=翻譯i love you 3 智能聊天:msg=你好笑話:msg=笑話 4 歌詞⑴:msg=歌詞后來 5 歌詞⑵:msg=歌詞后來-劉若英 6 計算⑴:msg=計算1+1*2/3-4 7 計算⑵:msg=1+1*2/3-4 8 IP⑴:msg=歸屬127.0.0.1 9 IP⑵:msg=127.0.0.1 10 手機⑴:msg=歸屬13430108888 11 手機⑵:msg=13430108888 12 成語查詢:msg=成語一生一世 13 五筆/拼音:msg=好字的五筆/拼音
2、封裝函數
2.1 封裝示例:
# 導入模塊 import requests from urllib import parse # 組裝請求 def test_get(msg): url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(parse.quote(msg)) html = requests.get(url) return html.json()["content"] # 解析返回並打印 while True: msg = input("我:") res = test_get(msg) print("答:", res)
2.2 執行返回結果:
C:\Users\yzp\AppData\Local\Programs\Python\Python37\python.exe D:/00test/RFTEST/qingyunke
我:您是誰?
答: 我是機器人
我:您在哪?
答: 我在找您
我:您要去哪里?
答: 哪也別去,改天菲菲帶你去吧
二、以圖靈聊天機器人為例示范post請求
1、接口說明
API V2.0是基於圖靈機器人平台語義理解、深度學習等核心技術,為廣大開發者和企業提供的在線服務和開發接口。目前API接口可調用聊天對話、語料庫、技能三大模塊的語料:
- 聊天對話是指平台免費提供的近10億條公有對話語料,滿足用戶對話娛樂需求;
- 語料庫是指用戶在平台上傳的私有語料,僅供個人查看使用,幫助用戶最便捷的搭建專業領域次的語料。
- 技能服務是指平台打包的26種實用服務技能。涵蓋生活、出行、購物等多個領域,一站式滿足用戶需求。/2、 使用說明
2、編碼方式:
UTF-8(調用圖靈API的各個環節的編碼方式均為UTF-8)/3、接口地址:
http://openapi.turingapi.com/openapi/api/v2
請求方式:
HTTP POST
請求參數:
請求參數格式為 json
請求示例:
{ "reqType":0, "perception": { "inputText": { "text": "附近的酒店" }, "inputImage": { "url": "imageUrl" }, "selfInfo": { "location": { "city": "北京", "province": "北京", "street": "信息路" } } }, "userInfo": { "apiKey": "", "userId": "" } }
部分參數說明,完整接口說明請看圖靈api官方網址:https://www.kancloud.cn/turing/www-tuling123-com/718227:
接口中apiKey需要自行上官網申請:http://www.turingapi.com/
3、完整調用示例:
import requests url = "http://openapi.tuling123.com/openapi/api/v2" data = { "reqType": 0, "perception": { "inputText": { "text": "附近的酒店" }, "inputImage": { "url": "imageUrl" }, "selfInfo": { "location": { "city": "北京", "province": "北京", "street": "信息路" } } }, "userInfo": { "apiKey": "1c99470a8a8354e248a4c229234d14af", "userId": "1" } } res = requests.post(url=url, json=data) # JSON格式的請求,將數據賦給json參數 print(res.text)
接口返回:
{ "intent": { "actionName": "", "code": 10037, "intentName": "" }, "results": [ { "groupType": 1, "resultType": "text", "values": { "text": "我還沒去過那,所以我暫時就不給你推薦了" } } ] }
4 、封裝函數:
4.1 封裝示例
import requests while True: url = "http://openapi.tuling123.com/openapi/api/v2" question = input("我:") data = { "reqType": 0, "perception": { "inputText": { "text": question } }, "userInfo": { "apiKey": "e825286159f9f57db1b597995d7b", "userId": "1234" } } res = requests.post(url=url, json=data) # JSON格式的請求,將數據賦給json參數 answer = res.json()["results"][0]["values"]["text"] print("答:"+answer)
4.2 執行返回結果
C:\Users\yzp\AppData\Local\Programs\Python\Python37\python.exe D:/00test/RFTEST/test_post.py 我:你是誰? 答:我是機器人 我:你今年多大了 答:18歲! 我: