botiverse.models.TRIPPY package#

Submodules#

botiverse.models.TRIPPY.TRIPPY module#

This Module has the TRIPPY model.

class botiverse.models.TRIPPY.TRIPPY.TRIPPY(n_slots, hid_dim, n_oper, dropout, from_scratch, BERT_config=<botiverse.models.BERT.config.BERTConfig object>, TRIPPY_config=<botiverse.models.TRIPPY.config.TRIPPYConfig object>)[source]#

Bases: Module

TRIPPY (Task-oriented Reasoning and Inference for Pre-trained models with Pre-trained Ypesystem) model.

This class implements the TRIPPY model for task-oriented dialogue understanding and slot filling.

Parameters:
  • n_slots (int) – The number of slots, corresponding to the number of dialogue slots to be filled.

  • hid_dim (int) – The hidden dimension size.

  • n_oper (int) – The number of operations.

  • dropout (float) – The dropout rate.

  • from_scratch (bool) – Whether to build the BERT model from scratch or load pre-trained weights, defaults to False.

  • BERT_config (BERTConfig) – The configuration for the BERT model, defaults to BERTConfig().

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(ids, mask, token_type_ids, inform_aux_features, ds_aux_features)[source]#

Forward pass of the TRIPPY model.

Parameters:
  • ids (torch.Tensor, shape [batch size, seq len]) – The input token IDs.

  • mask (torch.Tensor, shape [batch size, seq len]) – The attention mask indicating which tokens are valid.

  • token_type_ids (torch.Tensor, shape [batch size, seq len]) – The token type IDs.

  • inform_aux_features (torch.Tensor, shape [batch size, n_slots]) – The auxiliary features for informing slots.

  • ds_aux_features (torch.Tensor, shape [batch size, n_slots]) – The auxiliary features for dialogue state tracking.

Returns:

Tuple containing the logits for slot start positions, slot end positions, slot operations, and slot references.

Return type:

tuple(torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor)

training: bool#

botiverse.models.TRIPPY.TRIPPY_DST module#

This Module has base code and interfaces for TripPy Dialogue State Tracker.

class botiverse.models.TRIPPY.TRIPPY_DST.TRIPPYDST(domains, slot_list, label_maps, non_referable_slots, non_referable_pairs, from_scratch, BERT_config, TRIPPY_config=<botiverse.models.TRIPPY.config.TRIPPYConfig object>)[source]#

Bases: object

TRIPPYDST is a class that represents the TripPy Dialogue State Tracker.

It provides methods for loading the model, training the model, updating the dialogue state, getting the current dialogue state, deleting slots, resetting the tracker, and displaying the tracker information.

Parameters:
  • domains (list[str]) – The list of domains to consider.

  • slot_list (list[str]) – List of slot names.

  • label_maps (dict[str, list]) – Dictionary of the variants of the slot-values that are mapped to the canonical slot-values.

  • non_referable_slots (list[str]) – The list of non-referable slots.

  • non_referable_pairs (list[tuple[str, str]]) – The list of non-referable slot pairs.

  • from_scratch (bool) – Whether to train the model from scratch.

  • TRIPPY_config (TRIPPYConfig, optional) – The configuration for the TRIPPY model, defaults to TRIPPYConfig()

save_model(model_path)[source]#

Save the trained model.

Parameters:

model_path (str) – The path to save the model.

load_model(model_path, pretrained, test_path)[source]#

Load the trained model if path is given, else load a pretrained model.

Parameters:
  • model_path (str) – The path to the saved model.

  • test_path (str) – The path to the test data for evaluation.

train(train_path, dev_path=None, test_path=None)[source]#

Train the model.

Parameters:
  • train_path (str) – The path to the training data.

  • dev_path (str) – The path to the development data for evaluation during training.

  • test_path (str) – The path to the test data for evaluation after training.

update_state(sys_utter, user_utter, inform_mem)[source]#

Update the dialogue state based on the system and user utterances.

