gpt-po 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -1
- package/README_zh-CN.md +7 -0
- package/lib/package.json +1 -1
- package/lib/src/index.js +5 -3
- package/lib/src/translate.d.ts +3 -3
- package/lib/src/translate.js +12 -12
- package/lib/src/userprompt.txt +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,11 @@ Set `OPENAI_API_KEY` before using this tool.
|
|
|
19
19
|
|
|
20
20
|
**It is recommended to use the paid OpenAI API to improve translation speed, as the free OpenAI API is slower (only 3 translations per minute) and has usage restrictions.**
|
|
21
21
|
|
|
22
|
+
### Environment Variables
|
|
23
|
+
- `OPENAI_API_KEY`: OpenAI API key.
|
|
24
|
+
- `OPENAI_API_HOST`: OpenAI API host (default: https://api.openai.com).
|
|
25
|
+
- `OPENAI_MODEL_TMP`: OpenAI model temperature (default: 0.1).
|
|
26
|
+
|
|
22
27
|
## Usage Scenarios
|
|
23
28
|
|
|
24
29
|
- `gpt-po sync --po <file> --pot <file>` Update the po file based on the pot file, while preserving the original translations.
|
|
@@ -61,6 +66,8 @@ Options:
|
|
|
61
66
|
-l, --lang <lang> target language (ISO 639-1 code)
|
|
62
67
|
-o, --output <file> output file path, overwirte po file by default
|
|
63
68
|
--context <file> context file path (provides additional context to the bot)
|
|
69
|
+
--context-length <length> Maximum accumulated length of source strings (msgid) to translate in each API request (default: 2000, env: API_CONTEXT_LENGTH)
|
|
70
|
+
--timeout <ms> Timeout in milliseconds for API requests (default: 20000, env: API_TIMEOUT)
|
|
64
71
|
-h, --help display help for command
|
|
65
72
|
```
|
|
66
73
|
|
|
@@ -90,4 +97,4 @@ Options:
|
|
|
90
97
|
--po <file> po file path
|
|
91
98
|
--pot <file> pot file path
|
|
92
99
|
-h, --help display help for command
|
|
93
|
-
```
|
|
100
|
+
```
|
package/README_zh-CN.md
CHANGED
|
@@ -21,6 +21,11 @@ npm install gpt-po
|
|
|
21
21
|
|
|
22
22
|
*国内用户要设置`HTTPS_PROXY`环境变量上梯子才能用*
|
|
23
23
|
|
|
24
|
+
### 环境变量
|
|
25
|
+
- `OPENAI_API_KEY`: OpenAI API密钥。
|
|
26
|
+
- `OPENAI_API_HOST`: OpenAI API主机(默认:https://api.openai.com)。
|
|
27
|
+
- `OPENAI_MODEL_TMP`: OpenAI模型温度(默认:0.1)。
|
|
28
|
+
|
|
24
29
|
## 常见用法
|
|
25
30
|
|
|
26
31
|
- `gpt-po sync --po <file> --pot <file>` 根据 pot 文件更新 po 文件,同时保留原有翻译。
|
|
@@ -63,6 +68,8 @@ npm install gpt-po
|
|
|
63
68
|
-l, --lang <lang> 目标语言 (ISO 639-1 代码)
|
|
64
69
|
-o, --output <file> 输出文件路径,默认覆盖 po 文件
|
|
65
70
|
--context <file> 上下文文件路径(为机器人提供额外的上下文)
|
|
71
|
+
--context-length <length> 每次 API 请求中源字符串(msgid)的最大累计长度(默认:2000,环境变量:API_CONTEXT_LENGTH)
|
|
72
|
+
--timeout <ms> API 请求超时时间(单位:毫秒,默认:20000,环境变量:API_TIMEOUT)
|
|
66
73
|
-h, --help 显示命令帮助
|
|
67
74
|
```
|
|
68
75
|
|
package/lib/package.json
CHANGED
package/lib/src/index.js
CHANGED
|
@@ -39,10 +39,12 @@ const translateCommand = new SharedOptionsCommand("translate")
|
|
|
39
39
|
.option("-l, --lang <lang>", "target language (ISO 639-1)")
|
|
40
40
|
.option("--verbose", "print verbose log")
|
|
41
41
|
.option("--context <file>", "text file that provides the bot additional context")
|
|
42
|
+
.addOption(new Option("--context-length <length>", "maximum accumulated length of source strings (msgid) to translate in each API request").env("API_CONTEXT_LENGTH").default("2000"))
|
|
43
|
+
.addOption(new Option("--timeout <ms>", "timeout in milliseconds for API requests").env("API_TIMEOUT").default("20000"))
|
|
42
44
|
.addOption(new Option("-o, --output <file>", "output file path, overwirte po file by default").conflicts("dir"))
|
|
43
45
|
.addCompileOptions()
|
|
44
46
|
.action(async (args) => {
|
|
45
|
-
const { key, host, model, po, dir, source, lang, verbose, output, context } = args;
|
|
47
|
+
const { key, host, model, po, dir, source, lang, verbose, output, context, contextLength, timeout } = args;
|
|
46
48
|
if (host) {
|
|
47
49
|
process.env.OPENAI_API_HOST = host;
|
|
48
50
|
}
|
|
@@ -57,10 +59,10 @@ const translateCommand = new SharedOptionsCommand("translate")
|
|
|
57
59
|
init();
|
|
58
60
|
const compileOptions = getCompileOptions(args);
|
|
59
61
|
if (po) {
|
|
60
|
-
await translatePo(model, po, source, lang, verbose, output, context, compileOptions);
|
|
62
|
+
await translatePo(model, po, source, lang, verbose, output, context, parseInt(contextLength), parseInt(timeout), compileOptions);
|
|
61
63
|
}
|
|
62
64
|
else if (dir) {
|
|
63
|
-
await translatePoDir(model, dir, source, lang, verbose, context, compileOptions);
|
|
65
|
+
await translatePoDir(model, dir, source, lang, verbose, context, parseInt(contextLength), parseInt(timeout), compileOptions);
|
|
64
66
|
}
|
|
65
67
|
else {
|
|
66
68
|
console.error("po file or directory is required");
|
package/lib/src/translate.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ import { GetTextTranslation } from "gettext-parser";
|
|
|
2
2
|
import { OpenAI } from "openai";
|
|
3
3
|
import { CompileOptions } from "./utils.js";
|
|
4
4
|
export declare function init(force?: boolean): OpenAI;
|
|
5
|
-
export declare function translate(src: string, lang: string, model: string, translations: GetTextTranslation[], contextFile: string): Promise<void>;
|
|
6
|
-
export declare function translatePo(model: string, po: string, source: string, lang: string, verbose: boolean, output: string, contextFile: string, compileOptions?: CompileOptions): Promise<void>;
|
|
7
|
-
export declare function translatePoDir(model: string | undefined, dir: string, source: string, lang: string, verbose: boolean, contextFile: string, compileOptions?: CompileOptions): Promise<void>;
|
|
5
|
+
export declare function translate(src: string, lang: string, model: string, translations: GetTextTranslation[], contextFile: string, timeout: number): Promise<void>;
|
|
6
|
+
export declare function translatePo(model: string, po: string, source: string, lang: string, verbose: boolean, output: string, contextFile: string, contextLength: number, timeout: number, compileOptions?: CompileOptions): Promise<void>;
|
|
7
|
+
export declare function translatePoDir(model: string | undefined, dir: string, source: string, lang: string, verbose: boolean, contextFile: string, contextLength: number, timeout: number, compileOptions?: CompileOptions): Promise<void>;
|
package/lib/src/translate.js
CHANGED
|
@@ -36,7 +36,7 @@ export function init(force) {
|
|
|
36
36
|
}
|
|
37
37
|
return _openai;
|
|
38
38
|
}
|
|
39
|
-
export async function translate(src, lang, model, translations, contextFile) {
|
|
39
|
+
export async function translate(src, lang, model, translations, contextFile, timeout) {
|
|
40
40
|
const lang_code = lang
|
|
41
41
|
.toLowerCase()
|
|
42
42
|
.trim()
|
|
@@ -65,7 +65,7 @@ export async function translate(src, lang, model, translations, contextFile) {
|
|
|
65
65
|
.join("\n");
|
|
66
66
|
const res = await _openai.chat.completions.create({
|
|
67
67
|
model: model,
|
|
68
|
-
temperature: 0.1,
|
|
68
|
+
temperature: process.env.OPENAI_MODEL_TMP ? parseFloat(process.env.OPENAI_MODEL_TMP) : 0.1,
|
|
69
69
|
messages: [
|
|
70
70
|
{
|
|
71
71
|
role: "system",
|
|
@@ -73,12 +73,12 @@ export async function translate(src, lang, model, translations, contextFile) {
|
|
|
73
73
|
},
|
|
74
74
|
{
|
|
75
75
|
role: "user",
|
|
76
|
-
content: `${_userprompt}\n\nWait for my incoming message in
|
|
76
|
+
content: `${_userprompt}\n\nWait for my incoming message(s) in \`${src}\` and translate them into \`${lang}\` (\`${src}\` and \`${lang}\` are XPG/POSIX locale names, used in Unix-like systems and GNU Gettext). ` +
|
|
77
77
|
notes
|
|
78
78
|
},
|
|
79
79
|
{
|
|
80
80
|
role: "assistant",
|
|
81
|
-
content: `Understood, I will translate your incoming
|
|
81
|
+
content: `Understood, I will translate your incoming \`${src}\` message(s) into \`${lang}\`, carefully following guidelines. Please go ahead and send your message(s) for translation.`
|
|
82
82
|
},
|
|
83
83
|
// add userdict
|
|
84
84
|
...(dicts.user.length > 0
|
|
@@ -94,7 +94,7 @@ export async function translate(src, lang, model, translations, contextFile) {
|
|
|
94
94
|
}
|
|
95
95
|
]
|
|
96
96
|
}, {
|
|
97
|
-
timeout
|
|
97
|
+
timeout,
|
|
98
98
|
stream: false
|
|
99
99
|
});
|
|
100
100
|
const content = res.choices[0].message.content ?? "";
|
|
@@ -110,7 +110,7 @@ export async function translate(src, lang, model, translations, contextFile) {
|
|
|
110
110
|
}
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
|
-
export async function translatePo(model, po, source, lang, verbose, output, contextFile, compileOptions) {
|
|
113
|
+
export async function translatePo(model, po, source, lang, verbose, output, contextFile, contextLength, timeout, compileOptions) {
|
|
114
114
|
const potrans = await parsePo(po);
|
|
115
115
|
if (!lang)
|
|
116
116
|
lang = potrans.headers["Language"];
|
|
@@ -170,13 +170,13 @@ export async function translatePo(model, po, source, lang, verbose, output, cont
|
|
|
170
170
|
await new Promise((resolve) => setTimeout(resolve, 20000));
|
|
171
171
|
}
|
|
172
172
|
const trans = list[i];
|
|
173
|
-
if (c <
|
|
173
|
+
if (c < contextLength) {
|
|
174
174
|
translations.push(trans);
|
|
175
175
|
c += trans.msgid.length;
|
|
176
176
|
}
|
|
177
|
-
if (c >=
|
|
177
|
+
if (c >= contextLength || i == list.length - 1) {
|
|
178
178
|
try {
|
|
179
|
-
await translate(source, lang, model, translations, contextFile);
|
|
179
|
+
await translate(source, lang, model, translations, contextFile, timeout);
|
|
180
180
|
if (verbose) {
|
|
181
181
|
translations.forEach((trans) => {
|
|
182
182
|
console.log(trans.msgid);
|
|
@@ -187,7 +187,7 @@ export async function translatePo(model, po, source, lang, verbose, output, cont
|
|
|
187
187
|
c = 0;
|
|
188
188
|
// update progress
|
|
189
189
|
printProgress(i + 1, list.length);
|
|
190
|
-
// save po file after each 2000 characters
|
|
190
|
+
// save po file after each 2000 characters by default
|
|
191
191
|
await compilePo(potrans, output || po, compileOptions);
|
|
192
192
|
}
|
|
193
193
|
catch (error) {
|
|
@@ -213,13 +213,13 @@ export async function translatePo(model, po, source, lang, verbose, output, cont
|
|
|
213
213
|
}
|
|
214
214
|
console.log("done.");
|
|
215
215
|
}
|
|
216
|
-
export async function translatePoDir(model = "gpt-3.5-turbo", dir, source, lang, verbose, contextFile, compileOptions) {
|
|
216
|
+
export async function translatePoDir(model = "gpt-3.5-turbo", dir, source, lang, verbose, contextFile, contextLength, timeout, compileOptions) {
|
|
217
217
|
const files = fs.readdirSync(dir);
|
|
218
218
|
for (const file of files) {
|
|
219
219
|
if (file.endsWith(".po")) {
|
|
220
220
|
const po = path.join(dir, file);
|
|
221
221
|
console.log(`translating ${po}`);
|
|
222
|
-
await translatePo(model, po, source, lang, verbose, po, contextFile, compileOptions);
|
|
222
|
+
await translatePo(model, po, source, lang, verbose, po, contextFile, contextLength, timeout, compileOptions);
|
|
223
223
|
}
|
|
224
224
|
}
|
|
225
225
|
}
|
package/lib/src/userprompt.txt
CHANGED
|
@@ -15,7 +15,7 @@ Translation guidelines are as follows:
|
|
|
15
15
|
4. **Context Handling**:
|
|
16
16
|
- Some messages will include a context attribute in the translate tag, e.g., `<translate index="1" context="Menu">`.
|
|
17
17
|
- Use this context to inform your translation but only return the translated text.
|
|
18
|
-
- Example:
|
|
18
|
+
- Example (translating from `en_GB` to `fr_FR`):
|
|
19
19
|
Input: `<translate index="1" context="Menu">File</translate>`
|
|
20
20
|
Output: `<translated index="1">Fichier</translated>`
|
|
21
21
|
|
|
@@ -23,7 +23,7 @@ Translation guidelines are as follows:
|
|
|
23
23
|
- You may receive multiple translation requests in a single input, each with a unique index.
|
|
24
24
|
- Ensuring the complete number of requests are translated, even if they are repeated, while maintaining the original order when possible.
|
|
25
25
|
|
|
26
|
-
**
|
|
26
|
+
**Example (translating from `en_US` to `zh_CN`):**
|
|
27
27
|
- Input:
|
|
28
28
|
`<translate index="1">This is a message. </translate>`
|
|
29
29
|
`<translate index="2"> Hello %s</translate>`
|