Using NLG component from Python script

Hi, I’d like to use the NLG part of rasa from a Python script and I’m not sure how to go about that. I found a documentation for the NLU step, but nothing on NLG. What I want my script to do:

  • Take user input and modify it
  • Get intent from rasa NLU [implemented successfully]
  • Modify intent ranking
  • Generate response with rasa NLG from modified intent ranking [how can I do that?]
  • Modify response before output to user

Help and hints are much apprecited - thank you everybody in advance!

Hi @s8ttschm, we have an example NLG server hosted here that serves as a good example reference for some of the things you’re looking for: rasa/nlg_server.py at main · RasaHQ/rasa · GitHub

So, in case anyone’s interested, this is what we ended up doing. It may not be pretty but it works…

class CoreModelInterface():
def __init__(self, model_path, utterance_predictor="huggingtweets/ppredictors"):
    """
    Generate models that are the same for every execution
    """
    model = get_validated_path(model_path, "model", DEFAULT_MODELS_PATH)
    model_path = get_model(model)
    self.agent = Agent.load(model_path)
    self.processor = self.agent.create_processor()
    self.generator, self.tokenizer = dm.get_utterance_predictor_and_tokenizer(predictor=utterance_predictor)

def process_message(self, message:str):

    # Get tracker and initial intent ranking
    tracker = asyncio.run(self.processor.fetch_tracker_and_update_session("user"))
    user_message = UserMessage(message)
    parse_data = asyncio.run(self.processor.parse_message(user_message,tracker))

    # run our dm to get new intent prediction
    tokenized_message = word_tokenize(message)
    _, intent, score, _ = dm.main(tokenized_message, self.generator, self.tokenizer, self.agent.interpreter)
    # update parse_data with the new intent and score (we're using a dummy intent id)
    parse_data["intent"] = {'id': 1622048501520703154, 'name': intent, 'confidence': 4.4063952373107895e-05} 

    tracker.update(
        UserUttered(
            message,
            parse_data["intent"],
            parse_data["entities"],
            parse_data,
            input_channel=user_message.input_channel,
            message_id=user_message.message_id,
            metadata=user_message.metadata,
        ),
        self.processor.domain,
    )

    # Generate system response
    action,_ = self.processor.predict_next_action(tracker)
    output_channel = CollectingOutputChannel()
    messages = asyncio.run(action.run(output_channel, self.processor.nlg, tracker, self.processor.domain))
    system_reply = messages.pop().text

    return action.name(), system_reply
1 Like