Repository navigation
Expand file tree
/
Copy pathawaitify.js
More file actions
59 lines (50 loc) · 1.43 KB
/
Copy pathawaitify.js
File metadata and controls
59 lines (50 loc) · 1.43 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
'use strict';
const assert = require('assert');
const { Configuration, OpenAIApi } = require('openai');
const apiKey = process.env.OPEN_AI_KEY;
assert.ok(apiKey, 'No OPEN_AI_KEY specified');
const prompt = code => `
You are a software developer refactoring a legacy codebase.
The codebase uses Node.js callbacks. Refactor the code snippet to use async functions and await instead.
Assume that any function that takes a callback parameter also returns a promise.
Input:
const A = db.model('A', { n: [{ age: 'number' }] });
const a = new A({ n: [{ age: '47' }] });
assert.strictEqual(47, a.n[0].age);
a.save(function(err) {
assert.ifError(err);
A.findById(a, function(err) {
assert.ifError(err);
assert.strictEqual(47, a.n[0].age);
});
});
Output:
const A = db.model('A', { n: [{ age: 'number' }] });
const a = new A({ n: [{ age: '47' }] });
assert.strictEqual(47, a.n[0].age);
await a.save();
await A.findById(a);
assert.strictEqual(47, a.n[0].age);
Input:
${code}
Output:`.trim();
const configuration = new Configuration({
apiKey
});
const openai = new OpenAIApi(configuration);
module.exports = async function awaitify(req, res) {
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: prompt(req.body.code)
}
],
temperature: 0,
max_tokens: 2000
});
return res.json({
content: response.data.choices[0].message.content
});
};