forked from vertesia/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (62 loc) · 2.17 KB
/
index.js
File metadata and controls
62 lines (62 loc) · 2.17 KB
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
import { readFileSync, writeFileSync } from "fs";
import { encoding_for_model } from "tiktoken";
import { ProofreadDocumentation, configure } from "./interactions.js";
const apiKey = process.env.COMPOSABLE_PROMPTS_API_KEY;
const serverUrl = process.env.COMPOSABLE_PROMPTS_SERVER_URL || undefined;
const stream = false;
const file = process.argv[2];
if (!apiKey) {
throw new Error("No API key provided.");
}
configure({
apikey: apiKey,
serverUrl: serverUrl,
});
//call the interaction to proofread the content
async function proofread(content, tokenCount) {
//instantiating the interaction
const req = new ProofreadDocumentation();
//launch the execution with a custom max_tokens
//here you could also change the model or environment
const res = req.execute({
config: {
max_tokens: 8000 - 200 - tokenCount, //200 to take prompt template into account
},
data: {
current_doc: content,
},
});
return res;
}
//count token using the GPT-4 tokenizer
function getTokenCount(text) {
const encoder = encoding_for_model("gpt-4");
const tokens = encoder.encode(text);
encoder.free();
return tokens.length;
}
//date the updated file under <name>.updated.ext
function saveUpdateFile(originalName, content) {
const parts = originalName.split(".");
const ext = parts.pop();
const newName = parts.join(".") + ".updated." + ext;
console.log("Saving updated file: ", newName);
writeFileSync(newName, content, "utf8");
}
async function main() {
if (!file) {
throw new Error("No file provided.");
}
const fileContent = readFileSync(file, "utf8");
const tokenCount = getTokenCount(fileContent);
console.log("Token Count: ", tokenCount);
const res = await proofread(fileContent, tokenCount);
console.log("Response: ");
console.log("Received response in " + res.execution_time + "ms");
console.log("Length of updated doc: " + res.result.updated_doc.length);
console.log("Token used", JSON.stringify(res.token_use, null, 2));
console.log("Summary of Changes: ", res.result.changes_summary);
saveUpdateFile(file, res.result.updated_doc);
return;
}
main();