Parameters:
  • sys_utter (str) – The system utterance.

  • user_utter (str) – The user utterance.

  • inform_mem (dict[str, list[str]]) – The inform memory containing previous slot-value pairs.

Returns:

The updated dialogue state.

Return type:

dict[str, str]

get_dialogue_state()[source]#

Get a copy of the current dialogue state.

Returns:

A copy of the dialogue state.

Return type:

dict[str, str]

is_all_slots_filled()[source]#

Check if all slots are filled.

Returns:

True if all slots are filled, False otherwise.

Return type:

bool

delete_slots(domain, slot)[source]#

Delete slots from the dialogue state.

If a domain is specified, all slots in that domain will be deleted. If a slot is specified, that specific slot will be deleted. If neither domain nor slot is specified, all slots will be deleted.

Parameters:
  • domain (str) – The domain to delete slots from.

  • slot (str) – The slot to delete.

reset()[source]#

Reset the dialogue state.

Remove all slots from the dialogue state and clear the history.

botiverse.models.TRIPPY.config module#

This Module has the configuration class for TRIPPY.

class botiverse.models.TRIPPY.config.TRIPPYConfig(max_len=128, train_batch_size=32, dev_batch_size=1, test_batch_size=1, epochs=15, hid_dim=768, n_oper=7, dropout=0.3, vocab_path='vocab.txt', ignore_idx=-100, oper2id={'carryover': 0, 'dontcare': 1, 'inform': 6, 'no': 5, 'refer': 3, 'update': 2, 'yes': 4}, weight_decay=0.0, lr=0.0001, adam_epsilon=1e-06, warmup_proportion=0.1, multiwoz=False)[source]#

Bases: object

Configuration class for TRIPPY.

This class holds the configuration parameters for the TRIPPY model.

Parameters:
  • max_len (int) – The maximum sequence length, defaults to 128.

  • train_batch_size (int) – The batch size for training, defaults to 32.

  • dev_batch_size (int) – The batch size for development evaluation, defaults to 1.

  • test_batch_size (int) – The batch size for testing, defaults to 1.

  • epochs (int) – The number of training epochs, defaults to 15.

  • hid_dim (int) – The hidden dimension size, defaults to 768.

  • n_oper (int) – The number of operations, defaults to 7.

  • dropout (float) – The dropout rate, defaults to 0.3.

  • vocab_path (str) – The path to the vocabulary file, defaults to ‘vocab.txt’.

  • ignore_idx (int) – The index value to ignore, defaults to -100.

  • oper2id (dict[str, int]) – The mapping of operation names to IDs, defaults to {‘carryover’ : 0, ‘dontcare’: 1, ‘update’:2, ‘refer’:3, ‘yes’:4, ‘no’:5, ‘inform’:6}.

  • weight_decay (float) – The weight decay value, defaults to 0.0.

  • lr (float) – The learning rate, defaults to 1e-4.

  • adam_epsilon (float) – The epsilon value for Adam optimizer, defaults to 1e-6.

  • warmup_proportion (float) – The proportion of warmup steps, defaults to 0.1.

  • multiwoz (str) – The path to the MultiWOZ dataset, defaults to False.

botiverse.models.TRIPPY.data module#

This Module contains the data processing functions for TRIPPY.

botiverse.models.TRIPPY.data.fix_slot_list(slot_list, domains)[source]#

Fix slot list by filtering slots based on domains.

Parameters:
  • slot_list (list[str]) – List of slot names.

  • domains (list[str]) – The list of domains to filter the slots.

Returns:

The new fixed and sorted slot list.

Return type:

list[str]

botiverse.models.TRIPPY.data.read_raw_data(data_path, slot_list, max_len, domains, multiwoz)[source]#

Read raw data from the JSON file and preprocess it.

Parameters:
  • data_path (str) – The path to the JSON data file.

  • slot_list (list[str]) – The list of slots.

  • max_len (int) – The maximum length of the input sequence.

  • domains (list[str]) – The list of domains.

Returns:

The list of raw data instances.

