-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_a_book4.txt
113 lines (93 loc) · 3.03 KB
/
write_a_book4.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
102
103
104
105
106
107
108
109
110
111
112
113
Sure! I'll guide you through setting up a simple Node.js TypeScript server with an API endpoint to process the book writing steps, and then create a basic React component to follow along with each step and read the book as it's written.
Setting up a Node.js TypeScript server
Install the required dependencies:
bash
Copy code
npm init -y
npm install express typescript ts-node @types/node @types/express
Create a tsconfig.json file with the following content:
json
Copy code
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "dist"
},
"include": ["src"]
}
Create a src folder and inside it, create an index.ts file with the following content:
typescript
Copy code
import express from 'express';
const app = express();
const port = process.env.PORT || 3001;
app.use(express.json());
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Add a start script to your package.json:
json
Copy code
"scripts": {
"start": "ts-node src/index.ts"
}
Now you can start your server with npm start.
Creating a book writing API endpoint
First, you'll need to install the OpenAI SDK for JavaScript:
bash
Copy code
npm install openai
Then, move the code for the book writing functions to a new file, src/bookWriter.ts. Import the required modules and export the functions:
typescript
Copy code
import { CreateCompletionRequest } from 'openai';
import * as fs from 'fs/promises';
import openai from './openai';
// Add your existing functions here
// ...
export {
summarizeContent,
writeNewChapter,
editChapter,
improveChapter,
};
In src/index.ts, import your book writing functions and create an API endpoint for processing each step:
typescript
Copy code
import express from 'express';
import { summarizeContent, writeNewChapter, editChapter, improveChapter } from './bookWriter';
const app = express();
const port = process.env.PORT || 3001;
app.use(express.json());
// Add the book writing API endpoint
app.post('/process_step', async (req, res) => {
const { step, text, chapterNumber, previousChapters, summaries } = req.body;
try {
let responseText = '';
if (step === 0) {
responseText = await summarizeContent(text, chapterNumber);
} else if (step === 1) {
responseText = await writeNewChapter(text, chapterNumber, previousChapters);
} else if (step === 2) {
responseText = await editChapter(text, chapterNumber, previousChapters, summaries);
} else if (step === 3) {
responseText = await improveChapter(text, chapterNumber, previousChapters, summaries);
} else {
return res.status(400).json({ error: 'Invalid step number' });
}
res.status(200).json({ text: responseText });
} catch (error) {
res.status(500).json({ error: 'An error occurred while processing the step' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});