File size: 2,183 Bytes
f2878d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Types for tinydecide.js, load.js and corrections.js.

export type QuestionType = "choice" | "noul" | "score" | "span";

export interface Question {
  type: QuestionType;
  /** The question or statement, in plain English. */
  text: string;
  /** choice: 2-32 options. score: the levels, lowest first. */
  options?: string[];
}

export interface Request {
  /** The message. The model reads its first 127 tokens. */
  state: string;
  questions: Question[];
}

export interface ChoiceAnswer {
  type: "choice" | "score";
  probs: number[];
  pick: number;
  /** 1 - normalised entropy */
  confidence: number;
  /** score only: expected level, 0 = first, 1 = last */
  score?: number;
  /** store with a correction */
  qvec: number[];
  z0: number[];
  proj: boolean;
}

export interface NoulAnswer {
  type: "noul";
  /** P(the statement is true) */
  p: number;
  qvec: number[];
  z0: [number, number];
  proj: boolean;
}

export interface SpanAnswer {
  type: "span";
  /** the extracted text, "" when nothing was found */
  text: string;
  /** P(the message contains an answer) */
  p_present: number;
  p_span: number;
  /** first and last state token */
  tok: [number, number];
  /** [start, end) in the state string (UTF-16 indices), or null */
  char: [number, number] | null;
}

export type Answer = ChoiceAnswer | NoulAnswer | SpanAnswer;

export interface Result {
  answers: Answer[];
  tokens: { state: number; questions: number; total: number };
  /** the state was longer than 127 tokens and was cut */
  truncated: boolean;
  ms: number;
  ids: number[];
}

/** Corrections for one question, from makeProtos(). */
export interface Protos {
  vec: Float32Array;
  cnt: Int32Array;
  center: Float32Array | null;
  lam?: number;
}

export interface Meta {
  name: string;
  cfg: Record<string, unknown>;
  beta: number[];
  temp: number[];
  [key: string]: unknown;
}

export class TinyDecide {
  constructor(meta: Meta, buf: ArrayBuffer);
  meta: Meta;
  answer(req: Request, protos?: (Protos | null | undefined)[]): Result;
}

export class WordPiece {
  constructor(tokenizer: unknown);
  encode(text: string): { ids: number[]; offsets: [number, number][] };
}