-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
63 lines (56 loc) · 1.52 KB
/
generate.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
const OpenAI = require("openai-api");
const fs = require("fs");
const path = require("path");
const ora = require("ora");
require("dotenv").config();
const openai = new OpenAI(process.env.OPENAI_API_KEY);
const SAMPLE_COUNT = 30;
const readFile = () => {
return new Promise((res, rej) => {
fs.readFile(path.resolve(__dirname, "tweets.json"), (err, data) => {
if (err) {
rej("err");
}
const tweetsList = JSON.parse(data).tweetList;
res(tweetsList);
});
});
};
const constructTrainingData = (inputArr) => {
let result = "This is a tweet generator\n";
let sampleList = new Set();
while (sampleList.size < SAMPLE_COUNT) {
const randomElement = inputArr[Math.floor(Math.random() * inputArr.length)];
sampleList.add(randomElement);
}
for (input of sampleList) {
result += input + "\n";
result += "###\n";
}
return result;
};
const generateTweets = async () => {
let trainingData = await readFile();
const trainingString = constructTrainingData(trainingData);
const spinner = ora("Generating tweets").start();
const gptResponse = await openai.complete({
engine: "davinci",
prompt: trainingString,
maxTokens: 200,
temperature: 0.8,
topP: 1,
presencePenalty: 0,
frequencyPenalty: 0,
bestOf: 1,
n: 1,
stream: false,
});
spinner.stop();
const resultTextList = gptResponse.data.choices[0].text.split("\n");
for (result of resultTextList) {
if (result[0] !== "#") {
console.log(result);
}
}
};
generateTweets();