ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

rasa - http api测试

2021-10-13 13:02:51  阅读:528  来源: 互联网

标签:http 5005 rasa url text api post data localhost


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?'}]

对话流程

简单例子无法连续对话,如何连续对话呢?

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'

常用命令

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

遇到的坑

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

标签:http,5005,rasa,url,text,api,post,data,localhost
来源: https://blog.csdn.net/weixin_42486623/article/details/120740107

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有