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