Best practice content dir generation

pull/5388/head
Kamran Ahmed 8 months ago
parent cdb9a87c85
commit 2ab7690271
  1. 43
      scripts/best-practice-content.cjs

@ -2,14 +2,7 @@ const fs = require('fs');
const path = require('path');
const OPEN_AI_API_KEY = process.env.OPEN_AI_API_KEY;
const ALL_BEST_PRACTICES_DIR = path.join(
__dirname,
'../src/data/best-practices'
);
const BEST_PRACTICE_JSON_DIR = path.join(
__dirname,
'../public/jsons/best-practices'
);
const ALL_BEST_PRACTICES_DIR = path.join(__dirname, '../src/data/best-practices');
const bestPracticeId = process.argv[2];
const bestPracticeTitle = bestPracticeId.replace(/-/g, ' ');
@ -26,18 +19,13 @@ if (!allowedBestPracticeIds.includes(bestPracticeId)) {
process.exit(1);
}
const BEST_PRACTICE_CONTENT_DIR = path.join(
ALL_BEST_PRACTICES_DIR,
bestPracticeId,
'content'
);
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
const BEST_PRACTICE_CONTENT_DIR = path.join(ALL_BEST_PRACTICES_DIR, bestPracticeId, 'content');
const OpenAI = require('openai');
const openai = new OpenAI({
apiKey: OPEN_AI_API_KEY,
});
const openai = new OpenAIApi(configuration);
function getFilesInFolder(folderPath, fileList = {}) {
const files = fs.readdirSync(folderPath);
@ -67,8 +55,8 @@ function writeTopicContent(topicTitle) {
console.log(`Generating '${topicTitle}'...`);
return new Promise((resolve, reject) => {
openai
.createChatCompletion({
openai.chat.completions
.create({
model: 'gpt-4',
messages: [
{
@ -78,7 +66,7 @@ function writeTopicContent(topicTitle) {
],
})
.then((response) => {
const article = response.data.choices[0].message.content;
const article = response.choices[0].message.content;
resolve(article);
})
@ -91,7 +79,7 @@ function writeTopicContent(topicTitle) {
async function writeFileForGroup(group, topicUrlToPathMapping) {
const topicId = group?.properties?.controlName;
const topicTitle = group?.children?.controls?.control?.find(
(control) => control?.typeID === 'Label'
(control) => control?.typeID === 'Label',
)?.properties?.text;
const currTopicUrl = `/${topicId}`;
if (currTopicUrl.startsWith('/check:')) {
@ -102,7 +90,6 @@ async function writeFileForGroup(group, topicUrlToPathMapping) {
if (!contentFilePath) {
console.log(`Missing file for: ${currTopicUrl}`);
process.exit(0);
return;
}
@ -123,7 +110,7 @@ async function writeFileForGroup(group, topicUrlToPathMapping) {
return;
}
const topicContent = await writeTopicContent(topicTitle);
const topicContent = await writeTopicContent(currTopicUrl);
newFileContent += `\n\n${topicContent}`;
console.log(`Writing ${topicId}..`);
@ -138,14 +125,14 @@ async function writeFileForGroup(group, topicUrlToPathMapping) {
async function run() {
const topicUrlToPathMapping = getFilesInFolder(BEST_PRACTICE_CONTENT_DIR);
const bestPracticeJson = require(path.join(
BEST_PRACTICE_JSON_DIR,
`${bestPracticeId}.json`
));
const bestPracticeJson = require(
path.join(ALL_BEST_PRACTICES_DIR, `${bestPracticeId}/${bestPracticeId}`),
);
const groups = bestPracticeJson?.mockup?.controls?.control?.filter(
(control) =>
control.typeID === '__group__' &&
!control.properties?.controlName?.startsWith('ext_link')
!control.properties?.controlName?.startsWith('ext_link'),
);
if (!OPEN_AI_API_KEY) {

Loading…
Cancel
Save