-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_a_book2.txt
101 lines (80 loc) · 3.48 KB
/
write_a_book2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Great! I'm glad you found the updated prompts satisfactory. Now, let's move on to creating TypeScript code examples for each step of the process. I will explain each step and ensure you understand before moving on to the next one.
For this task, we'll assume that you're using the OpenAI API and have installed the @openai/client package from npm. Make sure you have your API key handy.
First, let's import the necessary libraries and set up the OpenAI client:
typescript
Copy code
import { OpenAIClient, CreateCompletionRequest } from '@openai/client';
const openai = new OpenAIClient('<YOUR_API_KEY>');
Replace <YOUR_API_KEY> with your actual API key.
Now, let's create a function to send prompts to the OpenAI API:
typescript
Copy code
async function sendPrompt(prompt: string): Promise<string> {
const request: CreateCompletionRequest = {
engine: 'text-davinci-003',
prompt: prompt,
max_tokens: 150,
n: 1,
stop: null,
temperature: 0.8,
};
try {
const response = await openai.createCompletion(request);
return response.choices[0].text.trim();
} catch (error) {
console.error('Error:', error);
return '';
}
}
Now we can start implementing each step:
Step 0: Summarizing the written content
Create a function to summarize the content:
typescript
Copy code
async function summarizeContent(text: string): Promise<string> {
const prompt = `Summarize the key events, characters, and themes of the following text as bullet points. Keep the summary concise and informative.
Text: """
${text}
"""`;
const summary = await sendPrompt(prompt);
return summary;
}
Step 1: Writing a new chapter
Create a function to write a new chapter:
typescript
Copy code
async function writeNewChapter(summaries: string, previousChapters: string): Promise<string> {
const prompt = `Using the context provided below, write a new chapter for a captivating love story set in the future.
The writing style should resemble Kurt Vonnegut but also draw inspiration from Steven King, Stanislaw Lem, and
Isaac Asimov. Make it a page-turner that is both entertaining and thought-provoking. a chapters length should be between 1,500 words—or six book pages— and 8,000 words, or 32 book pages
Context:
- Summaries: ${summaries}
- Previous chapters: ${previousChapters}`;
const newChapter = await sendPrompt(prompt);
return newChapter;
}
Step 2: Editing the new chapter
Create a function to edit the new chapter:
typescript
Copy code
async function editChapter(newChapter: string): Promise<string> {
const prompt = `Review the following chapter and provide suggestions for improvement in terms of flow, style, and coherence. Ensure that the writing style remains consistent and engaging.
Chapter:
${newChapter}`;
const editSuggestions = await sendPrompt(prompt);
return editSuggestions;
}
Step 3: Improving the chapter
Create a function to improve the chapter:
typescript
Copy code
async function improveChapter(newChapter: string, editSuggestions: string): Promise<string> {
const prompt = `Incorporate the suggestions provided below to rewrite and enhance the chapter. Maintain the engaging and thought-provoking style throughout the revised chapter.
Edit suggestions:
${editSuggestions}
Original chapter:
${newChapter}`;
const improvedChapter = await sendPrompt(prompt);
return improvedChapter;
}
These functions implement the steps you described, using the updated prompts. To create the book, you can call these functions in a loop, store the finalized chapters, and use the summaries to