将 ai.translate 与 PySpark 配合使用

ai.translate 函数使用生成式 AI 将输入文本翻译为新语言(所选语言),只需一行代码。

注释

  • 本文介绍如何将 ai.translate 与 PySpark 配合使用。 若要将 ai.translate 与 pandas 配合使用,请参阅 本文
  • 请参阅 本概述文章中的其他 AI 函数。
  • 了解如何自定义 AI 函数的配置

概述

ai.translate 函数可用于 Spark 数据帧。 必须将现有输入列名称指定为参数,以及目标语言。

该函数返回一个新的 DataFrame,其中包含每个输入文本行的翻译,存储在输出列中。

Syntax

df.ai.translate(to_lang="spanish", input_col="text", output_col="translations")

参数

Name Description
to_lang
必选
用于表示文本翻译目标语言的字符串
input_col
必选
包含现有列的名称的 字符串 ,其中包含要翻译的输入文本值。
output_col
可选
一个 字符串,其中包含一个新列的名称,该列用于存储每个输入文本行的翻译。 如果未设置此参数,则为输出列生成默认名称。
error_col
可选
一个 字符串,该字符串包含新列的名称,该列存储因处理每个输入文本行而导致的任何 OpenAI 错误。 如果未设置此参数,则为错误列生成默认名称。 如果输入行没有错误,则此列中的值 null

退货

该函数返回一个 Spark 数据帧 ,其中包含一个新列,其中包含输入列行中文本的翻译。 如果输入的文本是 null,则结果为 null

Example

# This code uses AI. Always review output for mistakes. 
# Read terms: https://azure.microsoft.com/support/legal/preview-supplemental-terms/.

df = spark.createDataFrame([
        ("Hello! How are you doing today?",),
        ("Tell me what you'd like to know, and I'll do my best to help.",),
        ("The only thing we have to fear is fear itself.",),
    ], ["text"])

translations = df.ai.translate(to_lang="spanish", input_col="text", output_col="translations")
display(translations)

此示例代码单元提供以下输出:

数据帧的屏幕截图,其中列为“text”和“translations”。“翻译”列包含翻译为西班牙语的文本。