-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathinit-cli.ts
More file actions
211 lines (196 loc) · 7.55 KB
/
Copy pathinit-cli.ts
File metadata and controls
211 lines (196 loc) · 7.55 KB
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
import {Argv, CommandModule, Options} from 'yargs';
import {input} from '@inquirer/prompts';
import {join, relative, dirname} from 'path';
import {cp} from 'fs/promises';
import {formatTitleCard} from './reporting/format.js';
import {generateId} from './utils/id-generation.js';
import {safeWriteFile, toProcessAbsolutePath} from './file-system-utils.js';
export const InitModule = {
builder,
handler,
command: 'init',
describe: 'Interactive guide through the process of creating an eval environment',
} satisfies CommandModule<{}, Options>;
interface InitOptions {
configPath: string;
displayName: string;
clientSideFramework: string;
sourceDirectory: string;
executablePrompts?: string;
generationSystemPrompt?: string;
}
function builder(argv: Argv): Argv<{}> {
return argv.strict().version(false).help();
}
async function handler(): Promise<void> {
try {
const answers = await getAnswers();
if (answers !== null) {
await writeConfig(answers);
}
} catch (e: unknown) {
// If the user presses ctrl + c, Inquirer will throw `ExitPromptError`. Ignore it.
if (!(e instanceof Error) || e.name !== 'ExitPromptError') {
throw e;
}
}
}
async function getAnswers(): Promise<InitOptions | null> {
console.log(
formatTitleCard(
[
'Welcome LLM enthusiast! 🎉',
'Answer the following questions to create an eval environment',
].join('\n'),
),
);
// Add some spaces at the end to align to the text of the line above.
const newLineSeparator = '\n ';
const displayName = await input({
message: 'What will be the name of your environment?',
required: true,
default: 'Hello World',
});
const configPath = await input({
message: 'Where should we place the environment config file?',
required: true,
default: join(generateId(displayName) || 'env', 'config.mjs'),
validate: value =>
value.endsWith('.js') || value.endsWith('.mjs') ? true : 'Config must be a .mjs or .js file',
});
const clientSideFramework = await input({
message: 'What client-side framework will it be using?',
required: true,
default: 'unknown',
});
const sourceDirectory = await input({
message:
'In which directory should the LLM generate and execute code?' +
newLineSeparator +
'This should be the root of the project, e.g. where the `package.json` is placed',
required: true,
});
const generationSystemPrompt = await input({
message:
'What file contains your system instructions (e.g. `my-instructions.md`)?' +
newLineSeparator +
'Leave this blank and we will create an example for you',
});
const executablePrompts = await input({
message:
'What prompts should the LLM execute (e.g. `my-prompts/**/*.md`)?' +
newLineSeparator +
'Leave this blank and we will create some example prompts for you',
});
return {
displayName,
configPath,
clientSideFramework,
sourceDirectory,
generationSystemPrompt,
executablePrompts,
};
}
async function writeConfig(options: InitOptions) {
const configPath = toProcessAbsolutePath(options.configPath);
const configDir = dirname(configPath);
const sourcePath = toProcessAbsolutePath(options.sourceDirectory);
let generationPromptPath: string;
let executablePromptsPattern: string;
if (options.generationSystemPrompt) {
generationPromptPath = relative(
configDir,
toProcessAbsolutePath(options.generationSystemPrompt),
);
} else {
generationPromptPath = './example-system-instructions.md';
await safeWriteFile(join(configDir, generationPromptPath), getExampleSystemInstructions());
}
if (options.executablePrompts) {
executablePromptsPattern = relative(
configDir,
toProcessAbsolutePath(options.executablePrompts),
);
} else {
const executablePromptDir = './example-prompts';
executablePromptsPattern = `${executablePromptDir}/**/*.md`;
await cp(
join(import.meta.dirname, '../examples/prompts'),
join(configDir, executablePromptDir),
{recursive: true},
);
}
await safeWriteFile(
configPath,
[
`import { getBuiltInRatings } from 'web-codegen-scorer';`,
``,
`/** @type {import("web-codegen-scorer").EnvironmentConfig} */`,
`export default {`,
` displayName: '${options.displayName}',`,
` clientSideFramework: '${options.clientSideFramework}',`,
` sourceDirectory: '${relative(configDir, sourcePath)}',`,
` ratings: [`,
` // This includes some framework-agnostic scoring ratings to your eval.`,
` // You can add your own custom ratings to this array.`,
` ...getBuiltInRatings()`,
` ],`,
` generationSystemPrompt: '${generationPromptPath}',`,
` executablePrompts: ['${executablePromptsPattern}'],`,
``,
` // The following options aren't mandatory, but can be useful:`,
` // id: '', Unique ID for the environment. If empty, one is generated from the \`displayName\`.`,
` // packageManager: 'npm', // Name of the package manager used to install dependencies.`,
` // skipInstall: false, // Whether to skip installing dependencies. Useful if you're doing it yourself already.`,
` // buildCommand: 'npm run build', // Command used to build the generated code.`,
` // serveCommand: 'npm run start -- --port 0', // Command used to start a dev server with the generated code.`,
` // mcpServers: [], // Model Context Protocal servers to run during the eval.`,
``,
` // repairSystemPrompt: '', // Path to a prompt used when repairing broken code.`,
` // editingSystemPrompt: '', // Path to a prompt used when editing code during a multi-step eval.`,
` // codeRatingPrompt: '', // Path to a prompt to use when automatically rating the generated code with an LLM.`,
` // classifyPrompts: false, // Whether to exclude the prompt text from the final report.`,
``,
` // Path to a directory that will be merged with the \`sourceDirectory\` to produce`,
` // the final project. Useful for reusing boilerplate between environments.`,
` // projectTemplate: '',`,
``,
` // If your setup has different client-side and full-stack framework, `,
` // you can specify a different full-stack framework here.`,
` // fullStackFramework: '',`,
`};`,
].join('\n'),
);
console.log(
formatTitleCard(
[
'Done! 🎉 You can run your eval with the following command:',
`web-codegen-scorer eval --env=${options.configPath}`,
].join('\n'),
),
);
}
function getExampleSystemInstructions(): string {
return [
`You are an expert in JavaScript, TypeScript, CSS, HTML, and scalable web application development.`,
`You write functional, maintainable, performant, and accessible code following web development best practices.`,
``,
`### TypeScript Best Practices`,
``,
`- Use strict type checking`,
`- Prefer type inference when the type is obvious`,
`- Avoid the \`any\` type; use \`unknown\` when type is uncertain`,
``,
`Follow instructions below CAREFULLY:`,
``,
`- Include all necessary code to run independently`,
`- Use comments sparingly and only for complex parts of the code`,
`- Make sure the generated code is **complete** and **runnable**`,
`- Aesthetics are **crucial**, make the application look amazing!`,
``,
`<!--`,
` Normally you would put other important instructions here, like where to`,
` output the generated code and best practices for your framework.`,
`-->`,
].join('\n');
}