Return type:

list[RawDataInstance]

botiverse.models.TRIPPY.data.create_slot_span(input, target_value, tok_input_offsets, padding_len, label_maps)[source]#

Create a slot span given the input, target value, and token input offsets, by matching the target value as tokens with the input sequence.

Parameters:
  • input (str) – The input string.

  • target_value (str) – The target value.

  • tok_input_offsets (list[tuple[int, int]]) – The token input offsets.

  • padding_len (int) – The padding length.

  • label_maps (dict) – The label maps.

Returns:

The slot span, span start index, and span end index.

Return type:

tuple[list[int], int, int]

botiverse.models.TRIPPY.data.create_inputs(history, user_utter, sys_utter, tokenizer, max_len)[source]#

Create inputs for BERT using the history, user utterance, system utterance, by creating and tokenizing the input seqence and creating the masks.

Parameters:
  • history (list[str]) – The history of utterances.

  • user_utter (str) – The user’s utterance.

  • sys_utter (str) – The system’s utterance.

  • tokenizer (transformers.PreTrainedTokenizer) – The tokenizer to tokenize the input.

  • max_len (int) – The maximum length of the input.

Returns:

The input string, token IDs, attention mask, token type IDs, token input offsets, tokenized input tokens, and padding length.

Return type:

tuple[str, list[int], list[int], list[int], list[tuple[int, int]], list[str], int]

botiverse.models.TRIPPY.data.is_informed(value, target, label_maps, multiwoz)[source]#

Check if a value is informed by the system given a target value and label maps.

Parameters:
  • value (str) – The value to check.

  • target (str) – The target value.

  • label_maps (dict) – The label maps.

Returns:

A tuple indicating if the value is informed and the informed value.

Return type:

tuple[bool, str]

botiverse.models.TRIPPY.data.get_refered_slot(target_value, slot, last_state, non_referable_slots, non_referable_pairs, label_maps={})[source]#

Get the referred slot if the user refers to another slot in the dialogue state given a target value, slot, last state, non-referable slots, non-referable pairs, and label maps.

Parameters:
  • target_value (str) – The target value.

  • slot (str) – The slot to check.

  • last_state (dict) – The last state.

  • non_referable_slots (list[str]) – The list of non-referable slots.

  • non_referable_pairs (list[tuple[str, str]]) – The list of non-referable slot pairs.

  • label_maps (dict, optional) – The label maps.

Returns:

The referred slot.

Return type:

str

botiverse.models.TRIPPY.data.create_labels(target_value, slot, last_state, input, tok_input_offsets, inform_mem, label_maps, padding_len, max_len, non_referable_slots, non_referable_pairs, multiwoz)[source]#

Create the target operation and the span labels for a slot.

Parameters:
  • target_value (str) – The target value.

  • slot (str) – The slot.

  • last_state (dict) – The last state.

  • input (str) – The input string.

  • tok_input_offsets (list[tuple[int, int]]) – The token input offsets.

  • inform_mem (dict) – The inform memory.

  • label_maps (dict) – The label maps.

  • padding_len (int) – The padding length.

  • max_len (int) – The maximum length of the input.

  • non_referable_slots (list[str]) – The list of non-referable slots (slots that can not use refering).

  • non_referable_pairs (list[tuple[str, str]]) – The list of non-referable slot pairs (slots pairs that can not refer to each other).

Returns:

The operation, span, span start index, span end index, referred slot, and informed value.

Return type:

tuple[str, list[int], int, int, str, str]

botiverse.models.TRIPPY.data.create_data(raw_data, slot_list, label_maps, tokenizer, max_len, non_referable_slots, non_referable_pairs, multiwoz)[source]#

Create the data instances for training or evaluation.

Parameters:
  • raw_data (list[RawDataInstance]) – The list of raw data instances.

  • slot_list (list[str]) – The list of slots.

  • label_maps (dict) – The label maps.

  • tokenizer (transformers.PreTrainedTokenizer) – The tokenizer to tokenize the input.

  • max_len (int) – The maximum length of the input.

  • non_referable_slots (list[str]) – The list of non-referable slots.

  • non_referable_pairs (list[tuple[str, str]]) – The list of non-referable slot pairs.

