-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (68 loc) · 2.16 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
// Generate WordPress Post Excerpt
// using Post Content and OpenAI 🤖
// Buzzwords: 🐝 GPT-3, AI, AGI
// ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
require("dotenv").config();
const argv = require("minimist")(process.argv.slice(2));
const postId = argv?.postId ? argv.postId : 1;
const siteID = process.env.HEADLESS_SITE_ID;
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const username = process.env.HEADLESS_USERNAME;
const password = process.env.HEADLESS_PASSWORD;
const search = argv?.search ? argv.search : 56;
const username_password = `${username}:${password}`;
const buffer = new Buffer.from(username_password);
const token = buffer.toString("base64");
const headlessURL = `hl-b.getshifter.co`;
const baseURL = `https://${siteID}.hl-b.getshifter.co`;
const restURL = `${baseURL}/wp-json/wp/v2`;
const axios = require("axios");
const { convert } = require("html-to-text");
const OpenAI = require("openai-api");
const openai = new OpenAI(OPENAI_API_KEY);
async function generatePostExcerpt({ postId }) {
try {
let {
data: { content, guid, id },
} = await axios({
headers: {
Authorization: `Basic ${token}`,
},
method: "get",
url: `${restURL}/posts/${postId}`,
});
const response = await openai.complete({
engine: "davinci-instruct-beta-v3",
prompt: `${convert(content.rendered).replace(
/(\r\n|\n|\r)/gm,
""
)}\ntl;dr:`,
maxTokens: 75,
temperature: 0.5,
topP: 1,
presencePenalty: 0,
frequencyPenalty: 0,
bestOf: 1,
n: 1,
stream: false,
});
const excerpt = response.data.choices[0].text.replace(/(\r\n|\n|\r)/gm, "");
await axios({
headers: {
Authorization: `Basic ${token}`,
},
method: "post",
url: `${restURL}/posts/${postId}`,
data: {
excerpt: excerpt,
},
});
console.log(
`🤖 Excerpt: ${excerpt}\nLink: ${baseURL}/wp-admin/post.php?post=${postId}&action=edit`
);
} catch (error) {
console.error(error);
}
}
generatePostExcerpt({ postId: postId });