-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
87 lines (69 loc) · 2.38 KB
/
index.js
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
import {
createFolder,
copyFile,
readFileContent,
writeOutputFile,
} from "./fileOperations.js";
import { displayProgressBar } from "./progressBar.js";
import {
estimateTokenCount,
calculateMaxChunkSize,
processChunk,
processDialogues,
countWords,
} from "./textProcessing.js";
import { resolveEndIndex } from "./resolveEndIndex.js";
import { buildMessages } from "./promptBuilder.js";
import { printSummary } from "./summary.js";
import { MODEL_TOKEN_LIMIT, SAFETY_MARGIN } from "./config.js";
function getTimestamp() {
return new Date().toISOString().replace(/[:.]/g, "-");
}
async function getDialogs(inputFilePath) {
const startTime = Date.now();
const timestamp = getTimestamp();
const resultFolderPath = `results/${timestamp}`;
const inputFileCopyPath = `${resultFolderPath}/input.txt`;
const outputFilePath = `${resultFolderPath}/output.json`;
try {
await createFolder(resultFolderPath);
await copyFile(inputFilePath, inputFileCopyPath);
const text = await readFileContent(inputFilePath);
let startIndex = 0;
let endIndex;
let responseContent = [];
let titles = [];
while (startIndex < text.length) {
const systemMessage = buildMessages("")[0];
const systemMessageTokenCount = estimateTokenCount(systemMessage.content);
const maxChunkSize = calculateMaxChunkSize(
systemMessageTokenCount,
MODEL_TOKEN_LIMIT,
SAFETY_MARGIN
);
endIndex = resolveEndIndex(text, startIndex, maxChunkSize);
displayProgressBar(endIndex, text.length);
try {
const chunkContent = await processChunk(text, startIndex, endIndex);
responseContent.push(chunkContent);
titles.push(
...chunkContent.map(
(item) => `${item.title} (${countWords(item.dialogue)} words)`
)
);
} catch (processError) {
console.error("Error processing chunk:", processError.message);
}
startIndex = endIndex;
}
responseContent = responseContent.flat();
responseContent = processDialogues(responseContent);
await writeOutputFile(outputFilePath, responseContent);
const endTime = Date.now();
const totalTime = ((endTime - startTime) / 1000).toFixed(2);
printSummary(timestamp, titles, totalTime, text.length);
} catch (error) {
console.error("General error:", error.message);
}
}
getDialogs("./input.txt");