Python調用Rasa API服務

問題描述

Rasa啓用API後可以調用,但官方文檔只給出了curl的方法




解決方案

  1. 啓動Rasa API服務rasa run --enable-api
  2. 訪問網址http://localhost:5005/檢驗是否啓動成功
  3. 運行代碼

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

查看Rasa HTTP API




PS

  1. 查看5005端口是否被佔用 netstat -aon | findstr 5005
  2. 啓動Rasa API服務(跨域)rasa run --enable-api --cors "*"
  3. 啓動Rasa API服務(保存日誌)rasa run --enable-api --log-file out.log
  4. 啓動Rasa API服務(指定模型)rasa run --enable-api -m models




參考文獻

  1. Configuring the HTTP API
  2. Rasa HTTP API
  3. windows系統安裝curl
  4. Windows查看某個端口被佔用的解決方法
  5. python - How to send POST request?
  6. NLU server - ParsingError · Issue #4024
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章