翻译 LLM (TLLM) 模型

Google 最新的先进翻译模型 TLLM 是一种大语言模型 (LLM),可提供最高质量的翻译。 与其他翻译模型相比,在处理困难的工作负载时,该模型可获得明显更高的 MetricXCOMET 分数。它支持自定义,以及更轻量级的自适应翻译

其模型 ID 为 general/translation-llm

如需访问 TLLM 模型,您必须拥有一个启用了 Cloud Translation 基本版 API 或 Cloud Translation 高级版 API 的 Google Cloud 项目,并且拥有能进行身份验证调用的凭据。如需使用 Python 或其他编程语言访问该模型,请安装客户端库(Cloud Translation - Advanced API 功能需要使用 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"
    }
  ]
}

文本翻译 REST 示例(基本版/V2)

您可以使用 Cloud Translation 基本版 (V2) REST API 调用标准 Translation LLM 模型。您必须使用 API 密钥进行身份验证。

curl -X POST \
     -H "Content-Type: application/json; charset=utf-8" \
     -d '{
       "q": ["The old lighthouse stood on the edge of the cliff."],
       "target": "es",
       "model": "projects/PROJECT_ID/locations/REGION_NAME/models/general/translation-llm"
     }' \
     "https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY"

文本翻译 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"