nlp_engine#

class baf.nlp.nlp_engine.NLPEngine(agent)[source]#

Bases: object

The NLP Engine of an agent.

It is in charge of running different Natural Language Processing tasks required by the agent.

Parameters:

agent (Agent) – the agent the NLPEngine belongs to

_agent#

The agent the NLPEngine belongs to

Type:

Agent

_llms#

The LLMs of the NLPEngine. Keys are the names and values are the LLMs themselves.

Type:

dict[str, LLM]

_intent_classifiers#

The collection of Intent Classifiers of the NLPEngine. There is one for each agent state (only states with transitions triggered by intent matching)

Type:

dict[State, IntentClassifier]

_ner#

The NER (Named Entity Recognition) system of the NLPEngine

Type:

NER or None

_language_to_speech2text_module#

A dictionary mapping the user language to a Speech-to-Text system of the NLPEngine. The user language is either automatically recognized if audio_language_detection_processor is set, or it can be set by the user, defaults to english. Keys are the language names and values are the Speech2Text system itself.

Type:

dict[str, Speech2Text]

_language_to_text2speech_module#

A dictionary mapping the user language to a Text-to-Speech system of the NLPEngine. The user language is set by the user, defaults to english. Keys are the language names and values are the Text2Speech system itself.

Type:

dict[str, Text2Speech]

_rag#

The RAG system of the NLPEngine

Type:

RAG

get_best_intent_prediction(intent_classifier_predictions)[source]#

Get the best intent prediction out of a list of intent predictions. If none of the predictions is well enough to be considered, return nothing.

Parameters:

intent_classifier_predictions (list[IntentClassifierPrediction]) –

Returns:

the best intent prediction or None if no intent prediction is well

enough

Return type:

IntentClassifierPrediction or None

get_property(prop)[source]#

Get a NLP property’s value from the NLPEngine’s agent.

Parameters:

prop (Property) – the property to get its value

Returns:

the property value, or None if the property is not an NLP property

Return type:

Any

initialize()[source]#

Initialize the NLPEngine.

property ner#

NLPEngine NER component.

Type:

NER

predict_intent(session)[source]#

Predict the intent of a user message.

Parameters:

session (Session) – the user session

Returns:

the intent prediction

Return type:

IntentClassifierPrediction

speech2text(session, speech)[source]#

Transcribe a voice audio into its corresponding text representation.

Parameters:
  • session (Session) – The user session

  • speech (bytes) – the recorded voice that wants to be transcribed

Returns:

the speech transcription

Return type:

str

text2speech(session, text)[source]#

Synthesize a text into its corresponding voice audio.

Parameters:
  • session (Session) – The Session of the Agent the Text2Speech system belongs to

  • text (str) – the text that wants to be synthesized

Returns:

the speech synthesis as a dictionary containing 2 keys:

  • audio (np.ndarray): the generated audio waveform as a numpy array with dimensions (nb_channels, audio_length), where nb_channels is the number of audio channels (usually 1 for mono) and audio_length is the number of samples in the audio

  • sampling_rate (int): an integer value containing the sampling rate, eg. how many samples correspond to one second of audio

Return type:

dict

train()[source]#

Train the NLP components of the NLPEngine.