You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rasa聊天机器人localhost部署后无响应问题求助

本地Rasa机器人网页部署排查步骤
  • Rasa服务与跨域检查

    1. 先确认Rasa服务正常:终端执行rasa run --enable-api --cors "*",看到Started http server on port 5005说明启动成功。可以用命令curl http://localhost:5005/webhooks/rest/webhook -X POST -H "Content-Type: application/json" -d '{"sender": "test", "message": "hello"}'测试,能收到数组格式的响应就是正常的。
    2. 跨域是高频坑:别直接用file://打开index.html,浏览器会拦截跨域请求。把前端文件放本地web服务器,比如用python -m http.server 8000启动,然后访问http://localhost:8000/index.html。
  • 前端请求代码校验

    1. 核对请求URL:必须是http://localhost:5005/webhooks/rest/webhook,端口、路径不能错。
    2. 确认请求格式:必须是POST请求,Content-Type设为application/json,请求体要有sender和message字段,示例代码:
      async function sendMsg() {
        const input = document.getElementById('user-input').value;
        try {
          const res = await fetch('http://localhost:5005/webhooks/rest/webhook', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({sender: 'web_user', message: input})
          });
          const data = await res.json();
          console.log(data); // 看控制台输出是否有有效响应
        } catch (err) {
          console.error('请求出错:', err);
        }
      }
      
    3. 用浏览器控制台(F12)查问题:Network标签看请求状态,404是路径错,403是跨域没配,500是Rasa内部问题。
  • 版本兼容性处理
    自己写前端组件不需要依赖Rasa Webchat,只要请求符合Rasa API规范就行。Rasa 2.x和3.x的rest webhook格式基本一致,都是接收sender和message,返回数组响应。执行rasa --version确认版本,3.x启动命令里的--enable-api和--cors参数依然有效。

本地部署成功案例
  1. 启动Rasa服务
    终端运行:

    rasa run --enable-api --cors "*" --port 5005
    
  2. 前端页面代码
    新建index.html,内容如下:

    <!DOCTYPE html>
    <html>
    <head>
      <title>本地Rasa聊天</title>
      <style>
        #chat-container { width: 400px; height: 500px; border: 1px solid #ddd; padding: 10px; overflow-y: auto; margin-bottom: 10px; }
        .user-msg { text-align: right; color: #2563eb; margin: 4px 0; }
        .bot-msg { text-align: left; color: #16a34a; margin: 4px 0; }
        #msg-input { width: 320px; padding: 6px; }
        #send-btn { padding: 6px 12px; }
      </style>
    </head>
    <body>
      <div id="chat-container"></div>
      <input type="text" id="msg-input" placeholder="输入消息...">
      <button id="send-btn">发送</button>
    
      <script>
        const chatBox = document.getElementById('chat-container');
        const input = document.getElementById('msg-input');
        const btn = document.getElementById('send-btn');
    
        function addMsg(content, isUser) {
          const msgDiv = document.createElement('div');
          msgDiv.className = isUser ? 'user-msg' : 'bot-msg';
          msgDiv.textContent = isUser ? `你: ${content}` : `机器人: ${content}`;
          chatBox.appendChild(msgDiv);
          chatBox.scrollTop = chatBox.scrollHeight;
        }
    
        async function sendMessage() {
          const content = input.value.trim();
          if (!content) return;
    
          addMsg(content, true);
          input.value = '';
    
          try {
            const res = await fetch('http://localhost:5005/webhooks/rest/webhook', {
              method: 'POST',
              headers: {'Content-Type': 'application/json'},
              body: JSON.stringify({sender: 'local_user', message: content})
            });
    
            const responses = await res.json();
            responses.forEach(item => {
              if (item.text) addMsg(item.text, false);
            });
          } catch (err) {
            addMsg('抱歉,暂时无法响应', false);
            console.error('请求失败:', err);
          }
        }
    
        btn.addEventListener('click', sendMessage);
        input.addEventListener('keypress', e => {
          if (e.key === 'Enter') sendMessage();
        });
      </script>
    </body>
    </html>
    
  3. 启动前端服务器
    在index.html所在目录打开终端,运行:

    python -m http.server 8000
    

    浏览器访问http://localhost:8000/index.html,输入消息就能收到机器人响应。

常见问题总结
  • 禁止直接用file://打开前端页面,必须通过本地web服务器访问。
  • Rasa启动必须加--cors "*",允许跨域请求。
  • 前端请求URL的端口要和Rasa服务端口一致,默认是5005。
  • 返回undefined时,优先看浏览器控制台的请求错误信息,大概率是请求失败或响应格式不对。

内容的提问来源于stack exchange,提问作者Daviz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:56:00