-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.ts
58 lines (46 loc) · 1.67 KB
/
app.ts
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
import * as dotenv from "dotenv";
import fs from "fs";
dotenv.config();
import { Configuration, OpenAIApi } from "openai";
const main = async () => {
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Assume the text file is located in the same directory as the script
const filePath = "./titles.txt";
// Read the file line by line
const stream = fs.createReadStream(filePath, { encoding: "utf8" });
const headlines = new Set<string>();
stream.on("data", (chunk: string) => {
// Split the chunk into lines
const lines = chunk.split("\n");
// Process each line
for (const line of lines) {
if (line.length > 12) {
headlines.add(line);
}
}
});
stream.on("end", async () => {
console.log("Finished reading file");
const lines = Array.from(headlines);
for (let index = 0; index < lines.length; index++) {
const line = lines[index];
const prompt = `Write a long, very detailed, highly SEO optimized article on '${line}'. The writing must be very engaging, and informative. Cover all factors. Write 5000 words minimum. Wrap the headings HTML strong tags and list itmes in strongs tags.`;
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt,
temperature: 1,
max_tokens: 4000,
});
const data = response.data.choices[0].text;
const fileName = line.replace(/[^a-zA-Z ]/g, "");
if (data) {
const saveData = data.replace(/(\r\n|\n|\r)/gm, "");
fs.writeFileSync(`output/${fileName}.html`, saveData);
}
}
});
};
main();