Edit

Share via


Fix grammar with the ai.fix_grammar function

The ai.fix_grammar function uses generative AI to correct the spelling, grammar, and punctuation of input text. It uses only a single line of code.

AI functions improve data engineering by using the power of large language models in Microsoft Fabric. To learn more, see this overview article.

Important

This feature is in preview, for use in Fabric Runtime 1.3 and later.

  • Review the prerequisites in this overview article, including the library installations that are temporarily required to use AI functions.
  • By default, the gpt-4o-mini model currently powers AI functions. Learn more about billing and consumption rates.
  • 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 the built-in AI endpoint in Fabric.

Use ai.fix_grammar with pandas

The ai.fix_grammar function extends the pandas Series class. To correct the spelling, grammar, and punctuation of each row of input, call the function on a pandas DataFrame text column.

The function returns a pandas Series that contains corrected text values, which can be stored in a new DataFrame column.

Syntax

df["corrections"] = df["text"].ai.fix_grammar()

Parameters

None

Returns

The function returns a pandas Series that contains corrected text for each input text 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 = pd.DataFrame([
        "There are an error here.",
        "She and me go weigh back. We used to hang out every weeks.",
        "The big picture are right, but you're details is all wrong."
    ], columns=["text"])

df["corrections"] = df["text"].ai.fix_grammar()
display(df)

Use ai.fix_grammar with PySpark

The ai.fix_grammar function is also available for Spark DataFrames. You must specify the name of an existing input column as a parameter.

The function returns a new DataFrame that includes corrected text for each input text row, stored in an output column.

Syntax

df.ai.fix_grammar(input_col="text", output_col="corrections")

Parameters

Name Description
input_col
Required
A string that contains the name of an existing column with input text values to correct for spelling, grammar, and punctuation.
output_col
Optional
A string that contains the name of a new column to store corrected text for each row of input text. If you don't set this parameter, a default name generates for the output column.
error_col
Optional
A string that contains the name of a new column to store any OpenAI errors that result from processing each row of input text. If you don't set this parameter, a default name generates for the error column. If there are no errors for a row of input, the value in this column is null.

Returns

The function returns a Spark DataFrame that includes a new column that contains corrected text for each row of text in the input column. 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([
        ("There are an error here.",),
        ("She and me go weigh back. We used to hang out every weeks.",),
        ("The big picture are right, but you're details is all wrong.",)
    ], ["text"])

results = df.ai.fix_grammar(input_col="text", output_col="corrections")
display(results)