翻譯專用大型語言模型 (TLLM)
Google 最新的先進翻譯模型 TLLM 是經過 LoRA 微調的 LLM,可提供最高品質的翻譯。在困難的工作負載上,這款模型獲得的 MetricX 和 COMET 分數,都比其他翻譯模型高出許多。支援自訂功能,以及更輕量的自適應翻譯。
型號 ID 為 general/translation-llm。
如要存取 TLLM 模型,您需要已啟用 Cloud Translation - Advanced API 的Google Cloud 專案,以及用於發出已驗證呼叫的憑證。如要使用 Python 或其他程式設計語言存取模型,請安裝適當的 v3 用戶端程式庫。
在本文範例中,PROJECT_ID 代表專案 ID,REGION_NAME 代表您要執行翻譯作業的 Google Cloud 區域技術區域名稱 (例如 us-central1)。如有需要,請使用 ISO-639 代碼識別原文和譯文語言。
文字翻譯 REST 範例
您可以使用 REST API,在 TLLM 模型上呼叫 TranslateText。您可以將要求欄位放入名為 request.json 的 JSON 檔案中:
{
"contents": ["This is text that I would like to have translated.",
"It can include up to 1024 strings."],
"mimeType": "text/plain",
"sourceLanguageCode": "en"
"targetLanguageCode": "it",
"model": "projects/PROJECT_ID/locations/REGION_NAME/models/general/translation-llm"
}
接著,您可以使用 curl 指令提交要求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://translation.googleapis.com/v3/projects/PROJECT_ID:translateText"
回覆內容為 JSON 文件,如下所示:
{
"translations": [
{
"translatedText": "Este es el texto que me gustaría traducir.",
"model": "projects/PROJECT_ID/locations/REGION_NAME/models/general/translation-llm"
},
{
"translatedText": "Puede incluir hasta 1024 cadenas.",
"model": "projects/PROJECT_ID/locations/REGION_NAME/models/general/translation-llm"
}
]
}
文字翻譯 Python 範例
以下是使用 TLLM 模型呼叫 TranslateText 的 Python 程式碼範例:
from google.cloud import translate_v3
def translate():
response = translate_v3.TranslationServiceClient().translate_text(
contents=["Life is short.",
"Art is long."],
target_language_code='fr',
source_language_code='en',
parent=f"projects/PROJECT_ID/locations/REGION_NAME",
model=f"projects/PROJECT_ID/locations/REGION_NAME/models/general/translation-llm"
)
print(response)
return response
translate()
回覆會以 JSON 物件的形式呈現:
translations {
translated_text: "La vie est courte."
model: "projects/261347268520/locations/us-central1/models/general/translation-llm"
}
translations {
translated_text: "L'art est long."
model: "projects/261347268520/locations/us-central1/models/general/translation-llm"
}
translations {
translated_text: "La vie est courte."
model: "projects/261347268520/locations/us-central1/models/general/translation-llm"
}
translations {
translated_text: "L'art est long."
model: "projects/261347268520/locations/us-central1/models/general/translation-llm"