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

如何像常规模型一样加载Rasa模型并逐句预测意图?

如何在Python中直接加载Rasa模型并单句预测意图

1. 安装必要依赖

确保已安装Rasa核心库:

pip install rasa

2. 核心代码实现

使用Rasa内置的RasaNLUInterpreter类即可完成模型加载与单句预测,完全无需调用rasa test或官方API:

from rasa.core.interpreter import RasaNLUInterpreter

# 加载训练好的模型压缩包
interpreter = RasaNLUInterpreter.load("x.tgz")

# 定义预测函数,返回意图及置信度
def predict(sentence):
    parse_result = interpreter.parse(sentence)
    return {
        "intent": parse_result["intent"]["name"],
        "confidence": parse_result["intent"]["confidence"]
    }

# 测试调用
print(predict("Predict for this sentence!!"))

补充说明

  • 若仅需意图识别,RasaNLUInterpreter完全满足需求;如果需要处理对话管理逻辑,可改用rasa.core.agent.Agent加载完整模型。
  • parse()方法返回的字典包含丰富信息,除意图外还可提取实体、响应词等,可根据业务需求调整返回内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 09:50:17