-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathsummarize.ts
69 lines (59 loc) · 1.39 KB
/
summarize.ts
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
import fs from "fs";
import axios from "axios";
import dotenv from "dotenv";
dotenv.config();
/**
* The API key for accessing the Dify.ai API.
*/
const apiKey = process.env.DIFY_API_KEY;
/**
* The file path of the text file to be summarized.
*/
const filePath = process.argv[2];
if (!filePath) {
console.log("Please provide a file path.");
process.exit(1);
}
if (!fs.existsSync(filePath)) {
console.log("The file path provided does not exist.");
process.exit(1);
}
/**
* The content of the text file to be summarized.
*/
const fileContent = fs.readFileSync(filePath, "utf-8");
/**
* The raw data to be sent to the Dify.ai API.
*/
const raw = JSON.stringify({
inputs: {},
query: `<input>${fileContent}</input>`,
response_mode: "blocking",
user: "abc-123",
});
/**
* Sends a request to the Dify.ai API to summarize the text file.
*/
const run = async () => {
console.log("Summarizing...\n\n\n");
try {
const res = await axios.post(
"https://api.dify.ai/v1/completion-messages",
raw,
{
headers: {
Authorization: "Bearer " + apiKey,
"Content-Type": "application/json",
},
}
);
/**
* The summarized text returned by the Dify.ai API.
*/
const result = res.data.answer.replace(/\n\n/g, "\n").trim();
console.log(result);
} catch (e: any) {
console.error("Error:" + e.message);
}
};
run();