Snips NLU (experimental)¶
Snips NLU is an open source Python/Rust library for Natural Language Understanding. It provides intent classification and entity (slot) tagging, and it runs locally as a standard Python library.
SnipsConnector
is a Connector that doesn’t require any other service
(nor remote or local) to run. It makes predictions internally, by calling the
underlying snips-nlu library functions.
To use SnipsConnector
it is necessary to install its optional
dependency group:
pip install intents[snips]
More details about Snips can be found at
-
class
SnipsConnector
(agent_cls, default_session=None, default_language=None)[source]¶ This is a
Connector
that runs entirely locally, without needing any resident service to operate. Predictions are made by calling the snips-nlu Python API.Warning
SnipsConnector is experimental: expect running into relevant rough edges when using it. Its main limitations include:
Intent relations are not implemented
Prompts for required parameters are not implemented: an exception will be thrown if all the required parameters are not present
Sys.Email
,Sys.PhoneNumber
andUrl
entities are patched with empty placeholdersSys.Date
is only available in English
- Parameters
agent_cls (
Type
[Agent
]) – The Agent class to train the systemdefault_session (
Optional
[str
]) – A default session identifier. Will be generated randomly if Nonedefault_language (
Union
[LanguageCode
,str
,None
]) – Default language for predictions. English will be used if None.
-
export
(destination)[source]¶ Export Agent in the given folder:
from example_agent.agent import ExampleAgent from intents.connectors._experimental.snips import SnipsConnector snips = SnipsConnector(ExampleAgent) snips.export("./TMP_SNIPS")
The export will generate one JSON file per language, they can be loaded into Snips as a JSON Dataset.
- Parameters
destination (
str
) – A folder that will contain exported JSON files
-
fulfill
(fulfillment_request)[source]¶ Not implemented
- Parameters
fulfillment_request (
FulfillmentRequest
) –- Return type
dict
-
predict
(message, session=None, language=None)[source]¶ Predict the given User message in the given session using the given language. When session or language are None, predict will use the default values that are specified in
__init__()
.predict will return an instance of
Prediction
, representing the service response.>>> from intents.connectors._experimental.snips import SnipsConnector >>> from example_agent import ExampleAgent >>> snips = SnipsConnector(ExampleAgent) >>> snips.upload() # This trains the models >>> prediction = snips.predict("Hi, my name is Guido") >>> prediction.intent UserNameGive(user_name='Guido') >>> prediction.intent.user_name "Guido" >>> prediction.fulfillment_text "Hi Guido, I'm Bot" >>> prediction.confidence 0.62
Note that the Italian version of
ExampleAgent
won’t be trained, asSys.Date
is not available for the Italian language in Snips.- Parameters
message (
str
) – The User message to predictsession (
Optional
[str
]) – Any string identifying a conversationlanguage (
Union
[LanguageCode
,str
,None
]) – A LanguageCode object, or a ISO 639-1 string (e.g. “en”)
- Return type
SnipsPrediction
-
trigger
(intent, session=None, language=None)[source]¶ As Snips runs locally and intent relation resolution is not supported, Triggers don’t do much more at the moment than returing the intent as it was passed in input.
>>> from intents.connectors._experimental.snips import SnipsConnector >>> from example_agent import ExampleAgent, smalltalk >>> snips = SnipsConnector(ExampleAgent) >>> snips.upload() # This trains the models >>> prediction = snips.trigger(smalltalk.AgentNameGive(agent_name='Alice')) >>> prediction.intent AgentNameGive(agent_name='Alice') >>> prediction.fulfillment_text "Howdy Human, I'm Alice" >>> prediction.confidence 1.0
- Parameters
intent (
Intent
) – The Intent instance to triggersession (
Optional
[str
]) – Any string identifying a conversationlanguage (
Union
[LanguageCode
,str
,None
]) – A LanguageCode object, or a ISO 639-1 string (e.g. “en”)
- Return type
SnipsPrediction
-
upload
()[source]¶ As Snips runs locally as a Python library, there is no external service to upload the model to. Instead, upload will train Snips local models.
Currently there is no persistence for trained models. This means that upload should be called every time
SnipsConnector
is instantiated.