forked from marekfoltanski/seo-zabijaka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai.js
More file actions
82 lines (76 loc) · 2.47 KB
/
openai.js
File metadata and controls
82 lines (76 loc) · 2.47 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Configuration, OpenAIApi } from "openai";
import { keys } from "./keys.js";
const createContent = async (title, headings = false) => {
if (!headings) {
console.log(`piszę: ${title}`);
}
const configuration = new Configuration({
apiKey: keys.AI_KEY,
});
const openai = new OpenAIApi(configuration);
const prompt = headings
? `Napisz dwa akapity na temat "${title}".`
: `Napisz zoptymalizowany pod SEO artykuł blogowy na temat "${title}". Tekst powinien zawierać 3 nagłówki, dla każdego nagłówka napisz 2 akapity. Tekst ma być sformatowany do HTML, nagłówki umieść w <h2>, a akapity w <p>`;
let failedRequests = 0;
const maxFails = 4;
const retry = async (ms) =>
new Promise((resolve) => {
openai
.createCompletion({
model: "text-davinci-003",
prompt: prompt,
temperature: 1,
max_tokens: 3700,
top_p: 0,
frequency_penalty: headings ? 0.5 : 0.3,
presence_penalty: headings ? 1 : 0.7,
})
.then((res) => resolve(res.data.choices[0].text))
.catch((error) => {
if (
(error.response.status === 429 || error.response.status === 500) &&
failedRequests < maxFails
) {
setTimeout(() => {
failedRequests++;
console.log(
`powtarzam: ${title} (${error.response.statusText}) ${failedRequests} / ${maxFails}`
);
retry(ms).then(resolve);
}, ms);
} else {
resolve({
error: true,
statusText: error.response.statusText,
});
}
});
});
const response = await retry(5000);
return response;
};
const contentWithHeadings = async (title, headings) => {
console.log(`piszę: ${title}`);
const arr = headings.map((item) => createContent(item, true));
const formatText = (item) => {
return item
.split("\n")
.map((item) => `<p>${item}</p>`)
.filter((item) => item !== "<p></p>")
.join("\n");
};
const res = await Promise.all(arr)
.then((result) =>
result?.map(
(item, index) => `<h2>${headings[index]}</h2>\n${formatText(item)}`
)
)
.catch((error) => {
return {
error: true,
statusText: "Wystąpił błąd podczas tworzenia treści",
};
});
return !res.error ? `<h1>${title}</h1>\n${res?.join("\n")}` : res;
};
export { createContent, contentWithHeadings };