Returns:

The list of data instances.

Return type:

list[DataInstance]

botiverse.models.TRIPPY.data.prepare_data(data_path, slot_list, label_maps, tokenizer, max_len, domains, non_referable_slots, non_referable_pairs, multiwoz)[source]#

Prepare the data for training or evaluation, this usually the function you want to call to preprocess the data for TripPy model, it encapsulates the whole process of preprcessing the data by calling the other functions in this module.

Parameters:
  • data_path (str) – The path to the JSON data file.

  • slot_list (list[str]) – The list of slots.

  • label_maps (dict) – The label maps.

  • tokenizer (transformers.PreTrainedTokenizer) – The tokenizer to tokenize the input.

  • max_len (int) – The maximum length of the input.

  • domains (list[str]) – The list of domains.

  • non_referable_slots (list[str]) – The list of non-referable slots.

  • non_referable_pairs (list[tuple[str, str]]) – The list of non-referable slot pairs.

Returns:

The raw data and prepared data.

Return type:

tuple[list[RawDataInstance], list[DataInstance]]

class botiverse.models.TRIPPY.data.Dataset(data, n_slots, oper2id, slot_list)[source]#

Bases: Dataset

PyTorch Dataset for the TRIPPY model.

Parameters:
  • data (list[DataInstance]) – The list of data instances.

  • n_slots (int) – The number of slots.

  • oper2id (dict[str, int]) – The mapping of operations to IDs.

  • slot_list (list[str]) – The list of slots.

botiverse.models.TRIPPY.evaluate module#

This Module has the evaluation functions for TRIPPY.

botiverse.models.TRIPPY.evaluate.get_informed_value(value, target, label_maps, multiwoz)[source]#

Get the informed value based on the value and target, taking into account label maps.

Parameters:
  • value (str) – The original value.

  • target (str) – The target value to compare with.

  • label_maps (dict) – The mapping of slot values to their variants.

Returns:

The informed value.

Return type:

str

botiverse.models.TRIPPY.evaluate.eval(raw_data, data, model, device, n_slots, slot_list, label_maps, oper2id, multiwoz)[source]#

Evaluate the model on the given data.

Parameters:
  • raw_data (list) – The raw data.

  • data (list) – The processed data.

  • model (nn.Module) – The model to evaluate.

  • device (torch.device) – The device to run the evaluation on.

  • n_slots (int) – The number of slots.

  • slot_list (list) – The list of slots.

  • label_maps (dict) – The mapping of slot values to their variants.

  • oper2id (dict) – The mapping of operations to their IDs.

Returns:

The evaluation metrics.

Return type:

tuple

botiverse.models.TRIPPY.infer module#

This Module has the inference functions for TRIPPY.

botiverse.models.TRIPPY.infer.infer(model, slot_list, current_state, history, sys_utter, user_utter, inform_mem, device, oper2id, tokenizer, max_len)[source]#

Infer the dialogue state using the TRIPPY model.

Parameters:
  • model (TRIPPY) – The TRIPPY model for inference.

  • slot_list (list) – The list of slots.

  • current_state (dict) – The current dialogue state.

  • history (list) – The dialogue history.

  • sys_utter (str) – The system’s utterance.

  • user_utter (str) – The user’s utterance.

  • inform_mem (dict) – The inform memory.

  • device (torch.device) – The device to run the inference on.

  • oper2id (dict) – The mapping of operations to IDs.

  • tokenizer (transformers.PreTrainedTokenizer) – The tokenizer to tokenize the input.

  • max_len (int) – The maximum length of the input sequence.

Returns:

The predicted dialogue state.

Return type:

dict

botiverse.models.TRIPPY.run module#

This Module has the run functions for TRIPPY that train and evaluate the model.

