-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathoptions.ts
367 lines (335 loc) · 10.1 KB
/
options.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
359
360
361
362
363
364
365
366
367
import yargs from "yargs/yargs";
import { parseBoolean, parseNumber } from "../util/util";
export const botActionChoices = ["ask", "eval"] as const;
export type Command = {
input: string;
options: Options;
output: string;
errors: string | undefined;
otherText: string;
action?: BotAction;
};
export type BotAction = (typeof botActionChoices)[number];
export const modeChoices = ["public", "everything", "staging", "cli", "gentest"] as const;
export type Mode = (typeof modeChoices)[number];
const keysArgs = {
openaiApiKey: {
description: "The API key to use for Open AI endpoints.",
type: "string",
modes: ["everything", "cli"],
},
};
const modelSelectionArgs = {
model: {
description: "The model to use",
type: "string",
modes: ["everything", "cli"],
defaults: {
all: "text-davinci-003",
},
},
};
const parallelismArgs = {
deterministic: {
description: "Do not run actions in parallel, to help make output to stdout deterministic.",
type: "boolean",
modes: ["everything", "cli"],
defaults: { all: false },
},
maxConcurrency: {
description: "The maximum number of concurrent requests to make to the GitHub API",
type: "number",
modes: ["everything", "cli"],
defaults: { all: 2 },
},
};
const modelRetryArgs = {
retryCount: {
description: "The number of times to retry a failed request",
type: "number",
modes: ["everything", "cli"],
defaults: { all: 3, cli: 20 },
},
retryBackoffFactor: {
description: "The factor by which to increase the delay between retries",
type: "number",
modes: ["everything", "cli"],
defaults: { all: 1, cli: 1.2 },
},
retryInitialDelay: {
description: "The initial delay (in ms) for retrying a failed request",
type: "number",
modes: ["everything", "cli"],
defaults: { all: 1000, cli: 5000 },
},
};
const commonEngineArgs = {
...keysArgs,
...parallelismArgs,
...modelRetryArgs,
...modelSelectionArgs,
verbose: {
description: "Enable verbose logging",
type: "boolean",
modes: ["everything", "cli"],
defaults: { all: false },
},
useLocalCache: {
description: "Use the local request cache for requests.",
type: "boolean",
modes: ["cli"],
defaults: { all: false, cli: true },
},
temperature: {
description:
"The temperature to use when generating completions. Higher means more likely to find a completion different to the existing text",
type: "number",
modes: ["everything", "cli"],
defaults: { all: 0 },
},
};
const commonAskArgs = {
arith: {
description: "Use arithmetic equip",
type: "boolean",
default: true,
modes: ["cli"],
},
noEmitComparisons: {
description: "Don't emit comparisons",
type: "boolean",
default: true,
modes: ["cli"],
},
noEliminateDateTime: {
description: "Don't request the reduction of date/time to minutes, hours, etc.",
type: "boolean",
default: true,
modes: ["cli"],
},
noSuppressArbitraryCode: {
description: "Don't suppress the generation of arbitrary code like lambdas and loops",
type: "boolean",
default: true,
modes: ["cli"],
},
emitChecks: {
description: "Emit checks",
type: "boolean",
default: true,
modes: ["cli"],
},
noEmitUnits: {
description: "Don't include units in the comments of the calculation code",
type: "boolean",
default: true,
modes: ["cli"],
},
noEmitDescriptions: {
description: "Don't include descriptions in comments of the calculation code",
type: "boolean",
default: true,
modes: ["cli"],
},
noIncludeCodeInFinalQuestion: {
description:
"Don't include the calculation code in the final question prompt, only the answers",
type: "boolean",
default: true,
modes: ["cli"],
},
};
const askArgs = {
...commonEngineArgs,
...commonAskArgs,
question: {
description: "The question to ask",
type: "string",
default: true,
modes: ["cli"],
},
questionfile: {
description: "The file containing the question to ask",
type: "string",
default: true,
modes: ["cli"],
},
singleline: {
description: "Request a single line answer?",
type: "boolean",
default: false,
modes: ["cli"],
},
};
const evalArgs = {
...commonEngineArgs,
...commonAskArgs,
questionset: {
description: "The set of questions to use",
type: "string",
default: "ASDiv",
modes: ["cli"],
},
questions: {
description: "The identifiers of the questions to run",
type: "string",
default: "",
modes: ["cli"],
},
};
const modeOptionsTemplate = {
ask: {
description: "Ask a question",
arguments: askArgs,
modes: ["cli", "everything"],
},
eval: {
description: "Evaluate performance on a set of questions",
arguments: evalArgs,
modes: ["cli", "everything"],
},
help: {
description: "Comments with a help message",
arguments: [],
modes: ["cli", "everything"],
},
};
type ArgumentOptions = {
description: string;
type: "string" | "number" | "boolean" | "array";
choices?: string[];
default?: string | number | boolean | string[];
demand?: boolean;
array?: boolean;
};
type Arguments = {
[key: string]: ArgumentOptions;
};
type CommandSpec = {
description: string;
arguments: Arguments;
};
type ModeOptions = {
[key in BotAction]+?: CommandSpec;
};
/**
* Specializes the template for a particular mode into the form for argument parsing.
*
* Any commands or arguments which are not valid for this mode are excluded from the result.
*/
export function modeOptions(mode: Mode): ModeOptions {
const modeOptions: ModeOptions = {};
for (const [action, commandSpecTemplate] of Object.entries(modeOptionsTemplate)) {
if (commandSpecTemplate.modes.includes(mode)) {
const args: Arguments = {};
for (const [arg, options] of Object.entries(commandSpecTemplate.arguments)) {
if (options.modes.includes(mode)) {
args[arg] = {
description: options.description,
type: (options as any).type,
choices: (options as any).choices,
default: (options as any).defaults
? (options as any).defaults[mode] ?? (options as any).defaults["all"]
: undefined,
demand: (options as any).demand,
array: (options as any).array,
};
}
}
modeOptions[action as BotAction] = {
description: commandSpecTemplate.description,
arguments: args,
};
}
}
return modeOptions;
}
// Parse the arguments for the command line or bot invocation
export function spec(
scriptName: string,
modeOptions: ModeOptions,
strict: "not-strict" | "strict"
) {
let y = yargs().parserConfiguration({});
for (const [action, commandSpec] of Object.entries(modeOptions)) {
let a = action;
// if any of the entries are required then make them positional
for (const [arg, options] of Object.entries(commandSpec.arguments)) {
if (options["demand"] === true) {
a += ` [${arg}]`;
}
}
y = y.command(a, commandSpec.description, commandSpec.arguments);
}
if (strict === "strict") {
y = y.strict();
}
return y.exitProcess(false).help().scriptName(scriptName);
}
type ExpandedArguments<T extends { arguments: any }> = { [P in keyof T["arguments"]]: any };
type Expanded<T extends { [P in keyof T]: { arguments: any } }> = {
[Command in keyof T]: ExpandedArguments<T[Command]>;
};
/** useful types */
/**
* The type Options is the type of specialized template once its been expanded
* for a particular mode, arguments, default values and environment variables.
*/
export type Options = Expanded<typeof modeOptionsTemplate>;
/**
* The type CommonEngineOptions is the subset of Options which are common to all
* modes.
*/
export type CommonEngineOptions = { [P in keyof typeof commonEngineArgs]: any };
/**
* The type CommonBotOptions is the subset of Options which pertain to choice
* of which model to use.
*/
export type ModelChoiceOption = keyof typeof modelSelectionArgs;
/**
* The type ApiKeyOption is the subset of Options which pertain to setting
* the API keys for different models.
*/
export type ApiKeyOption = keyof typeof keysArgs;
/**
* The type ModelRetryOption is the subset of Options which pertain to
* retrying the model.
*/
export type ModelRetryOption = { [P in keyof typeof modelRetryArgs]: any };
/** The variable apiKeyOptions is the list of all the options which are API keys */
export const apiKeyOptions = Object.keys(keysArgs) as ApiKeyOption[];
/**
* This function takes the parsed command line arguments, and the mode, and
* expands the template for the mode into the form for using as a context object.
* All commands and arguments are retained and given a value which is
* (in order of highest priority first): taken from the environment variable, taken
* from the command line arguments, taken from the default in the template for the
* chosen mode.
*/
export function getOptions(argv: any, mode: Mode): Options {
const options = {} as Options;
for (const [action, commandSpecTemplate] of Object.entries(modeOptionsTemplate)) {
const args: any = {};
for (const [arg, options] of Object.entries(commandSpecTemplate.arguments)) {
const deflt = (options as any).defaults
? (options as any).defaults[mode] ?? (options as any).defaults["all"]
: undefined;
// convert arg from camelCase to capitals with underscores
const env = process.env[arg.replace(/([A-Z])/g, "_$1").toUpperCase()];
let commandLine = argv ? argv[arg] : undefined;
// if there are no instances of an array argument then yargs is returning
// an array containing a single undefined element - strip it here
if (Array.isArray(commandLine)) {
commandLine = commandLine.filter((x: any) => x !== undefined);
}
args[arg] = env ?? commandLine ?? deflt;
if (options.type === "boolean") {
args[arg] = parseBoolean(args[arg]);
} else if (options.type === "number") {
args[arg] = parseNumber(args[arg]);
}
}
options[action as BotAction] = args;
}
return options;
}