# Using NLG component from Python script

**URL:** <https://forum.rasa.com/t/using-nlg-component-from-python-script/40971>\
**Category:** Rasa Open Source\
**Created:** [March 7, 2021, 9:25am UTC](https://forum.rasa.com/t/using-nlg-component-from-python-script/40971 "2021-03-07T09:25:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![s8ttschm](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@s8ttschm](https://forum.rasa.com/u/s8ttschm)\
**Post date:** [March 7, 2021, 9:25am UTC](https://forum.rasa.com/t/using-nlg-component-from-python-script/40971/1 "2021-03-07T09:25:53Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![b-quachtran](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@b-quachtran](https://forum.rasa.com/u/b-quachtran)\
**Post date:** [March 8, 2021, 7:45pm UTC](https://forum.rasa.com/t/using-nlg-component-from-python-script/40971/2 "2021-03-08T19:45:02Z")

</div>

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](https://github.com/RasaHQ/rasa/blob/main/examples/nlg_server/nlg_server.py)

---

<div class="post-metadata">

**Author:** ![s8ttschm](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@s8ttschm](https://forum.rasa.com/u/s8ttschm)\
**Post date:** [March 23, 2021, 6:37pm UTC](https://forum.rasa.com/t/using-nlg-component-from-python-script/40971/3 "2021-03-23T18:37:37Z")

</div>

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
```
