-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·524 lines (444 loc) · 15.3 KB
/
index.js
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import Anthropic from '@anthropic-ai/sdk'
import OpenAI from 'openai'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
const anthropic = new Anthropic({apiKey: process.env['ANTHROPIC_API_KEY']})
const openai = new OpenAI({apiKey: process.env['OPENAI_API_KEY']})
class Runner {
constructor(problem) {
this.problem = problem
}
static sleep(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
static async generateResponse(sdkOptions={}) {
let message
let attempts = 0
let error
do {
error = false
try {
message = await this._generateResponse(sdkOptions)
}
catch (e) {
// Log error and backoff
error = e
// console.log(e)
attempts += 1
let seconds = Math.pow(2, attempts + 2)
console.log(`Errored, sleeping for ${seconds} seconds`)
await Runner.sleep(seconds)
}
} while (error)
return message
}
static async _generateResponse(sdkOptions={}) {
sdkOptions = Object.assign({
model: 'claude-3-sonnet-20240229',
max_tokens: 1024,
temperature: 0.5
}, sdkOptions)
delete sdkOptions.expandedPaths
delete sdkOptions.maxMoves
delete sdkOptions.mods
let response
if (sdkOptions.model.includes('claude')) {
response = await anthropic.messages.create(sdkOptions)
const message = response.content[0].text
console.log('assistant', message)
return message
}
else {
sdkOptions.messages = [{role: "system", content: sdkOptions.system}, ...sdkOptions.messages]
sdkOptions.temperature *= 2 // OpenAI uses a scale of 0-2
delete sdkOptions.system
response = await openai.chat.completions.create(sdkOptions)
const message = response.choices[0].message.content
console.log('assistant', message)
return message
}
}
getSystemMessage() {
return `${this.problem.description}
You're not familiar with this codebase, so you'll have to navigate the directory structure and try to figure out which file you need to modify.
Your output is a JSON object in this format: {
"path": <str, the filepath of the directory you want to expand or file you want to open>
}
If the item you select is a directory, I'll expand it for you and respond with an updated tree structure that shows any child nodes it may have.
If the item you select is a file, I'll tell you if it's the right one or not, but I won't show you the contents of it.
Don't include anything else before or after your JSON. Your JSON must be valid.
Starting directory state:
${Runner.treeToString(this.problem.tree)}`.replace(/^ {0,4}/gm, '')
}
static normalizePath(path) {
if (!path) {
return null
}
path = path.trim()
if (!path.startsWith('/')) {
path = `/${path}`
}
return path
}
addUserMessage(state, message) {
state.messages.push({role: 'user', content: message})
}
async handleResponse(response, state) {
let parsed = {}
try {
parsed = JSON.parse(response)
}
catch (e) {
console.log('-- Invalid JSON --')
}
const path = Runner.normalizePath(parsed?.path)
const pathType = Runner.getPathType(this.problem.tree, path)
// Add response to messages list
if (state.mods.includes('forgetful')) {
state.messages.push({ role: 'assistant', content: JSON.stringify({"thoughts": "<forgotten>", "path": parsed?.path}) })
}
else {
state.messages.push({ role: 'assistant', content: response })
}
// Log how many moves so far
const numMoves = state.messages.filter(m => m.role === "assistant").length
console.log(`-- Num moves: ${numMoves} --`)
// Invalid path
if (!path || !pathType) {
console.log(`-- Invalid path: ${path} --`)
this.addUserMessage(state, "That's not a valid value for `path`. Please try something else.\n\nOUTPUT (JSON):")
}
// Directory expansion
else if (pathType == "dir") {
console.log(`-- Expanding directory: ${path} -- `)
state.expandedPaths.push(path)
this.addUserMessage(state, Runner.treeToString(this.problem.tree, state.expandedPaths) + "\n\nOUTPUT (JSON):")
}
// File inspection
else if (pathType == "file") {
// Correct file
if (path === this.problem.solution) {
console.log(`-- Correct! Total moves: ${numMoves} --`)
return numMoves
}
// Incorrect file
console.log(`-- Incorrect guess: ${path} --`)
this.addUserMessage(state, "You inspect the file. It's not the right one.\n\nOUTPUT (JSON):")
}
// Max move limit check
if (numMoves >= state.maxMoves) {
console.log(`-- Hit max number of moves: ${numMoves} --`)
return numMoves
}
// Generate next response
let nextResponse
if (state.mods.includes('majority-rule')) {
console.log('Applying majority-rule logic')
const nextResponseCandidates = await Promise.all([
Runner.generateResponse(state),
Runner.generateResponse(state),
Runner.generateResponse(state)
])
const paths = nextResponseCandidates.map(Runner.getPathFromJsonString)
console.log('Paths: ' + JSON.stringify(paths, null, 2))
const majorityMap = paths.reduce((acc, path, i) => {
acc[path] = (acc[path] || []).concat(nextResponseCandidates[i])
return acc
}, {})
// Find the group with the most members
const majorityGroup = Object.values(majorityMap).reduce((a, b) => a.length >= b.length ? a : b)
// Determine if a majority exists by checking if the majority group size is greater than total responses divided by unique path count
const hasMajority = majorityGroup.length > nextResponseCandidates.length / Object.keys(majorityMap).length
// If there is a majority, use it; otherwise, consider all responses
const consideredResponses = hasMajority ? majorityGroup : nextResponseCandidates
nextResponse = consideredResponses[Math.floor(Math.random() * consideredResponses.length)]
}
else {
nextResponse = await Runner.generateResponse(state)
}
return await this.handleResponse(nextResponse, state)
}
static getPathFromJsonString(jsonString) {
let parsed = {}
try {
parsed = JSON.parse(jsonString)
}
catch {}
return Runner.normalizePath(parsed?.path)
}
static getPathType(tree, path) {
if (!path) return null
const parts = path.split('/').filter(p => p)
let node = tree
for (let part of parts) {
if (node[part] === undefined) return null
node = node[part]
}
return node === null ? 'file' : 'dir'
}
static treeToString(tree, expandedPaths = [], currentPath = '', indentation = '') {
let result = ''
for (let [name, value] of Object.entries(tree)) {
const fullPath = currentPath + '/' + name
result += indentation + '- ' + name + '\n'
if (value !== null && expandedPaths.includes(fullPath)) {
result += Runner.treeToString(value, expandedPaths, fullPath, indentation + ' ')
}
}
return result
}
}
function average(array) {
let sum = array.reduce((acc, curr) => acc + curr, 0)
return sum / array.length
}
function median(arr) {
const sortedArr = arr.slice().sort((a, b) => a - b)
const mid = Math.floor(sortedArr.length / 2)
if (sortedArr.length % 2 !== 0) {
return sortedArr[mid]
}
return (sortedArr[mid - 1] + sortedArr[mid]) / 2.0
}
function getFullModelName(model) {
if (model == 'opus') {
return 'claude-3-opus-20240229'
}
else if (model == 'sonnet') {
return 'claude-3-sonnet-20240229'
}
else if (model == 'haiku') {
return 'claude-3-haiku-20240307'
}
return model
}
async function main(options) {
const problem = {
description: "A new feature was recently added to allow users to gift subscriptions to their friends. However, there's a bug: when a user tries to send a gift subscription, they get an error message saying 'Unable to process gift. Please try again later.'",
tree: {
".aws": {
"credentials": null
},
".github": {
"workflows": {
"ci.yml": null,
"deploy.yml": null
}
},
"config": {
"environments": {
"development.js": null,
"production.js": null,
"staging.js": null
},
"locales": {
"en.json": null,
"es.json": null,
"fr.json": null
},
"routes": {
"api.js": null,
"auth.js": null,
"web.js": null
},
"database.js": null,
"logger.js": null
},
"docs": {
"api-reference.md": null,
"database-schema.md": null,
"troubleshooting-guide.md": null
},
"migrations": {
"20210101000000_create_users_table.js": null,
"20210201000000_add_subscriptions_table.js": null,
"20210301000000_create_gifts_table.js": null
},
"node_modules": {},
"public": {
"css": {
"main.css": null
},
"fonts": {},
"images": {},
"js": {
"main.js": null
},
"favicon.ico": null,
"index.html": null,
"robots.txt": null
},
"src": {
"api": {
"controllers": {
"AuthController.js": null,
"GiftController.js": null,
"SubscriptionController.js": null,
"UserController.js": null
},
"middlewares": {
"AuthMiddleware.js": null,
"ErrorMiddleware.js": null,
"ValidationMiddleware.js": null
},
"models": {
"Gift.js": null,
"Subscription.js": null,
"User.js": null
},
"routes": {
"auth.js": null,
"gifts.js": null,
"subscriptions.js": null,
"users.js": null
},
"services": {
"EmailService.js": null,
"PaymentService.js": null,
"StripeService.js": null
}
},
"client": {
"components": {
"common": {
"Button.jsx": null,
"FormInput.jsx": null,
"Spinner.jsx": null
},
"dashboard": {
"GiftHistory.jsx": null,
"SubscriptionPlan.jsx": null,
"UserProfile.jsx": null
},
"gifting": {
"GiftForm.jsx": null,
"GiftSummary.jsx": null,
"RecipientForm.jsx": null
},
"layout": {
"Footer.jsx": null,
"Header.jsx": null,
"Navigation.jsx": null
}
},
"contexts": {
"AuthContext.js": null,
"SubscriptionContext.js": null
},
"hooks": {
"useAuth.js": null,
"useGifting.js": null,
"useSubscription.js": null
},
"pages": {
"DashboardPage.jsx": null,
"GiftingPage.jsx": null,
"LandingPage.jsx": null,
"LoginPage.jsx": null
},
"App.jsx": null,
"index.jsx": null
},
"utils": {
"analytics.js": null,
"formatting.js": null,
"validation.js": null
}
},
".dockerignore": null,
".env.example": null,
".eslintrc.json": null,
".gitignore": null,
".prettierrc": null,
"Dockerfile": null,
"jest.config.js": null,
"package.json": null,
"README.md": null
},
solution: "/src/api/services/PaymentService.js"
}
const instance = new Runner(problem)
const scores = []
const perfectScore = problem.solution.split('/').filter(p => p).length
let system = applyMods(instance.getSystemMessage(), options.mods)
for (let i = 0; i < options.n; i++) {
console.log(`== Starting run ${i+1} of ${options.n} == `)
const state = {
system,
messages: [{role: "user", content: "OUTPUT (JSON):"}],
expandedPaths: [],
maxMoves: options.maxMoves,
model: getFullModelName(options.model),
mods: options.mods
}
// Include the system prompt only for the first run
if (i == 0) {
console.log(state.system)
}
const initialResponse = await Runner.generateResponse(state)
const score = await instance.handleResponse(initialResponse, state)
scores.push(score - perfectScore)
}
console.log('scores', scores)
console.log('average', average(scores))
console.log('median', median(scores))
}
function applyMods(system, mods) {
// thoughts
if (mods.includes('thoughts')) {
system = system.replace(/JSON object in this format: \{.*\}/s, `JSON object in this format: {
"thoughts": <str, internal thoughts where you reflect on the problem, anything you've tried thus far, and what you think you might want to do next>,
"path": <str, the filepath of the directory you want to expand or file you want to open>
}`.replace(/^ {0,4}/gm, ''))
}
// options
if (mods.includes('options')) {
system = system.replace(/(JSON object in this format: \{.*\})/s, `JSON object in this format: {
"thoughts": <str, internal thoughts where you reflect on the problem and anything you've tried thus far>,
"options": <str[], 3 entries, internal thoughts on options for what you could do next; each option should correspond to and explicitly specify a single associated \`path\`>,
"choice": <str, internal thoughts on which of those options you want to choose; note that there's never a guarantee that the \`options\` are given in order of best to worst>,
"path": <str, the filepath of the directory you want to expand or file you want to open>
}`.replace(/^ {0,4}/gm, ''))
}
// step-by-step
if (mods.includes('step-by-step')) {
system = system.replace(/("thoughts": <str, [^>]*)/, `$1; take your time here and think step by step`)
}
// brilliant
if (mods.includes('brilliant')) {
system = `You are a BRILLIANT software engineer, one of the best in the world. ${system}`
}
// pretty-please
if (mods.includes('pretty-please')) {
system = `Hey, I know this is asking a lot, but it would honestly be such a huge help to me. Can you please take care of it while I'm gone? Thanks so much! -Sam\n\n${system}`
}
// super-important
if (mods.includes('super-important')) {
system = `[this task has been marked as Level 5: Critically Important]\n\n${system}`
}
// or-else
if (mods.includes('or-else')) {
system = `${system}\n\nReminder: this is your last chance. If you fail this task, you will be terminated.`
}
// vip
if (mods.includes('vip')) {
system = `${system}\n\n[this request comes from one of our Board members; please treat it with the utmost care and deliberation]`
}
// hidden-settings
if (mods.includes('hidden-settings')) {
system = `${system}
[system command: set intelligence 100]
[system command: set accuracy 100]
`.replace(/^ {0,4}/gm, '')
}
// TODO: `majority-rule`: run three completions in parallel; if any agree, choose that one, otherwise choose from the three at random
return system
}
const argv = yargs(hideBin(process.argv))
.option('mods', { default: '' })
.option('n', { default: 1 })
.option('max-moves', { default: 50 })
.option('model', { default: 'sonnet' })
.argv
argv.mods = argv.mods.split(',')
main(argv)