-
Notifications
You must be signed in to change notification settings - Fork 0
/
intents.py
48 lines (35 loc) · 1.6 KB
/
intents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from contextlib import suppress
from typing import Generic, NamedTuple, Optional, TypeVar, Union
from pydantic import validator
from pydantic.generics import GenericModel
from emma_experience_hub.datamodels.simbot import (
SimBotIntentType,
SimBotPhysicalInteractionIntentType,
SimBotVerbalInteractionIntentType,
)
from emma_experience_hub.datamodels.simbot.enums import SimBotActionType
T = TypeVar("T", bound=SimBotIntentType)
class SimBotIntent(GenericModel, Generic[T]):
"""Model represenating the intent behind the utterance."""
type: T
action: Optional[SimBotActionType] = None
entity: Optional[str] = None
@validator("type", pre=True)
@classmethod
def convert_intent_type_enum(
cls, intent_type: Union[str, SimBotIntentType]
) -> Union[str, SimBotIntentType]:
"""Check the incoming value for the intent type and ensure it will be an enum."""
if isinstance(intent_type, str):
# See if the intent type is already a value within the enum
with suppress(ValueError):
return SimBotIntentType(intent_type)
# See if the intent type is already a key within the enum
with suppress(KeyError):
return SimBotIntentType[intent_type]
# Otherwise just return it and let it error if it errors
return intent_type
class SimBotAgentIntents(NamedTuple):
"""Tuple of selected agent intents."""
physical_interaction: Optional[SimBotIntent[SimBotPhysicalInteractionIntentType]] = None
verbal_interaction: Optional[SimBotIntent[SimBotVerbalInteractionIntentType]] = None