From 84dfa123263cef778e243fb72ab493ba1730aade Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Fri, 14 Apr 2023 17:20:15 -0400 Subject: [PATCH] write scene details Signed-off-by: Xe Iaso --- book.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.ts | 29 +++++++++++++++++++++++++++ tsconfig.json | 3 +-- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/book.ts b/book.ts index 9067257..c1c71b6 100644 --- a/book.ts +++ b/book.ts @@ -145,3 +145,57 @@ export const createAndParseSummary = async ( return bookInfo; }; + +export const createChapterScenes = async ( + dirName: string, + openai: OpenAIApi, + summary: Summary, + ch: ChapterListItem +): Promise => { + console.log(`creating chapter scene information for chapter ${ch.title}`); + + const prompt = + `Given the following plot summary, character information, and chapter information, write descriptions of scenes that would happen in that chapter. End each description with two newlines. Write at least 4 scenes. Use detail and be creative. DO NOT include the chapter title in your output. ONLY output the scenes separated by newlines like this. + +What happens first. + +What happens after that. + +Plot summary: ${summary.plotSummary} +Character information: +` + + summary.characters.map((char) => `- ${char.name}: ${char.role}`).join("\n") + + ` +Chapter title: ${ch.title} +Chapter summary: ${ch.summary}`; + + const chInfo = await openai.createChatCompletion({ + model: "gpt-3.5-turbo", + messages: [ + { + role: "user", + content: prompt, + }, + ], + }); + + const chInfoText = chInfo.data.choices[0].message?.content as string; + + return { + title: ch.title, + summary: ch.summary, + sceneDescriptions: chInfoText.split("\n\n"), + }; +}; + +export const writeChapterScene = async ( + dirName: string, + openai: OpenAIApi, + summary: Summary, + ch: Chapter, + chNum: number, + sceneNum: number, + scene: string +): Promise => { + return ""; +}; diff --git a/index.ts b/index.ts index 2d98cca..f939d17 100644 --- a/index.ts +++ b/index.ts @@ -104,4 +104,33 @@ program summary.chapterList.forEach(({ title, summary }) => console.log(`- ${title} - ${summary}`)); }); +program + .command("genChapterScenes ") + .description("generate the list of scenes for all of the chapters in the chapter") + .action(async (dir) => { + if (!fileExists(dir)) { + console.error(`${dir} does not exist, run init?`); + process.exit(1); + } + if (!fileExists(`${dir}/summary.json`)) { + console.error(`plot summary does not exist in ${dir}, run genSummary?`); + process.exit(1); + } + if (fileExists(`${dir}/chapterScenes.json`)) { + console.error(`plot summary already exists in ${dir}`); + process.exit(1); + } + + const summary: book.Summary = JSON.parse(await fs.readFile(`${dir}/summary.json`, "utf8")); + const chapters: book.Chapter[] = []; + + for (const ch of summary.chapterList) { + chapters.push(await book.createChapterScenes(dir, openai, summary, ch)); + } + + console.log(chapters); + + await fs.writeFile(`${dir}/chapterScenes.json`, JSON.stringify(chapters)); + }); + program.parse(); diff --git a/tsconfig.json b/tsconfig.json index 08fd84a..88ec98d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,8 +10,7 @@ "sourceMap": true, "target": "es2022", "types": ["node"], - "outDir": "dist", - "allowImportingTsExtensions": true + "outDir": "dist" }, "exclude": ["node_modules"] }