- Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathquestion_and_answer.ts
358 lines (329 loc) · 11.9 KB
/
question_and_answer.ts
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
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* 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.
* =============================================================================
*/
import*astfconvfrom'@tensorflow/tfjs-converter';
import*astffrom'@tensorflow/tfjs-core';
import{BertTokenizer,CLS_INDEX,loadTokenizer,SEP_INDEX,Token}from'./bert_tokenizer';
constMODEL_URL='https://tfhub.dev/tensorflow/tfjs-model/mobilebert/1';
constINPUT_SIZE=384;
constMAX_ANSWER_LEN=32;
constMAX_QUERY_LEN=64;
constMAX_SEQ_LEN=384;
constPREDICT_ANSWER_NUM=5;
constOUTPUT_OFFSET=1;
// This is threshold value for determining if a question is irrelevant to the
// context. This value comes from the QnA model, and is generated by the
// training process based on the SQUaD 2.0 dataset.
constNO_ANSWER_THRESHOLD=4.3980759382247925;
exportinterfaceQuestionAndAnswer{
/**
* Given the question and context, find the best answers.
* @param question the question to find answers for.
* @param context context where the answers are looked up from.
* @return array of answers
*/
findAnswers(question: string,context: string): Promise<Answer[]>;
}
/**
* MobileBert model loading is configurable using the following config
* dictionary.
*
* `modelUrl`: An optional string that specifies custom url of the model. This
* is useful for area/countries that don't have access to the model hosted on
* GCP.
*/
exportinterfaceModelConfig{
/**
* An optional string that specifies custom url of the model. This
* is useful for area/countries that don't have access to the model hosted on
* GCP.
*/
modelUrl: string;
/**
* Wheter the url is from tfhub.
*/
fromTFHub?: boolean;
}
/**
* Answer object returned by the model.
* `text`: string, the text of the answer.
* `startIndex`: number, the index of the starting character of the answer in
* the passage.
* `endIndex`: number, index of the last character of the answer text.
* `score`: number, indicates the confident
* level.
*/
exportinterfaceAnswer{
text: string;
startIndex: number;
endIndex: number;
score: number;
}
interfaceFeature{
inputIds: number[];
inputMask: number[];
segmentIds: number[];
origTokens: Token[];
tokenToOrigMap: {[key: number]: number};
}
interfaceAnswerIndex{
start: number;
end: number;
score: number;
}
classQuestionAndAnswerImplimplementsQuestionAndAnswer{
privatemodel: tfconv.GraphModel;
privatetokenizer: BertTokenizer;
constructor(privatemodelConfig: ModelConfig){
if(this.modelConfig==null){
this.modelConfig={modelUrl: MODEL_URL,fromTFHub: true};
}
if(this.modelConfig.fromTFHub==null){
this.modelConfig.fromTFHub=false;
}
}
privateprocess(
query: string,context: string,maxQueryLen: number,maxSeqLen: number,
docStride=128): Feature[]{
// always add the question mark to the end of the query.
query=query.replace(/\?/g,'');
query=query.trim();
query=query+'?';
constqueryTokens=this.tokenizer.tokenize(query);
if(queryTokens.length>maxQueryLen){
thrownewError(
`The length of question token exceeds the limit (${maxQueryLen}).`);
}
constorigTokens=this.tokenizer.processInput(context.trim());
consttokenToOrigIndex: number[]=[];
constallDocTokens: number[]=[];
for(leti=0;i<origTokens.length;i++){
consttoken=origTokens[i].text;
constsubTokens=this.tokenizer.tokenize(token);
for(letj=0;j<subTokens.length;j++){
constsubToken=subTokens[j];
tokenToOrigIndex.push(i);
allDocTokens.push(subToken);
}
}
// The -3 accounts for [CLS], [SEP] and [SEP]
constmaxContextLen=maxSeqLen-queryTokens.length-3;
// We can have documents that are longer than the maximum sequence
// length. To deal with this we do a sliding window approach, where we
// take chunks of the up to our max length with a stride of
// `doc_stride`.
constdocSpans: Array<{start: number,length: number}>=[];
letstartOffset=0;
while(startOffset<allDocTokens.length){
letlength=allDocTokens.length-startOffset;
if(length>maxContextLen){
length=maxContextLen;
}
docSpans.push({start: startOffset, length});
if(startOffset+length===allDocTokens.length){
break;
}
startOffset+=Math.min(length,docStride);
}
constfeatures=docSpans.map(docSpan=>{
consttokens=[];
constsegmentIds=[];
consttokenToOrigMap: {[index: number]: number}={};
tokens.push(CLS_INDEX);
segmentIds.push(0);
for(leti=0;i<queryTokens.length;i++){
constqueryToken=queryTokens[i];
tokens.push(queryToken);
segmentIds.push(0);
}
tokens.push(SEP_INDEX);
segmentIds.push(0);
for(leti=0;i<docSpan.length;i++){
constsplitTokenIndex=i+docSpan.start;
constdocToken=allDocTokens[splitTokenIndex];
tokens.push(docToken);
segmentIds.push(1);
tokenToOrigMap[tokens.length]=tokenToOrigIndex[splitTokenIndex];
}
tokens.push(SEP_INDEX);
segmentIds.push(1);
constinputIds=tokens;
constinputMask=inputIds.map(id=>1);
while((inputIds.length<maxSeqLen)){
inputIds.push(0);
inputMask.push(0);
segmentIds.push(0);
}
return{inputIds, inputMask, segmentIds, origTokens, tokenToOrigMap};
});
returnfeatures;
}
asyncload(){
this.model=awaittfconv.loadGraphModel(
this.modelConfig.modelUrl,{fromTFHub: this.modelConfig.fromTFHub});
// warm up the backend
constbatchSize=1;
constinputIds=tf.ones([batchSize,INPUT_SIZE],'int32');
constsegmentIds=tf.ones([1,INPUT_SIZE],'int32');
constinputMask=tf.ones([1,INPUT_SIZE],'int32');
this.model.execute({
input_ids: inputIds,
segment_ids: segmentIds,
input_mask: inputMask,
global_step: tf.scalar(1,'int32')
});
this.tokenizer=awaitloadTokenizer();
}
/**
* Given the question and context, find the best answers.
* @param question the question to find answers for.
* @param context context where the answers are looked up from.
* @return array of answers
*/
asyncfindAnswers(question: string,context: string): Promise<Answer[]>{
if(question==null||context==null){
thrownewError(
'The input to findAnswers call is null, '+
'please pass a string as input.');
}
constfeatures=
this.process(question,context,MAX_QUERY_LEN,MAX_SEQ_LEN);
constinputIdArray=features.map(f=>f.inputIds);
constsegmentIdArray=features.map(f=>f.segmentIds);
constinputMaskArray=features.map(f=>f.inputMask);
constglobalStep=tf.scalar(1,'int32');
constbatchSize=features.length;
constresult=tf.tidy(()=>{
constinputIds=
tf.tensor2d(inputIdArray,[batchSize,INPUT_SIZE],'int32');
constsegmentIds=
tf.tensor2d(segmentIdArray,[batchSize,INPUT_SIZE],'int32');
constinputMask=
tf.tensor2d(inputMaskArray,[batchSize,INPUT_SIZE],'int32');
returnthis.model.execute(
{
input_ids: inputIds,
segment_ids: segmentIds,
input_mask: inputMask,
global_step: globalStep
},
['start_logits','end_logits'])as[tf.Tensor2D,tf.Tensor2D];
});
constlogits=awaitPromise.all([result[0].array(),result[1].array()]);
// dispose all intermediate tensors
globalStep.dispose();
result[0].dispose();
result[1].dispose();
constanswers=[];
for(leti=0;i<batchSize;i++){
answers.push(this.getBestAnswers(
logits[0][i],logits[1][i],features[i].origTokens,
features[i].tokenToOrigMap,context,i));
}
returnanswers.reduce((flatten,array)=>flatten.concat(array),[])
.sort((logitA,logitB)=>logitB.score-logitA.score)
.slice(0,PREDICT_ANSWER_NUM);
}
/**
* Find the Best N answers & logits from the logits array and input feature.
* @param startLogits start index for the answers
* @param endLogits end index for the answers
* @param origTokens original tokens of the passage
* @param tokenToOrigMap token to index mapping
*/
getBestAnswers(
startLogits: number[],endLogits: number[],origTokens: Token[],
tokenToOrigMap: {[key: string]: number},context: string,
docIndex=0): Answer[]{
// Model uses the closed interval [start, end] for indices.
conststartIndexes=this.getBestIndex(startLogits);
constendIndexes=this.getBestIndex(endLogits);
constorigResults: AnswerIndex[]=[];
startIndexes.forEach(start=>{
endIndexes.forEach(end=>{
if(tokenToOrigMap[start+OUTPUT_OFFSET]&&tokenToOrigMap[end+OUTPUT_OFFSET]&&end>=start){
constlength=end-start+1;
if(length<MAX_ANSWER_LEN){
origResults.push(
{start, end,score: startLogits[start]+endLogits[end]});
}
}
});
});
origResults.sort((a,b)=>b.score-a.score);
constanswers: Answer[]=[];
for(leti=0;i<origResults.length;i++){
if(i>=PREDICT_ANSWER_NUM||
origResults[i].score<NO_ANSWER_THRESHOLD){
break;
}
letconvertedText='';
letstartIndex=0;
letendIndex=0;
if(origResults[i].start>0){
[convertedText,startIndex,endIndex]=this.convertBack(
origTokens,tokenToOrigMap,origResults[i].start,
origResults[i].end,context);
}else{
convertedText='';
}
answers.push({
text: convertedText,
score: origResults[i].score,
startIndex,
endIndex
});
}
returnanswers;
}
/** Get the n-best logits from a list of all the logits. */
getBestIndex(logits: number[]): number[]{
consttmpList=[];
for(leti=0;i<MAX_SEQ_LEN;i++){
tmpList.push([i,i,logits[i]]);
}
tmpList.sort((a,b)=>b[2]-a[2]);
constindexes=[];
for(leti=0;i<PREDICT_ANSWER_NUM;i++){
indexes.push(tmpList[i][0]);
}
returnindexes;
}
/** Convert the answer back to original text form. */
convertBack(
origTokens: Token[],tokenToOrigMap: {[key: string]: number},
start: number,end: number,context: string): [string,number,number]{
// Shifted index is: index of logits + offset.
constshiftedStart=start+OUTPUT_OFFSET;
constshiftedEnd=end+OUTPUT_OFFSET;
conststartIndex=tokenToOrigMap[shiftedStart];
constendIndex=tokenToOrigMap[shiftedEnd];
conststartCharIndex=origTokens[startIndex].index;
constendCharIndex=endIndex<origTokens.length-1 ?
origTokens[endIndex+1].index-1 :
origTokens[endIndex].index+origTokens[endIndex].text.length;
return[
context.slice(startCharIndex,endCharIndex+1).trim(),startCharIndex,
endCharIndex
];
}
}
exportasyncfunctionload(modelConfig?: ModelConfig):
Promise<QuestionAndAnswer>{
constmobileBert=newQuestionAndAnswerImpl(modelConfig);
awaitmobileBert.load();
returnmobileBert;
}