-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateCompletion.js
50 lines (44 loc) · 1.39 KB
/
createCompletion.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
import { createConfiguration, OpenAIApi } from "@fortaine/openai";
import { streamCompletion } from "@fortaine/openai/stream";
const configurationOpts = {
authMethods: {
apiKeyAuth: {
accessToken: process.env.OPENAI_API_KEY,
},
},
};
const configuration = createConfiguration(configurationOpts);
const openai = new OpenAIApi(configuration);
export async function createCompletion({ model, prompt }) {
try {
// https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
const max_tokens = 4097 - prompt.length;
const completion = await openai.createCompletion({
model,
prompt,
max_tokens,
stream: true,
});
for await (const message of streamCompletion(completion)) {
try {
const parsed = JSON.parse(message);
const { text } = parsed.choices[0];
process.stdout.write(text);
} catch (error) {
console.error("Could not JSON parse stream message", message, error);
}
}
process.stdout.write("\n");
} catch (error) {
if (error.code) {
try {
const parsed = JSON.parse(error.body);
console.error("An error occurred during OpenAI request: ", parsed);
} catch (error) {
console.error("An error occurred during OpenAI request: ", error);
}
} else {
console.error("An error occurred during OpenAI request", error);
}
}
}