diff --git a/book.ts b/book.ts index 3f886a1..173e3a5 100644 --- a/book.ts +++ b/book.ts @@ -190,6 +190,15 @@ Chapter summary: ${ch.summary}`; }; }; +const getLastLine = (str: string): string => { + const lastLineIndex = str.lastIndexOf("\n"); + if (lastLineIndex === -1) { + return str; + } else { + return str.slice(lastLineIndex + 1); + } +}; + // https://pandoc.org/epub.html export const writeChapterScene = async ( dirName: string, @@ -213,7 +222,7 @@ Scene summary: ${scene} ${ : "" }`; - const sceneInfo = await openai.createChatCompletion({ + let sceneInfo = await openai.createChatCompletion({ model: "gpt-3.5-turbo", messages: [ { @@ -223,12 +232,31 @@ Scene summary: ${scene} ${ ], }); + let prose = sceneInfo.data.choices[0].message?.content as string; + + sceneInfo = await openai.createChatCompletion({ + model: "gpt-3.5-turbo", + messages: [ + { + role: "user", + content: prompt, + }, + { + role: "assistant", + content: getLastLine(prose), + }, + { + role: "user", + content: "Continue writing the story.", + }, + ], + }); + + prose = (prose + "\n\n" + sceneInfo.data.choices[0].message?.content) as string; + const fname = `ch-${chNum}-sc-${sceneNum}.md`; - await fs.writeFile( - `${dirName}/src/${fname}`, - sceneInfo.data.choices[0].message?.content as string - ); + await fs.writeFile(`${dirName}/src/${fname}`, prose); console.log(`wrote chapter ${chNum} scene ${sceneNum}`); diff --git a/index.ts b/index.ts index b64f998..a599723 100644 --- a/index.ts +++ b/index.ts @@ -220,4 +220,24 @@ language: en-US console.log(await execa("pandoc", args)); }); +program + .command("writeOneScene ") + .description("write a single scene of the novel, useful for debugging the generation process") + .action(async (dir) => { + await fs.mkdir(`${dir}/src`, { recursive: true }); + + const summary: book.Summary = JSON.parse(await fs.readFile(`${dir}/summary.json`, "utf8")); + const chapters: book.Chapter[] = JSON.parse( + await fs.readFile(`${dir}/chapterScenes.json`, "utf8") + ); + + const chNum = 1; + const sceneNum = 2; + + const ch = chapters[chNum - 1]; + const scene = ch.sceneDescriptions[sceneNum - 1]; + + await book.writeChapterScene(dir, openai, summary, ch, chNum, sceneNum, scene); + }); + program.parse();