Chatgpt还展示了其作为强大的翻译人员的能力,不仅能够处理通用语言,还可以处理表情符号和词语等非常规的写作形式。但是,即使在指示遵循精确的指令并将模型temperature参数设置为0时,它可能并不总是会产生确定性的输出并遵守线对线相关性,并可能破坏字幕的时间。
该实用程序使用OpenAI Chatgpt API翻译文本,特别关注基于线路的翻译,尤其是对于SRT字幕。 Translator通过删除SRT开销,将文本分组为批处理,从而优化令牌用法,从而在没有过多的令牌消耗的情况下进行任意长度翻译,同时确保线路输入和输出之间的一对一匹配。
--experimental-structured-mode 。--experimental-use-full-context (仅CLI)。参考:https://github.com/openai/openai-quickstart-node#setup
>= 16.13.0必需。此读书我假设bash壳环境git clone https://github.com/Cerlancism/chatgpt-subtitle-translator && cd chatgpt-subtitle-translatornpm installchmod +x cli/translator.mjs.example.env复制到.env cp .env.example .env.env文件中 cli/translator.mjs --help
Usage: translator [options]
Translation tool based on ChatGPT API
选项:
--from <language>
源语言(默认:“”)
--to <language>
目标语言(默认:“英语”)
-i, --input <file>
输入源文本带有此文件的内容,以.srt格式或纯文本
-o, --output <file>
输出文件名,默认为基于输入文件名称
-p, --plain-text <text>
带有此纯文本参数的输入源文本
-s, --system-instruction <instruction>
覆盖提示系统指令模板Translate ${from} to ${to}并使用此纯文本,忽略--from和--to选项
--initial-prompts <prompts>
初始提示JSON中的翻译(默认: "[]" )
--no-use-moderator
不要使用OpenAI API端点
--moderation-model
(默认值: "omni-moderation-latest" )https://platform.openai.com/docs/models/moderation
--no-prefix-number
不要以数值索引前缀线
--no-line-matching
不要强制执行一到一行的数量输入输出匹配
-l, --history-prompt-length <length>
下一个请求批次保留的及时历史记录(默认:10)
-b, --batch-sizes <sizes>批量的订单增加了json数组中翻译提示片的订单(默认值: "[10,100]" )
只要估计在令牌限制内,每个翻译提示中要包含的行数。如果输出线数量不匹配,则该数字将根据阵列中的值逐步减少,最终达到一个。
较大的批量大小通常会导致更有效的令牌利用率和潜在的更好的上下文翻译。但是,不匹配的输出线数量或超过令牌限制将导致令牌浪费,需要重新提交具有较小批次大小的批次。
--experimental-structured-mode [mode]
启用结构化响应。 (默认值: array ,选择array , object )
--experimental-structured-mode array结构输入和输出为普通阵列格式。与基本模式相比,此选项比基本模式更简洁,尽管它每批使用了更多的令牌。--experimental-structured-mode object结构将输入和输出到动态生成的对象架构中。此选项更加简洁,使用更少的令牌,但需要较小的批量尺寸,并且可能会缓慢且不可靠。由于其不可靠性,它可能会导致更多的重新提取,并在此过程中浪费更多的令牌。 --experimental-use-full-context
包括翻译数据的完整上下文,可与及时缓存配合使用。
每个用户和助手消息对的翻译行被切成薄片,按照--history-prompt-length --history-prompt-length 10 )的定义,建议将其设置为最大的批量尺寸(默认情况下--batch-sizes "[10,100]" ): --history-prompt-length 100 。
启用这一点可能会冒险进入模型上下文窗口限制,通常为128K ,但对于大多数情况来说应该足够。
--log-level <level>
日志级别(默认: debug ,选择: trace , debug , info , warn , error , silent )
--silent
与--log-level silent相同
--quiet
与--log-level silent相同
GPT的其他选项:
-m, --model <model>"gpt-4o-mini" )https://platform.openai.com/docs/api-reference/chat/create--stream-t, --temperature <temperature>0.3的低值,以使翻译更确定性(默认值: 1 )https://platform.openai.com/docs/api-reference/chat/create#chat-chat-chat-create-create-temperature--top_p <top_p>--presence_penalty <presence_penalty>--frequency_penalty <frequency_penalty--logit_bias <logit_bias>cli/translator.mjs --plain-text "你好"标准输出
Hello.
cli/translator.mjs --stream --to " Emojis " --temperature 0 --plain-text " $( curl ' https://api.chucknorris.io/jokes/0ECUwLDTTYSaeFCq6YMa5A ' | jq .value ) "输入参数
Chuck Norris can walk with the animals, talk with the animals; grunt and squeak and squawk with the animals... and the animals, without fail, always say 'yessir Mr. Norris'.
标准输出
????♂️??????????????️??️❗️??????=???????????????????=??.
cli/translator.mjs --stream --system-instruction " Scramble characters of words while only keeping the start and end letter " --no-prefix-number --no-line-matching --temperature 0 --plain-text " Chuck Norris can walk with the animals, talk with the animals; "标准输出
Cuhck Nroris can wakl wtih the aiamnls, talk wtih the aiamnls;
cli/translator.mjs --stream --system-instruction " Unscramble characters back to English " --no-prefix-number --no-line-matching --temperature 0 --plain-text " Cuhck Nroris can wakl wtih the aiamnls, talk wtih the aiamnls; "标准输出
Chuck Norris can walk with the animals, talk with the animals;
cli/translator.mjs --stream --temperature 0 --input test/data/test_cn.txt输入文件:test/data/test_cn.txt
你好。
拜拜!
标准输出
Hello.
Goodbye!
cli/translator.mjs --stream --temperature 0 --input test/data/test_ja_small.srt输入文件:test/data/test_ja_small.srt
1
00 : 00 : 00 , 000 --> 00 : 00 : 02 , 000
おはようございます。
2
00 : 00 : 02 , 000 --> 00 : 00 : 05 , 000
お元気ですか?
3
00 : 00 : 05 , 000 --> 00 : 00 : 07 , 000
はい、元気です。
4
00 : 00 : 08 , 000 --> 00 : 00 : 12 , 000
今日は天気がいいですね。
5
00 : 00 : 12 , 000 --> 00 : 00 : 16 , 000
はい、とてもいい天気です。输出文件:test/data/test_ja_small.srt.out_english.srt
1
00 : 00 : 00 , 000 --> 00 : 00 : 02 , 000
Good morning.
2
00 : 00 : 02 , 000 --> 00 : 00 : 05 , 000
How are you?
3
00 : 00 : 05 , 000 --> 00 : 00 : 07 , 000
Yes, I'm doing well.
4
00 : 00 : 08 , 000 --> 00 : 00 : 12 , 000
The weather is nice today, isn' t it?
5
00 : 00 : 12 , 000 --> 00 : 00 : 16 , 000
Yes, it's very nice weather.系统说明
令牌: 5
Translate Japanese to English
| 输入 | 迅速的 | 转换 | 输出 |
|---|---|---|---|
令牌: | 令牌: | 令牌: | 令牌: |
1
00 : 00 : 00 , 000 --> 00 : 00 : 02 , 000
おはようございます。
2
00 : 00 : 02 , 000 --> 00 : 00 : 05 , 000
お元気ですか?
3
00 : 00 : 05 , 000 --> 00 : 00 : 07 , 000
はい、元気です。
4
00 : 00 : 08 , 000 --> 00 : 00 : 12 , 000
今日は天気がいいですね。
5
00 : 00 : 12 , 000 --> 00 : 00 : 16 , 000
はい、とてもいい天気です。 | | | 1
00 : 00 : 00 , 000 --> 00 : 00 : 02 , 000
Good morning.
2
00 : 00 : 02 , 000 --> 00 : 00 : 05 , 000
How are you?
3
00 : 00 : 05 , 000 --> 00 : 00 : 07 , 000
Yes, I'm doing well.
4
00 : 00 : 08 , 000 --> 00 : 00 : 12 , 000
The weather is nice today, isn' t it?
5
00 : 00 : 12 , 000 --> 00 : 00 : 16 , 000
Yes, it's very nice weather. |
托多:更多分析
5个SRT线:
test/data/test_ja_small.srt
299362276 30个SRT线:
test/data/test_ja.srt
16256719[5, 10]的SRT剥离和线批处理,包括多达10个历史及时的上下文:1036