Chatgpt還展示了其作為強大的翻譯人員的能力,不僅能夠處理通用語言,還可以處理表情符號和詞語等非常規的寫作形式。但是,即使在指示遵循精確的指令並將模型temperature參數設置為0時,它可能並不總是會產生確定性的輸出並遵守線對線相關性,並可能破壞字幕的時間。
該實用程序使用OpenAI Chatgpt API翻譯文本,特別關注基於線路的翻譯,尤其是對於SRT字幕。 Translator通過刪除SRT開銷,將文本分組為批處理,從而優化令牌用法,從而在沒有過多的令牌消耗的情況下進行任意長度翻譯,同時確保線路輸入和輸出之間的一對一匹配。
--experimental-structured-mode 。--experimental-use-full-context 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