gpt-po 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/package.json +1 -1
- package/lib/src/index.js +12 -2
- package/lib/src/systemprompt.txt +1 -1
- package/lib/src/translate.js +5 -7
- package/lib/src/utils.d.ts +2 -1
- package/lib/src/utils.js +6 -3
- package/package.json +1 -1
package/lib/package.json
CHANGED
package/lib/src/index.js
CHANGED
@@ -46,6 +46,11 @@ program
|
|
46
46
|
if (key) {
|
47
47
|
process.env.OPENAI_API_KEY = key;
|
48
48
|
}
|
49
|
+
// process.env.OPENAI_API_KEY is not set, exit
|
50
|
+
if (!process.env.OPENAI_API_KEY) {
|
51
|
+
console.error("OPENAI_API_KEY is required");
|
52
|
+
process.exit(1);
|
53
|
+
}
|
49
54
|
(0, translate_1.init)();
|
50
55
|
if (po) {
|
51
56
|
yield (0, translate_1.translatePo)(model, po, source, lang, verbose, output);
|
@@ -70,12 +75,17 @@ program
|
|
70
75
|
program
|
71
76
|
.command("systemprompt")
|
72
77
|
.description("open/edit system prompt")
|
73
|
-
.
|
78
|
+
.option("--reset", "reset system prompt to default")
|
79
|
+
.action((args) => {
|
80
|
+
const { reset } = args;
|
74
81
|
// open `systemprompt.txt` file by system text default editor
|
75
82
|
const copyFile = __dirname + "/systemprompt.txt";
|
76
83
|
// user home path
|
77
84
|
const promptFile = (0, utils_1.findConfig)("systemprompt.txt");
|
78
|
-
(0, utils_1.copyFileIfNotExists)(promptFile, copyFile);
|
85
|
+
(0, utils_1.copyFileIfNotExists)(promptFile, copyFile, reset);
|
86
|
+
if (reset) {
|
87
|
+
console.log("systemprompt.txt reset to default");
|
88
|
+
}
|
79
89
|
(0, utils_1.openFileByDefault)(promptFile);
|
80
90
|
});
|
81
91
|
// program command `userdict` with help text `open/edit user dictionary`
|
package/lib/src/systemprompt.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
You are a translation expert,
|
1
|
+
You are a translation expert, translate the text in a colloquial, professional and elegant manner without sounding like a machine translation. Remember to only translate the text and not interpret it further.
|
package/lib/src/translate.js
CHANGED
@@ -54,14 +54,9 @@ function translate(text, src, lang, model = "gpt-3.5-turbo") {
|
|
54
54
|
model,
|
55
55
|
temperature: 0.1,
|
56
56
|
messages: [
|
57
|
-
{ role: "system", content: _systemprompt },
|
58
57
|
{
|
59
|
-
role: "
|
60
|
-
content: `Translate the ${src} content
|
61
|
-
},
|
62
|
-
{
|
63
|
-
role: "assistant",
|
64
|
-
content: "Sure, Please send me the content that needs to be translated.",
|
58
|
+
role: "system",
|
59
|
+
content: _systemprompt + ` Translate the ${src} user content into ${lang} language. Please translate it as a text, not as a table. The parts that cannot be translated will retain their original format.`
|
65
60
|
},
|
66
61
|
// add userdict here
|
67
62
|
...dicts,
|
@@ -136,6 +131,9 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output)
|
|
136
131
|
}
|
137
132
|
else {
|
138
133
|
console.error(error.message);
|
134
|
+
if (error.code == "ECONNABORTED") {
|
135
|
+
console.log('you may need to set "HTTPS_PROXY" to reach openai api.');
|
136
|
+
}
|
139
137
|
}
|
140
138
|
}
|
141
139
|
}
|
package/lib/src/utils.d.ts
CHANGED
@@ -3,8 +3,9 @@ import { GetTextTranslations } from "gettext-parser";
|
|
3
3
|
* copy source file to destination file if destination file does not exist
|
4
4
|
* @param file destination file path
|
5
5
|
* @param copyFile source file path
|
6
|
+
* @param force force copy file
|
6
7
|
*/
|
7
|
-
export declare function copyFileIfNotExists(file: string, copyFile: string): void;
|
8
|
+
export declare function copyFileIfNotExists(file: string, copyFile: string, force?: boolean): void;
|
8
9
|
export declare function openFileByDefault(filePath: string): void;
|
9
10
|
export declare function parsePo(poFile: string, defaultCharset?: string): Promise<GetTextTranslations>;
|
10
11
|
export declare function compilePo(data: GetTextTranslations, poFile: string): Promise<void>;
|
package/lib/src/utils.js
CHANGED
@@ -10,15 +10,18 @@ const path = require("path");
|
|
10
10
|
* copy source file to destination file if destination file does not exist
|
11
11
|
* @param file destination file path
|
12
12
|
* @param copyFile source file path
|
13
|
+
* @param force force copy file
|
13
14
|
*/
|
14
|
-
function copyFileIfNotExists(file, copyFile) {
|
15
|
+
function copyFileIfNotExists(file, copyFile, force = false) {
|
15
16
|
// make sure the directory exists
|
16
17
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
17
18
|
// check if file exists else create it
|
18
19
|
try {
|
19
20
|
fs.accessSync(file, fs.constants.F_OK);
|
20
|
-
// check if the file is empty
|
21
|
-
fs.statSync(file).size === 0
|
21
|
+
// check if the file is empty or force, copy the file
|
22
|
+
if (force || fs.statSync(file).size === 0) {
|
23
|
+
fs.copyFileSync(copyFile, file);
|
24
|
+
}
|
22
25
|
}
|
23
26
|
catch (err) {
|
24
27
|
fs.copyFileSync(copyFile, file);
|