1// 2// Copyright (C) 2018 The Android Open Source Project 3// 4// Licensed under the Apache License, Version 2.0 (the "License"); 5// you may not use this file except in compliance with the License. 6// You may obtain a copy of the License at 7// 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// See the License for the specific language governing permissions and 14// limitations under the License. 15// 16 17include "annotator/model.fbs"; 18include "utils/codepoint-range.fbs"; 19include "utils/flatbuffers.fbs"; 20include "utils/intents/intent-config.fbs"; 21include "utils/resources.fbs"; 22include "utils/tokenizer.fbs"; 23include "utils/zlib/buffer.fbs"; 24 25file_identifier "TC3A"; 26 27// TensorFlow Lite model for suggesting actions. 28namespace libtextclassifier3; 29table TensorflowLiteModelSpec { 30 // TensorFlow Lite model for suggesting actions. 31 tflite_model:[ubyte] (force_align: 16); 32 33 // Input specification. 34 // (num messages,) int32 tensor, the user id per message. 35 input_user_id:int = 0; 36 37 // (num messages,) string tensor, each message of the conversation. 38 input_context:int = 1; 39 40 // int, the number of messages in the conversation. 41 input_context_length:int = 2; 42 43 // (num messages,) float tensor, the time difference in seconds of the 44 // messages in the conversation. 45 input_time_diffs:int = 3; 46 47 // int, the number of smart replies to produce. 48 input_num_suggestions:int = 4; 49 50 // float, the output diversification distance parameter. 51 input_diversification_distance:int = -1; 52 53 // float, the empirical probability factor parameter. 54 input_empirical_probability_factor:int = -1; 55 56 // float, the confidence threshold. 57 input_confidence_threshold:int = -1; 58 59 // Input port for hashed and embedded tokens, a (num messages, max tokens, 60 // embedding size) float tensor specifying the embeddings of each token of 61 // each message in the conversation. 62 input_token_embeddings:int = -1; 63 64 // Input port for the number of tokens per message. 65 // (num messages) int32 tensor specifying the number of tokens in each message 66 // in the conversation. 67 input_num_tokens:int = -1; 68 69 // Output specification. 70 output_replies:int = 0; 71 72 output_replies_scores:int = 1; 73 output_sensitive_topic_score:int = 3; 74 output_triggering_score:int = 4; 75 output_actions_scores:int = 5; 76 77 // Model setup. 78 // When true, the inputs are resized to the concrete input sizes before 79 // inference otherwise, it's assumed that the model has the correct input 80 // shapes set. 81 resize_inputs:bool = false; 82 83 // Input port for the hashed, embedded and flattened/concatenated tokens. 84 // A (max tokens, embedding_size) float tensor specifying the embeddings of 85 // each token. 86 input_flattened_token_embeddings:int = -1; 87} 88 89// Configuration for the tokenizer. 90namespace libtextclassifier3; 91table ActionsTokenizerOptions { 92 type:TokenizationType = INTERNAL_TOKENIZER; 93 94 // If true, white space tokens will be kept when using the icu tokenizer. 95 icu_preserve_whitespace_tokens:bool = false; 96 97 // Codepoint ranges that determine what role the different codepoints play 98 // during tokenized. The ranges must not overlap. 99 tokenization_codepoint_config:[TokenizationCodepointRange]; 100 101 // A set of codepoint ranges to use in the mixed tokenization mode to identify 102 // stretches of tokens to re-tokenize using the internal tokenizer. 103 internal_tokenizer_codepoint_ranges:[CodepointRange]; 104 105 // If true, tokens will be also split when the codepoint's script_id changes 106 // as defined in TokenizationCodepointRange. 107 tokenize_on_script_change:bool = false; 108} 109 110// Configuration for the feature processor. 111namespace libtextclassifier3; 112table ActionsTokenFeatureProcessorOptions { 113 // Tokenizer options. 114 tokenizer_options:ActionsTokenizerOptions; 115 116 // Serialized TensorFlow Lite model with weights for the token embeddings. 117 embedding_model:[ubyte] (force_align: 16); 118 119 // Size of the embedding. 120 embedding_size:int = -1; 121 122 // Number of bits for quantization for embeddings. 123 embedding_quantization_bits:int = 8; 124 125 // Number of buckets used for hashing charactergrams. 126 num_buckets:int = -1; 127 128 // Orders of charactergrams to extract, e.g. 2 means character bigrams, 3 129 // character trigrams etc. 130 chargram_orders:[int]; 131 132 // Whether to extract the token case feature. 133 extract_case_feature:bool; 134 135 // If true, will use the unicode-aware functionality for extracting features. 136 unicode_aware_features:bool; 137 138 // Regexp features to extract. 139 regexp_features:[string]; 140 141 // Whether to remap digits to a single number. 142 remap_digits:bool; 143 144 // Whether to lowercase all tokens. 145 lowercase_tokens:bool; 146 147 // Maximum length of a word. 148 max_token_length:int = 20; 149 150 // The `max_num_tokens_per_message` and `min_num_tokens_per_message` are 151 // applied when tokens are embedded per message. 152 // If set and the number of tokens of a message is bigger than this limit, 153 // tokens at the beginning of the message are dropped to fit the limit. 154 max_num_tokens_per_message:int = -1; 155 156 // If set, the tokens of each message will be padded to this fixed number of 157 // tokens. 158 min_num_tokens_per_message:int = -1; 159 160 // If set and the total number of concatenated tokens is bigger than this 161 // limit, tokens at the start of the conversation are dropped. 162 max_num_total_tokens:int = -1; 163 164 // If set and the total number of concatenaed tokens is smaller than this 165 // limit, the conversation is padded with padding tokens. 166 min_num_total_tokens:int = -1; 167 168 // Id that is used as encoding of the padding token. 169 padding_token_id:int = 0; 170 171 // Id that is used as encoding of the start of message token. 172 start_token_id:int = 1; 173 174 // Id that is used as encoding of the end of message token. 175 end_token_id:int = 2; 176} 177 178// N-Gram based linear regression model. 179namespace libtextclassifier3; 180table NGramLinearRegressionModel { 181 // A flat list of all the hashed n-grams concatenated back to back. Elements 182 // should only ever be accessed via the offset table below. 183 hashed_ngram_tokens:[uint]; 184 185 // Offsets to the start of the n-grams in hashed_ngram_tokens. The last 186 // element in this array is the length of hashed_ngrams to make it easier to 187 // compute n-gram lengths. 188 ngram_start_offsets:[ushort]; 189 190 // Weights of the n-grams. 191 ngram_weights:[float]; 192 193 // The default weight assigned to n-grams that weren't matched. 194 default_token_weight:float; 195 196 // Maximum n-gram length to consider when calculating the denominatior. 197 // This should usually be the same as max_ngram_length but can diverge 198 // if additional (longer) n-grams are added to a model as part of a minor 199 // update. 200 max_denom_ngram_length:int; 201 202 // If non-zero, the order of the skip-gram to match. 203 max_skips:int; 204 205 // The threshold above which the model output is considered positive. 206 threshold:float; 207 208 // Model specific tokenizer options. 209 // If not specified, will reuse the feature processor tokenizer. 210 tokenizer_options:ActionsTokenizerOptions; 211} 212 213namespace libtextclassifier3; 214table TriggeringPreconditions { 215 // Lower bound thresholds for the smart reply model prediction output. 216 min_smart_reply_triggering_score:float; 217 218 // Maximum sensitive score for which actions and smart replies are shown. 219 max_sensitive_topic_score:float = 1; 220 221 // Whether to suppress all model output when a conversation is classified as 222 // sensitive. 223 suppress_on_sensitive_topic:bool = true; 224 225 // Thresholds on the model prediction input. 226 // The minimal length of input to consider for prediction. 227 min_input_length:int = 0; 228 229 // The maximal length of input to consider for prediciton, -1 if unbounded. 230 max_input_length:int = -1; 231 232 // Minimal fraction of messages in the input conversation that need to match 233 // a locale that the model can handle. 234 min_locale_match_fraction:float = 0.75; 235 236 handle_missing_locale_as_supported:bool = false; 237 handle_unknown_locale_as_supported:bool = false; 238 239 // Filter input with low-confidence triggers. 240 suppress_on_low_confidence_input:bool = true; 241 242 // Same as low_confidence_rules in ActionsModel. 243 // NOTE: Only fill this when the TriggeringPreconditions are pushed separately 244 // as a flag value (i.e. as overlay). 245 low_confidence_rules:RulesModel; 246 247 // Smart reply thresholds. 248 diversification_distance_threshold:float = 0; 249 250 confidence_threshold:float = 0; 251 empirical_probability_factor:float = 0; 252 min_reply_score_threshold:float = 0; 253} 254 255namespace libtextclassifier3; 256table ActionSuggestionSpec { 257 // Type of the action suggestion. 258 type:string; 259 260 // Text of a smart reply action. 261 response_text:string; 262 263 // Score. 264 score:float; 265 266 // Serialized entity information. 267 serialized_entity_data:string; 268 269 // Priority score used for internal conflict resolution. 270 priority_score:float = 0; 271} 272 273// Options to specify triggering behaviour per action class. 274namespace libtextclassifier3; 275table ActionTypeOptions { 276 // The name of the predicted action. 277 name:string; 278 279 // Triggering behaviour. 280 // Whether the action class is considered in the model output or not. 281 enabled:bool = true; 282 283 // Minimal output score threshold. 284 min_triggering_score:float = 0; 285 286 // The action to trigger. 287 action:ActionSuggestionSpec; 288} 289 290namespace libtextclassifier3.AnnotationActionsSpec_; 291table AnnotationMapping { 292 // The annotation collection. 293 annotation_collection:string; 294 295 // The action name to use. 296 action:ActionSuggestionSpec; 297 298 // Whether to use the score of the annotation as the action score. 299 use_annotation_score:bool = true; 300 301 // Minimum threshold for the annotation score for filtering. 302 min_annotation_score:float; 303 304 // If set, the text of the annotation will be used to set a field in the 305 // action entity data. 306 entity_field:FlatbufferFieldPath; 307} 308 309// Configuration for actions based on annotatations. 310namespace libtextclassifier3; 311table AnnotationActionsSpec { 312 annotation_mapping:[AnnotationActionsSpec_.AnnotationMapping]; 313 314 // Whether to deduplicate annotations by type and text prior to generating 315 // actions. 316 deduplicate_annotations:bool = true; 317 318 // Annotation usecase to specify for text annotation. 319 annotation_usecase:AnnotationUsecase = ANNOTATION_USECASE_SMART; 320 321 // Maximum number of recent messages to consider from any person. 322 // We consider at most `max_history_from_any_person` many recent messages if 323 // they were received from different users or at most the maximum of this and 324 // `max_history_from_last_person` if they are all from the same user. 325 max_history_from_any_person:int = 1; 326 327 // Maximum number of recent messages to consider from the last person. 328 max_history_from_last_person:int = 1; 329 330 // Whether to include messages from the local user. 331 include_local_user_messages:bool = false; 332 333 // Whether to only consider messages up to the last one sent by the local 334 // user. 335 only_until_last_sent:bool = true; 336 337 // If true, annotator would populare serialized_entity_data in the results. 338 is_serialized_entity_data_enabled:bool = true; 339} 340 341// Ranking options. 342namespace libtextclassifier3; 343table RankingOptions { 344 // When true, actions suggestions are deduplicated by `type`, `response_text` 345 // and associated annotations, keeping the higher scoring actions. 346 deduplicate_suggestions:bool = true; 347 348 // When true, actions are deduplicated by the span they are referring to. 349 deduplicate_suggestions_by_span:bool = true; 350 351 // Optional script to run for ranking and filtering the action suggestions. 352 // The following global variables are available to the script: 353 // * input: (optionally deduplicated) action suggestions, via the `actions` 354 // global 355 // * output: indices of the actions to keep in the provided order. 356 lua_ranking_script:string; 357 358 compressed_lua_ranking_script:CompressedBuffer; 359 360 // If true, suppresses smart replies if other smart actions are suggested. 361 suppress_smart_replies_with_actions:bool = false; 362 363 // If true, keep actions from the same entities together for ranking. 364 group_by_annotations:bool = true; 365} 366 367// Entity data to set from capturing groups. 368namespace libtextclassifier3.RulesModel_.Rule_.RuleActionSpec_; 369table RuleCapturingGroup { 370 // The id of group. 371 group_id:int; 372 373 // If set, the text of the capturing group will be used to set a field 374 // in the action entity data. 375 entity_field:FlatbufferFieldPath; 376 377 // If set, the capturing group will be used to create a text annotation 378 // with the given name and type. 379 annotation_type:string; 380 381 annotation_name:string; 382 383 // If set, the capturing group text will be used to create a text 384 // reply. 385 text_reply:ActionSuggestionSpec; 386} 387 388// The actions to produce upon triggering. 389namespace libtextclassifier3.RulesModel_.Rule_; 390table RuleActionSpec { 391 // The action. 392 action:ActionSuggestionSpec; 393 394 capturing_group:[RuleActionSpec_.RuleCapturingGroup]; 395} 396 397// List of regular expression matchers. 398namespace libtextclassifier3.RulesModel_; 399table Rule { 400 // The regular expression pattern. 401 pattern:string; 402 403 compressed_pattern:CompressedBuffer; 404 actions:[Rule_.RuleActionSpec]; 405 406 // Patterns for post-checking the outputs. 407 output_pattern:string; 408 409 compressed_output_pattern:CompressedBuffer; 410} 411 412// Rule based actions. 413namespace libtextclassifier3; 414table RulesModel { 415 rule:[RulesModel_.Rule]; 416 417 // If true, will compile the regexes only on first use. 418 lazy_regex_compilation:bool = true; 419} 420 421namespace libtextclassifier3; 422table ActionsModel { 423 // Comma-separated list of locales supported by the model as BCP 47 tags. 424 locales:string; 425 426 // Version of the actions model. 427 version:int; 428 429 // A name for the model that can be used e.g. for logging. 430 name:string; 431 432 tflite_model_spec:TensorflowLiteModelSpec; 433 434 // Output classes. 435 smart_reply_action_type:string; 436 437 action_type:[ActionTypeOptions]; 438 439 // Triggering conditions of the model. 440 preconditions:TriggeringPreconditions; 441 442 // Default number of smart reply predictions. 443 num_smart_replies:int = 3; 444 445 // Length of message history to consider, -1 if unbounded. 446 max_conversation_history_length:int = 1; 447 448 // Configuration for mapping annotations to action suggestions. 449 annotation_actions_spec:AnnotationActionsSpec; 450 451 // Configuration for rules. 452 rules:RulesModel; 453 454 // Configuration for intent generation on Android. 455 android_intent_options:IntentFactoryModel; 456 457 // Model resources. 458 resources:ResourcePool; 459 460 // Schema data for handling entity data. 461 actions_entity_data_schema:[ubyte]; 462 463 // Action ranking options. 464 ranking_options:RankingOptions; 465 466 // Lua based actions. 467 lua_actions_script:string; 468 469 compressed_lua_actions_script:CompressedBuffer; 470 471 // Low confidence classifiers. 472 low_confidence_rules:RulesModel; 473 474 low_confidence_ngram_model:NGramLinearRegressionModel; 475 476 // Feature processor options. 477 feature_processor_options:ActionsTokenFeatureProcessorOptions; 478} 479 480root_type libtextclassifier3.ActionsModel; 481