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
|
// https://pandoc.org/epub.html
|
||||||
export const writeChapterScene = async (
|
export const writeChapterScene = async (
|
||||||
dirName: string,
|
dirName: string,
|
||||||
@ -213,7 +222,7 @@ Scene summary: ${scene} ${
|
|||||||
: ""
|
: ""
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
const sceneInfo = await openai.createChatCompletion({
|
let sceneInfo = await openai.createChatCompletion({
|
||||||
model: "gpt-3.5-turbo",
|
model: "gpt-3.5-turbo",
|
||||||
messages: [
|
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`;
|
const fname = `ch-${chNum}-sc-${sceneNum}.md`;
|
||||||
|
|
||||||
await fs.writeFile(
|
await fs.writeFile(`${dirName}/src/${fname}`, prose);
|
||||||
`${dirName}/src/${fname}`,
|
|
||||||
sceneInfo.data.choices[0].message?.content as string
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(`wrote chapter ${chNum} scene ${sceneNum}`);
|
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));
|
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();
|
program.parse();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user