botiverse.models.TRIPPY.run.run(model, domains, slot_list, label_maps, train_path, dev_path, test_path, device, non_referable_slots, non_referable_pairs, model_path, TRIPPY_config)[source]#

Train and evaluate the TRIPPY model.

Parameters:
  • model (TRIPPY) – The TRIPPY model.

  • domains (list) – The domains to consider in the dataset.

  • slot_list (list) – The list of slots.

  • label_maps (dict) – The mapping of slot values to their variants.

  • train_path (str) – The path to the training dataset in JSON format.

  • dev_path (str) – The path to the development dataset in JSON format.

  • test_path (str) – The path to the testing dataset in JSON format.

  • device (torch.device) – The device to train and evaluate the model on.

  • non_referable_slots (list) – The slots that are not referable.

  • non_referable_pairs (list) – The pairs of slots that are not referable.

  • model_path (str) – The path to save the best model.

  • TRIPPY_config (TRIPPYConfig) – The configuration for TRIPPY.

botiverse.models.TRIPPY.train module#

This Module has the training functions for TRIPPY.

botiverse.models.TRIPPY.train.span_loss_fn(start_logits, end_logits, targets_start, targets_end, ignore_idx)[source]#

Compute the span loss.

Parameters:
  • start_logits (torch.Tensor) – The start logits.

  • end_logits (torch.Tensor) – The end logits.

  • targets_start (torch.Tensor) – The start targets.

  • targets_end (torch.Tensor) – The end targets.

  • ignore_idx (int) – The index to ignore in the loss calculation.

Returns:

The span loss.

Return type:

torch.Tensor

botiverse.models.TRIPPY.train.oper_loss_fn(oper_logits, oper_labels, ignore_idx)[source]#

Compute the operation loss.

Parameters:
  • oper_logits (torch.Tensor) – The operation logits.

  • oper_labels (torch.Tensor) – The operation labels.

  • ignore_idx (int) – The index to ignore in the loss calculation.

Returns:

The operation loss.

Return type:

torch.Tensor

botiverse.models.TRIPPY.train.refer_loss_fn(refer_logits, refer_labels, ignore_idx)[source]#

Compute the refer loss.

Parameters:
  • refer_logits (torch.Tensor) – The refer logits.

  • refer_labels (torch.Tensor) – The refer labels.

  • ignore_idx (int) – The index to ignore in the loss calculation.

Returns:

The refer loss.

Return type:

torch.Tensor

botiverse.models.TRIPPY.train.train(data_loader, model, optimizer, device, scheduler, n_slots, ignore_idx, oper2id)[source]#

Perform the training loop for a model on the given data.

Parameters:
  • data_loader (DataLoader) – The data loader providing the training batches.

  • model (nn.Module) – The model to be trained.

  • optimizer (Optimizer) – The optimizer used to update the model’s parameters.

  • device (torch.device) – The device (e.g., CPU or GPU) on which the training will be performed.

  • scheduler (_LRScheduler) – The scheduler for adjusting the learning rate during training.

  • n_slots (int) – The number of slots in the task.

  • ignore_idx (int) – The index to ignore during loss computation.

  • oper2id (dict) – A dictionary mapping operation names to their corresponding IDs.

botiverse.models.TRIPPY.utils module#

class botiverse.models.TRIPPY.utils.RawDataInstance(dial_idx, turn_idx, user_utter, sys_utter, history, turn_slots, inform_mem)[source]#

Bases: object

Represents a raw data instance.

Parameters:
  • dial_idx (str) – Dialogue index.

  • turn_idx (int) – Turn index.

  • user_utter (str) – User utterance.

  • sys_utter (str) – System utterance.

  • history (list[str]) – Dialogue history.

  • turn_slots (dict[str, str]) – Slots for the current turn.

  • inform_mem (dict[str, list[str]]) – Informed slots from previous turns.

class botiverse.models.TRIPPY.utils.DataInstance(ids, mask, token_type_ids, spans, spans_start, spans_end, padding_len, input_tokens, input, opers, target_values, last_state, cur_state, refer, inform_aux_features, ds_aux_features)[source]#

