few-shot the prose generation process
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
7a08d39f32
commit
2bcb73d0a5
38
book.ts
38
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}`);
|
||||
|
||||
|
20
index.ts
20
index.ts
@ -220,4 +220,24 @@ language: en-US
|
||||
console.log(await execa("pandoc", args));
|
||||
});
|
||||
|
||||
program
|
||||
.command("writeOneScene <dir>")
|
||||
.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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user