問題描述
Rasa啓用API後可以調用,但官方文檔只給出了curl的方法
解決方案
- 啓動Rasa API服務
rasa run --enable-api
- 訪問網址
http://localhost:5005/
檢驗是否啓動成功 - 運行代碼
A.py(不要命名爲test.py)
import json
import requests
url = "http://localhost:5005/model/parse"
data = {"text": "hello"}
data = json.dumps(data, ensure_ascii=False)
data = data.encode(encoding="utf-8") # 如果text帶中文需要轉編碼
r = requests.post(url=url, data=data)
print(json.loads(r.text))
結果
{'intent': {'name': 'greet', 'confidence': 0.9930983185768127}, 'entities': [], 'intent_ranking': [{'name': 'greet', 'confidence': 0.9930983185768127}, {'name': 'mood_unhappy', 'confidence': 0.0036425672005861998}, {'name': 'bot_challenge', 'confidence': 0.002293727360665798}, {'name': 'mood_great', 'confidence': 0.00035825479426421225}, {'name': 'goodbye', 'confidence': 0.00032570294570177794}, {'name': 'affirm', 'confidence': 0.00022301387798506767}, {'name': 'deny', 'confidence': 5.849302760907449e-05}], 'text': 'hello'}
其他API
PS
- 查看5005端口是否被佔用
netstat -aon | findstr 5005
- 啓動Rasa API服務(跨域)
rasa run --enable-api --cors "*"
- 啓動Rasa API服務(保存日誌)
rasa run --enable-api --log-file out.log
- 啓動Rasa API服務(指定模型)
rasa run --enable-api -m models