Agent¶
The Agent
base class is the entry point of your project. You will
subclass it when defining your own Agent, and will later Agent.register()
Intent classes and other resources into it.
Once the Agent is defined, you will connect it to a cloud service with a
Connector
, to make prediction and trigger requests.
-
class
Agent
[source]¶ As the name suggests, Agent is the base class that models an Agent definition within the Intents framework.
Typically, you will define a single Agent class in your project, that could be as simple as this:
from intents import Agent class MyAgent(Agent): """A little docstring for your Agent"""
You can optionally define the languages that you intend to support. Intents will look for language resources based on the language class variable:
class MyAgent(Agent): """A little docstring for your Agent""" languages = ["en", "it"]
Languages are values from
LanguageCode
. If omitted, Intents will discover language resources by itself.You won’t do much more with your Agent class, other than registering intents and resources with
Agent.register()
, or passing it to aConnector
to make predictions.-
classmethod
handle_location
(location)[source]¶ This class method can be overridden to implement custom logic when a user shares a location with the Agent. This can happen in two ways:
Through an Assistant. Both Google and Alexa have facilities to request a User’s location and send it to the Agent (e.g. https://developers.google.com/assistant/df-asdk/helpers#user_information). In this case the Connector will receive a fulfillment request with location info, and will call this method to understand how to react.
Through explicit location sharing in a messaging platform such as Whatsapp and Telegram. In this case a message gateway service should be responsible for routing messages and predicted responses from the platform to the Connector and vice versa. Upon receiving a shared location, the gateway service is supposed to call a special method on the connector, and from there the flow is similar to the one described above.
By default, this will just return the Agent’s fallback intent (if defined).
- Parameters
location (Location) – A Location payload
- Returns
The Intent to trigger in response of the shared location
- Return type
-
classmethod
register
(resource)[source]¶ Register the given resource in Agent. The resource could be:
An
Intent
A module. In this case, the module is scanned (non recursively) for Intents, and each Intent is added individually
This is how you register a single intent:
from intents import Agent, Intent class MyAgent(Agent): pass @dataclass class MyTestIntent(Intent): """A little docstring for my Intent...""" a_parameter: Sys.Date another_parameter: Sys.Person MyAgent.register(MyTestIntent)
Alternatively, you can register a whole module containing Intents. This is how you register all the intents that are defined in the smalltalk module of example_agent:
from example_agent import smalltalk class MyAgent(Agent): pass MyAgent.register(smalltalk)
Note that together with each Intent, the Agent will register all of its linked properties and resources, such as parameters and Entities.
- Parameters
resource (
Union
[Type
[Intent
],module
]) – The resource to register (an Intent, or a module containing Intents)
-
classmethod
-
class
RegisteredParameter
(metadata, used_in)[source]¶ Agents register intent parameters. If two intents declare the same parameter name, they must also declare the same type for it.
- Parameters
metadata (
IntentParameter
) –used_in (
List
[Type
[Intent
]]) –