Edit

Share via


Translate text with the ai.translate function

The ai.translate function uses Generative AI to translate input text to a new language of your choice—all with a single line of code.

AI functions turbocharge data engineering by putting the power of Fabric's built-in large languages models into your hands. To learn more, visit this overview article.

Important

This feature is in preview, for use in the Fabric 1.3 runtime and higher.

  • Review the prerequisites in this overview article, including the library installations that are temporarily required to use AI functions.
  • By default, AI functions are currently powered by the gpt-3.5-turbo (0125) model. To learn more about billing and consumption rates, visit this article.
  • Although the underlying model can handle several languages, most of the AI functions are optimized for use on English-language texts.
  • During the initial rollout of AI functions, users are temporarily limited to 1,000 requests per minute with Fabric's built-in AI endpoint.

Tip

The ai.translate function was tested with 10 languages: Czech, English, Finnish, French, German, Greek, Italian, Polish, Spanish, and Swedish. Your results with other languages may vary.

Use ai.translate with pandas

The ai.translate function extends the pandas Series class. Call the function on a pandas DataFrame text column to translate each input row into a target language of your choosing.

The function returns a pandas Series that contains translations, which you can store in a new DataFrame column.

Syntax

df["translations"] = df["text"].ai.translate("target_language")

Parameters

Name Description
to_lang
Required
A string representing the target language for text translations.

Returns

A pandas Series that contains translations for each row of input text. If the input text is null, the result is null.

Example

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

df = pd.DataFrame([
        "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."
    ], columns=["text"])

df["translations"] = df["text"].ai.translate("spanish")
display(df)

Use ai.translate with PySpark

The ai.translate function is also available for Spark DataFrames. You must specify an existing input column name as a parameter, along with a target language.

The function returns a new DataFrame, with translations for each input text row stored in an output column.

Syntax

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

Parameters

Name Description
to_lang
Required
A string that represents the target language for text translations.
input_col
Required
A string that contains the name of an existing column with input text values to be translated.
output_col
Optional
A string that contains the name of a new column that stores translations for each input text row. If this parameter isn't set, a default name is generated for the output column.
error_col
Optional
A string that contains the name of a new column that stores any OpenAI errors that result from processing each input text row. If this parameter isn't set, a default name is generated for the error column. If an input row has no errors, the value in this column is null.

Returns

A Spark DataFrame with a new column that contains translations for the text in the input column row. If the input text is null, the result is 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)