From 555f727d651e440ac58fcedb3e0265f695e06e66 Mon Sep 17 00:00:00 2001 From: OgnjenFrancuski Date: Fri, 2 Feb 2024 16:36:37 +0100 Subject: [PATCH] Move duckling loading to its own module --- .github/workflows/continous-integration.yml | 1 + actions/check_restaurant_availability.py | 12 ++++-------- actions/entity_extractor.py | 15 +++++++++++++++ actions/setup_recurrent_payment.py | 4 ++-- 4 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 actions/entity_extractor.py diff --git a/.github/workflows/continous-integration.yml b/.github/workflows/continous-integration.yml index 13fd7c6..2502fab 100644 --- a/.github/workflows/continous-integration.yml +++ b/.github/workflows/continous-integration.yml @@ -69,6 +69,7 @@ jobs: OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}} RASA_PRO_LICENSE: ${{secrets.RASA_PRO_LICENSE}} RASA_PRO_BETA_INTENTLESS: true + DUCKLING_URL: ${{secrets.DUCKLING_URL}} run: | make train diff --git a/actions/check_restaurant_availability.py b/actions/check_restaurant_availability.py index e5bd0ce..948a508 100644 --- a/actions/check_restaurant_availability.py +++ b/actions/check_restaurant_availability.py @@ -1,17 +1,13 @@ from datetime import datetime, timedelta +from typing import List, Optional from rasa.shared.nlu.training_data.message import Message -from rasa_sdk.interfaces import Action, Tracker from rasa_sdk.events import EventType, SlotSet from rasa_sdk.executor import CollectingDispatcher +from rasa_sdk.interfaces import Action, Tracker from rasa_sdk.types import DomainDict -from rasa.nlu.extractors.duckling_entity_extractor import DucklingEntityExtractor -from typing import List, Optional -duckling_config = {**DucklingEntityExtractor.get_default_config(), - "url": "https://rasa:xCTBjGqjTqDqE6X72FXLiBWVXYaQDZ@duckling.rasa-dev.io", - "dimensions": ["time"]} -duckling = DucklingEntityExtractor(duckling_config) +from actions.entity_extractor import duckling_entity_extractor class CheckRestaurantAvailability(Action): @@ -44,7 +40,7 @@ async def run( def parse_datetime(text: str) -> Optional[datetime]: msg = Message.build(text) - duckling.process([msg]) + duckling_entity_extractor.process([msg]) if len(msg.data["entities"]) == 0: return None parsed_value = msg.data["entities"][0]["value"] diff --git a/actions/entity_extractor.py b/actions/entity_extractor.py new file mode 100644 index 0000000..18ff9d0 --- /dev/null +++ b/actions/entity_extractor.py @@ -0,0 +1,15 @@ +import os + +from dotenv import load_dotenv +from rasa.nlu.extractors.duckling_entity_extractor import DucklingEntityExtractor + +load_dotenv() +duckling_url = os.environ.get("DUCKLING_URL") + +duckling_config = { + **DucklingEntityExtractor.get_default_config(), + "url": duckling_url, + "dimensions": ["time"] +} + +duckling_entity_extractor = DucklingEntityExtractor(duckling_config) diff --git a/actions/setup_recurrent_payment.py b/actions/setup_recurrent_payment.py index 60a2744..a70628a 100644 --- a/actions/setup_recurrent_payment.py +++ b/actions/setup_recurrent_payment.py @@ -7,12 +7,12 @@ from rasa_sdk.executor import CollectingDispatcher from rasa_sdk.types import DomainDict -from actions.check_restaurant_availability import duckling +from actions.entity_extractor import duckling_entity_extractor def parse_datetime(text: str) -> Optional[datetime]: msg = Message.build(text) - duckling.process([msg]) + duckling_entity_extractor.process([msg]) if len(msg.data["entities"]) == 0: return None