Bases: object

Represents a processed data instance.

Parameters:
  • ids (list[int]) – Input IDs.

  • mask (list[int]) – Attention mask.

  • token_type_ids (list[int]) – Token type IDs.

  • spans (list[int]) – Spans.

  • spans_start (list[int]) – Start positions of spans.

  • spans_end (list[int]) – End positions of spans.

  • padding_len (int) – Padding length.

  • input_tokens (str) – Input tokens.

  • input (str) – Input text.

  • opers (list[int]) – Slot operations.

  • target_values (list[str]) – Target slot values.

  • last_state (dict[str, str]) – Last dialogue state.

  • cur_state (dict[str, str]) – Current dialogue state.

  • refer (list[int]) – Referenced slots.

  • inform_aux_features (list[float]) – Informed auxiliary features.

  • ds_aux_features (list[float]) – Filled slot auxiliary features.

class botiverse.models.TRIPPY.utils.AverageMeter[source]#

Bases: object

Computes and stores the average and current value.

reset()[source]#

Reset the average meter.

update(val, n=1)[source]#

Update the average meter with a new value.

Parameters:
  • val (float) – New value.

  • n (int) – Number of instances the value represents.

botiverse.models.TRIPPY.utils.normalize(text, multiwoz)[source]#

Normalize the given text by converting it to lowercase and splitting it into tokens.

Parameters:

text (str) – Input text.

Returns:

Normalized tokens.

Return type:

list[str]

botiverse.models.TRIPPY.utils.is_included(value, target)[source]#

Check if the target is included in the value.

Parameters:
  • value (str) – The value to check.

  • target (str) – The target value to search for.

Returns:

True if the target is included in the value, False otherwise.

Return type:

bool

botiverse.models.TRIPPY.utils.included_with_label_maps(value, target, label_maps)[source]#

Check if the value is included in the target or any of its variants based on the label maps.

Parameters:
  • value (str) – The value to check.

  • target (str) – The target value to search for.

  • label_maps (dict[str, list[str]]) – Dictionary of label maps.

Returns:

True if the value is included in the target or any of its variants, False otherwise.

Return type:

bool

botiverse.models.TRIPPY.utils.match_with_label_maps(value, target, label_maps={})[source]#

Check if the value matches the target or any of its variants based on the label maps.

Parameters:
  • value (str) – The value to check.

  • target (str) – The target value to match against.

  • label_maps (dict[str, list[str]]) – Dictionary of label maps.

Returns:

True if the value matches the target or any of its variants, False otherwise.

Return type:

bool

botiverse.models.TRIPPY.utils.create_span_output(output_start, output_end, padding_len, input_tokens)[source]#

Create the span output based on the output start and end positions.

Parameters:
  • output_start (list[int]) – Output start positions.

  • output_end (list[int]) – Output end positions.

  • padding_len (int) – Padding length.

  • input_tokens (str) – Input tokens.

Returns:

The created span output.

Return type:

str

botiverse.models.TRIPPY.utils.mask_utterance(utter, inform_mem, multiwoz, replace_with='[UNK]')[source]#

Mask the utterance by replacing the informed values in the inform memory.

Parameters:
  • utter (list[str]) – The utterance to mask.

  • inform_mem (dict[str, list[str]]) – The inform memory containing slot-value pairs.

  • replace_with (str) – The replacement token.

Returns:

The masked utterance.

Return type:

list[str]

botiverse.models.TRIPPY.utils.normalize_time(text)[source]#

Normalize the time format in the given text (specific to MultiWoz dataset).

Parameters:

text (str) – The input text.

Returns:

The normalized text.

Return type:

str

botiverse.models.TRIPPY.utils.normalize_text(text)[source]#

Normalize the text (specific to MultiWoz dataset).

Parameters:

text (str) – The input text.

Returns:

The normalized text.

Return type:

str

Module contents#