翻译 LLM (TLLM) 模型

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

其模型 ID 为 general/translation-llm

如需访问 TLLM 模型,您需要有一个Google Cloud 项目,其中启用了 Cloud Translation 高级版 API,并具有能进行身份验证调用的凭据。如需使用 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"