Skip to main content
 首页 » 编程设计

python之LUIS.ai json 迁移到 Rasa 格式 json 不返回实体,但返回正确的意图

2025年05月04日33EasonJim

我已经使用以下命令将从 LUIS 应用程序下载的 json 迁移到 RASA 格式:python -m rasa_nlu.train -c config_spacy.json

我的配置文件是这样的:

{      
"path" : "./models",    
"data" : "./data/examples/rasa/BookACab.json", 
"pipeline" : ["nlp_spacy", "tokenizer_spacy", "intent_featurizer_spacy",  
              "ner_crf", "ner_synonyms", "intent_classifier_sklearn",  
              "ner_duckling"]  
} 

模型是用 RASA 格式的 json 生成的,如下所示。但是,当我使用

查询此模型时

http://localhost:5000/parse?q=book a ride later

返回与我输入的文本及其所有相关实体相关的正确高分意图。但是当我尝试另一个文本时:

http://localhost:5000/parse?q=I want to go ride today 5pm

返回的意图是正确的,但它的 Entities 对象是空的。正如您在下面的 json 中看到的,与工作示例类似,该话语也将实体映射到它。

请帮助我了解这是否是每个使用 RASA 的人都会遇到的问题,还是我做错了什么?谢谢!

  { 
  "rasa_nlu_data": { 
    "common_examples": [ 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "later", 
            "start": 0, 
            "end": 5 
          } 
        ], 
        "intent": "None", 
        "text": "later" 
      }, 
      { 
        "entities": [], 
        "intent": "ServiceRequestEnquiry", 
        "text": "wake up" 
      }, 
      { 
        "entities": [], 
        "intent": "ConfirmationNo", 
        "text": "no not now" 
      }, 
      { 
        "entities": [], 
        "intent": "ConfirmationNo", 
        "text": "not sure" 
      }, 
      { 
        "entities": [], 
        "intent": "ConfirmationNo", 
        "text": "no bot" 
      }, 
      { 
        "entities": [], 
        "intent": "ConfirmationNo", 
        "text": "no goride bot" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "later", 
            "start": 12, 
            "end": 17 
          } 
        ], 
        "intent": "BookCab", 
        "text": "book a ride later" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "now", 
            "start": 21, 
            "end": 24 
          } 
        ], 
        "intent": "BookCab", 
        "text": "i want go for a ride now" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "today", 
            "start": 12, 
            "end": 17 
          } 
        ], 
        "intent": "BookCab", 
        "text": "book a ride today" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "today 5pm", 
            "start": 18, 
            "end": 27 
          } 
        ], 
        "intent": "BookCab", 
        "text": "I want to go ride today 5pm" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "today", 
            "start": 12, 
            "end": 17 
          } 
        ], 
        "intent": "BookCab", 
        "text": "book a ride today 5pm" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "later", 
            "start": 13, 
            "end": 18 
          } 
        ], 
        "intent": "BookCab", 
        "text": "book shuttle later" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "now", 
            "start": 15, 
            "end": 18 
          } 
        ], 
        "intent": "None", 
        "text": "i want to book now" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "booknow", 
            "start": 10, 
            "end": 17 
          } 
        ], 
        "intent": "None", 
        "text": "i want to booknow" 
      }, 
      { 
        "entities": [ 
          { 
            "entity": "RideTime", 
            "value": "book later", 
            "start": 10, 
            "end": 20 
          } 
        ], 
        "intent": "None", 
        "text": "i want to book later" 
      } 
    ], 
    "regex_features": [] 
  } 
} 

请您参考如下方法:

如果您能包含 pipeline 会很有帮助您正在与 Rasa 一起使用。您可以在您的 configuration 中找到它文件。假设您没有更改 config_spacy.json 中的默认管道,那么您正在使用 ner_crf用于实体识别。

很可能由于库的差异,Rasa 只需要比 LUIS 需要更多的训练数据。从质量上讲,mitie 管道通常需要较少的训练数据,但代价是需要更多的训练时间。

所以您的问题的基本答案是:如果您想使用 ner_crf,那么您需要增加为实体识别提供的训练数据量。

也就是说:RideTime 是您唯一的实体吗?如果是这样,您应该考虑添加 ner_duckling到您的管道,它可以识别日期。这比您尝试自己训练日期的效果更好。

因此使用上面的训练数据和管道:

["nlp_spacy", "tokenizer_spacy", "intent_featurizer_spacy", "ner_crf", "ner_synonyms",  "intent_classifier_sklearn", "ner_duckling"] 

结果如下:

{ 
    "entities": [ 
        { 
            "additional_info": { 
                "grain": "hour", 
                "others": [ 
                    { 
                        "grain": "hour", 
                        "value": "2017-07-26T17:00:00.000Z" 
                    } 
                ], 
                "value": "2017-07-26T17:00:00.000Z" 
            }, 
            "end": 27, 
            "entity": "time", 
            "extractor": "ner_duckling", 
            "start": 18, 
            "text": "today 5pm", 
            "value": "2017-07-26T17:00:00.000Z" 
        } 
    ], 
    "intent": { 
        "confidence": 0.5469262356494486, 
        "name": "BookCab" 
    }, 
    "intent_ranking": [ 
        { 
            "confidence": 0.5469262356494486, 
            "name": "BookCab" 
        }, 
        { 
            "confidence": 0.2812606328712321, 
            "name": "None" 
        }, 
        { 
            "confidence": 0.08727531874740564, 
            "name": "ConfirmationNo" 
        }, 
        { 
            "confidence": 0.0845378127319134, 
            "name": "ServiceRequestEnquiry" 
        } 
    ], 
    "text": "I want to go ride today 5pm" 
} 

这个完整的训练集对我来说效果很好。这只是添加更多训练示例的问题。因此,当您进行更多测试时,如果您遇到一个无法按预期工作的示例,请将其添加到训练数据中并重新训练。从而教会您的模型处理更多不同的请求。

https://gist.github.com/wrathagom/7f05fbda75c785977bd07cd89e62ddd7