From 239395747eca1b6ec3599b49d0c70057d507cca2 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Wed, 12 Apr 2023 16:02:41 -0400 Subject: [PATCH] set up per-novel temporary folders with generated data Signed-off-by: Xe Iaso --- index.ts | 34 ++++++++++++++++++++++++++++++---- var/.gitignore | 2 ++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 var/.gitignore diff --git a/index.ts b/index.ts index fa3b886..a74986c 100644 --- a/index.ts +++ b/index.ts @@ -1,24 +1,50 @@ import * as dotenv from "dotenv"; import { Configuration, OpenAIApi } from "openai"; import PlotGenerator from "@xeserv/plottoriffic"; +import { generateName } from "@kotofurumiya/th-namegen"; +import * as fs from "node:fs/promises"; dotenv.config(); -const pg = new PlotGenerator({}); +const dirName = `var/${generateName()}`; +await fs.mkdir(dirName, { recursive: true }); +console.log(`dirName: ${dirName}`); + +const pg = new PlotGenerator({ flipGenders: false }); const plot = pg.generate(); +await fs.writeFile(`${dirName}/plotto.json`, JSON.stringify(plot)); + const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(configuration); -const completion = await openai.createChatCompletion({ +plot.cast.forEach(async (ch) => {}); + +const promptBase = `Write me the following about the following plot summary for a novel: + +- A two word title for the novel starting with "Title: " and followed by two newlines. +- A detailed plot summary for the story starting with "Plot Summary: " and followed by two newlines. +- The string "Chapter Summaries" followed by two newlines. +- A markdown list of detailed chapter summaries in at least 3 sentences and titles for each of the 10 chapters that a novel based on the plot summary would have. Surround each chapter title in quotes and put a dash after the name like this: "Chapter name" - Chapter summary goes here.`; + +const summary = await openai.createChatCompletion({ model: "gpt-3.5-turbo", messages: [ { role: "user", - content: "Write me a plot summary for the following story:\n\n" + plot.plot, + content: promptBase + "\n\n" + plot.plot, }, ], }); -console.log(completion.data.choices[0].message); + +if (!!summary.data.usage) { + const usage = summary.data.usage; + console.log( + `${usage.total_tokens} tokens (${usage.prompt_tokens} prompt, ${usage?.completion_tokens} completion)` + ); +} + +console.log(summary.data.choices[0].message?.content); +await fs.writeFile(`${dirName}/summary.txt`, summary.data.choices[0].message?.content as string); diff --git a/var/.gitignore b/var/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/var/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore