項目位址:https://github.com/lin423497786/django-chatbot.git
效果圖
django-chatbot
django-chatbot是chatbot-py的django封裝版本,chatbot-py是中文聊天機器人, 支援上下文管理,動态的函數調用,
可以根據自己的語料訓練出自己想要的聊天機器人,可以用于智能客服、線上問答、智能聊天等場景!
快速開始
1. 安裝
git clone https://github.com/lin423497786/django-chatbot.git
cd django-chatbot
pip install -r requirements.txt
2. 啟動django服務
python manage.py runserver 80
3. 靜态語料學習
靜态語料:不需要調用函數或接口的語料
import requests
post_data = {
'question': '早上吃雞蛋對身體好嗎?',
'answer': '早餐當中吃雞蛋,的确是對身體有很大的益處',
}
headers = {
'Content-Type': 'application/json',
'Accept': '*/*',
}
# post
requests.post('http://127.0.0.1/api/chatbot/learn/', json=post_data, headers=headers)
4. 動态語料學習
動态語料:需要調用函數或接口的語料
這裡以天氣查詢為例子,首先先定義一個函數,這裡使用chatbot.ext.integrate_weather_gaode.get_weather函數
import requests
post_data = {
'question': '天氣查詢', # 問題
'answer': 'chatbot.ext.integrate_weather_gaode.get_weather', # 回答,這裡要填函數的絕對路徑
'category': '天氣', # 分類,可以随便填,不填也行
'type_': 1 # 固定值1,所有動态的問題該值都是1
}
headers = {
'Content-Type': 'application/json',
'Accept': '*/*',
}
# post
requests.post('http://127.0.0.1/api/chatbot/learn/', json=post_data, headers=headers)
5. 提問
學習完後就可以向機器人提出問題, 這裡以步驟4學習的“天氣查詢”進行提問,因為“天氣查詢”調用的是chatbot.ext.integrate_weather_gaode.get_weather
函數,該函數有2個參數,程式會一一識别出來,并一個個詢問,當所有參數都詢問完後程式就會調用該函數,将該函數的傳回值作為回複,如下所示。
import requests
session = requests.session()
# 第一輪詢問
response = session.post('http://127.0.0.1/api/chatbot/question/', json={'question': '天氣查詢'})
print(response.text)
# 第二輪詢問
response = session.post('http://127.0.0.1/api/chatbot/question/', json={'question': '南京'})
print(response.text)
# 第三輪詢問
response = session.post('http://127.0.0.1/api/chatbot/question/', json={'question': '20190822'})
print(response.text)
效果圖
這是內建到微信公衆号後的效果圖