rasa - http api测试

rasa - http api测试

rasa run --enable-api --cors "*"

仅解析意图

请求地址:localhost:5005/model/parse
命令行 curl -X POST localhost:5005/model/parse -d ‘{“text”: “hello”}’

代码示例

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")
r = requests.post(url=url, data=data)
print(json.loads(r.text))

连续对话

请求地址: localhost:5005/webhooks/rest/webhook
curl -X POST localhost:5005/webhooks/rest/webhook -d {“sender”: sender, “message”: message}

代码示例

import json
import secrets
import requests


def post(url, data=None):
    data = json.dumps(data, ensure_ascii=False)
    data = data.encode(encoding="utf-8")
    r = requests.post(url=url, data=data)
    r = json.loads(r.text)
    return r


sender = secrets.token_urlsafe(16)
url = "http://localhost:5005/webhooks/rest/webhook"
while True:
    message = input("Your input ->  ")
    data = {
        "sender": sender,
        "message": message
    }
    print(post(url, data))

1. post请求,添加messages

请求地址:localhost:5005/conversations/0/messages
命令行 curl -X POST localhost:5005/conversations/0/messages -d ‘{ “text”: “Hello!”, “sender”: “user” }’

2. post请求,预测action

请求地址:localhost:5005/conversations/0/predict
命令行 curl -X POST localhost:5005/conversations/0/predict

3. 执行action

请求地址:localhost:5005/conversations/0/execute
命令行 curl -X POST localhost:5005/conversations/0/execute -d ‘{ “name”: “utter_greet” }’

代码示例

import json
import requests


def post(url, data=None):
    data = json.dumps(data, ensure_ascii=False)
    data = data.encode(encoding="utf-8")
    r = requests.post(url=url, data=data)
    r = json.loads(r.text)
    return r


if __name__ == "__main__":
    # 会话id,此处简单设为0
    conversation_id = 0
    
    # 1. 发送消息
    url = "http://localhost:5005/conversations/{}/messages".format(conversation_id)
    data = {
        "text": "Hi",
        "sender": "user"
    }
    result = post(url, data)
    print(result)

    # 2. 预测下一步动作
    url = "http://localhost:5005/conversations/{}/predict".format(conversation_id)
    result = post(url)
    print(result)

    # 3. 执行动作
    url = "http://localhost:5005/conversations/{}/execute".format(conversation_id)
    data = {
        "name": result["scores"][0]["action"]  # 取置信度最高的动作
    }
    result = post(url, data)
    print(result)
    print(result["messages"])  # 获取对话信息
    # [{'recipient_id': '1', 'text': 'Hey! How are you?'}]

对话流程

简单例子无法连续对话,如何连续对话呢?
rasa - http api测试

import json
import secrets
import requests


def post(url, data=None):
    data = json.dumps(data, ensure_ascii=False)
    data = data.encode(encoding="utf-8")
    r = requests.post(url=url, data=data)
    r = json.loads(r.text)
    return r


if __name__ == "__main__":
    conversation_id = secrets.token_urlsafe(16)  # 随机生成会话id
    messages_url = "http://localhost:5005/conversations/{}/messages".format(conversation_id)  # 发送消息
    predict_url = "http://localhost:5005/conversations/{}/predict".format(conversation_id)  # 预测下一步动作
    execute_url = "http://localhost:5005/conversations/{}/execute".format(conversation_id)  # 执行动作
    action = "action_listen"  # 动作初始化为等待输入
    while True:
        if action in ["action_listen", "action_default_fallback", "action_restart"]:
            # 等待输入
            text = input("Your input ->  ")
            post(messages_url, data={"text": text, "sender": "user"})  # 发送消息

        response = post(predict_url)  # 预测下一步动作
        action = response["scores"][0]["action"]  # 取出置信度最高的下一步动作

        response = post(execute_url, data={"name": action})  # 执行动作
        messages = response["messages"]  # 取出对话信息
        if messages:
            print(messages)

domain.yml中的utter_great可添加buttons

responses:
  utter_greet:
  - text: Hey! How are you?
    buttons:
      - payload: '/mood_great'
        title: 'great'
      - payload: '/mood_unhappy'
        title: 'sad'

rasa - http api测试

常用命令

查看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

遇到的坑

若遇到死循环,一般是遇到默认动作没有退出,查看默认动作

上一篇:成为一名更好的软件工程师的简单方法


下一篇:idea如何远程调试线上代码