使用 AI 函数转换和扩充数据(预览版)

Important

此功能目前为预览版

Microsoft Fabric AI Functions 使所有业务专业人员(从开发人员到分析师)都可以使用生成 AI 来转换和丰富其企业数据。

AI 函数使用行业领先的大型语言模型(LLM)进行汇总、分类、文本生成等。 使用单行代码,可以:

无论是使用 pandas 还是 Spark,都可以将这些函数合并为数据科学和数据工程工作流的一部分。 没有详细的配置,也没有复杂的基础结构管理。 不需要任何特定的技术专业知识。

Prerequisites

Note

  • Fabric Runtime 1.3 及更高版本中支持 AI 函数。
  • 除非配置其他模型,否则 AI 函数默认为 gpt-4.1-mini。 详细了解 计费和消耗率
  • 尽管基础模型可以处理多种语言,但大多数 AI 函数都经过优化,可用于英语文本。

AI 函数入门

AI 函数可与 Pandas(Python 和 PySpark 运行时)以及 PySpark(PySpark 运行时)配合使用。 以下部分概述了必要的每一步安装和导入步骤,并附带相应的命令。

安装依赖项

  • Pandas (Python 运行时)
    • synapseml_internalsynapseml_core whl 文件需要安装(以下代码单元中提供了命令)
    • openai 需要安装包(命令请见以下代码单元)
  • Pandas (PySpark 运行时)
    • openai 需要安装软件包(命令在以下代码单元中提供)
  • PySpark (PySpark 运行时)
    • 无需安装
# The pandas AI functions package requires OpenAI version 1.99.5 or later
%pip install -q --force-reinstall openai==1.99.5 2>/dev/null

导入所需的库

以下代码单元导入 AI 函数库及其依赖项。

# Required imports
import synapse.ml.aifunc as aifunc
import pandas as pd

应用 AI 函数

以下每个函数都允许调用 Fabric 中的内置 AI 终结点,以使用单行代码转换和扩充数据。 可以使用 AI 函数分析 pandas 数据帧或 Spark 数据帧。

Tip

了解如何自定义 AI 函数 的配置

使用 ai.analyze_sentiment 检测情绪

ai.analyze_sentiment 函数调用 AI 来识别输入文本表示的情感状态是正、负、混合还是中性。 如果 AI 无法做出此决定,则输出将留空。 有关 ai.analyze_sentiment 与 pandas 使用的更详细说明,请参阅 本文。 有关 ai.analyze_sentiment PySpark,请参阅 本文

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

df = pd.DataFrame([
        "The cleaning spray permanently stained my beautiful kitchen counter. Never again!",
        "I used this sunscreen on my vacation to Florida, and I didn't get burned at all. Would recommend.",
        "I'm torn about this speaker system. The sound was high quality, though it didn't connect to my roommate's phone.",
        "The umbrella is OK, I guess."
    ], columns=["reviews"])

df["sentiment"] = df["reviews"].ai.analyze_sentiment()
display(df)

包含“评论”和“情绪”列的数据帧的屏幕截图。“sentiment”列包括“负”、“正”、“混合”和“中性”。

使用 ai.classify 对文本进行分类

ai.classify 函数调用 AI,根据所选自定义标签对输入文本进行分类。 有关 ai.classify 的 pandas 使用详细信息,请转到 本文。 有关 ai.classify PySpark,请参阅 本文

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

df = pd.DataFrame([
        "This duvet, lovingly hand-crafted from all-natural fabric, is perfect for a good night's sleep.",
        "Tired of friends judging your baking? With these handy-dandy measuring cups, you'll create culinary delights.",
        "Enjoy this *BRAND NEW CAR!* A compact SUV perfect for the professional commuter!"
    ], columns=["descriptions"])

df["category"] = df['descriptions'].ai.classify("kitchen", "bedroom", "garage", "other")
display(df)

数据帧的屏幕截图,其中包含“说明”和“类别”列。“category”列列出每个说明的类别名称。

使用 ai.extract 提取实体

ai.extract 函数调用 AI 来扫描输入文本并提取所选标签指定的特定类型信息(例如位置或名称)。 有关 ai.extract 与 pandas 更详细的说明,请参阅 本文。 有关 ai.extract PySpark,请参阅 本文

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

df = pd.DataFrame([
        "MJ Lee lives in Tucson, AZ, and works as a software engineer for Microsoft.",
        "Kris Turner, a nurse at NYU Langone, is a resident of Jersey City, New Jersey."
    ], columns=["descriptions"])

df_entities = df["descriptions"].ai.extract("name", "profession", "city")
display(df_entities)

显示包含从原始数据帧中提取的数据列“name”、“profession”和“city”的新数据帧的屏幕截图。

使用 ai.fix_grammar 修复语法

ai.fix_grammar 函数调用 AI 来更正输入文本的拼写、语法和标点符号。 关于如何使用 ai.fix_grammar 与 pandas 的详细说明,请参阅 本文。 有关 ai.fix_grammar PySpark,请参阅 本文

# 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)

显示包含“text”列和“更正”列的数据帧的屏幕截图,其中包含具有更正语法的文本列中的文本。

使用ai.generate_response回答自定义用户提示

ai.generate_response 函数调用 AI 以根据自己的说明生成自定义文本。 关于 ai.generate_response 和 pandas 使用的详细说明,请参阅 本文。 有关 ai.generate_response PySpark,请参阅 本文

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

df = pd.DataFrame([
        ("Scarves"),
        ("Snow pants"),
        ("Ski goggles")
    ], columns=["product"])

df["response"] = df.ai.generate_response("Write a short, punchy email subject line for a winter sale.")
display(df)

显示包含列“产品”和“响应”的数据帧的屏幕截图。“响应”列包含产品的简洁有力的主题行。

使用 ai.similarity 计算相似性

ai.similarity 函数将每个输入文本值与一个公共引用文本或另一列中的对应值(成对模式)进行比较。 输出相似性分数值是相对的,它们可以范围从 -1 (相反)到 1 (相同)。 分数 0 指示值的含义不相关。 有关如何使用ai.similarity与pandas的详细说明,请参阅这篇文章。 有关 ai.similarity PySpark,请参阅 本文

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

df = pd.DataFrame([ 
        ("Bill Gates", "Technology"), 
        ("Satya Nadella", "Healthcare"), 
        ("Joan of Arc", "Agriculture") 
    ], columns=["names", "industries"])
    
df["similarity"] = df["names"].ai.similarity(df["industries"])
display(df)

数据帧的屏幕截图,其中列为“名称”、“行业”和“相似性”。“相似性”列的名称和行业具有相似性分数。

使用 ai.summarize 汇总文本

ai.summarize 函数调用 AI 来生成输入文本的摘要(数据帧的单个列中的值或所有列中的行值)。 要了解有关如何使用 ai.summarize 与 pandas 的详细说明,请参阅 本文。 有关 ai.summarize PySpark,请参阅 本文

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

df= pd.DataFrame([
        ("Microsoft Teams", "2017",
        """
        The ultimate messaging app for your organization—a workspace for real-time 
        collaboration and communication, meetings, file and app sharing, and even the 
        occasional emoji! All in one place, all in the open, all accessible to everyone.
        """),
        ("Microsoft Fabric", "2023",
        """
        An enterprise-ready, end-to-end analytics platform that unifies data movement, 
        data processing, ingestion, transformation, and report building into a seamless, 
        user-friendly SaaS experience. Transform raw data into actionable insights.
        """)
    ], columns=["product", "release_year", "description"])

df["summaries"] = df["description"].ai.summarize()
display(df)

显示数据帧的屏幕截图。“摘要”列仅在相应的行中对“description”列进行了摘要。

使用 ai.translate 翻译文本

ai.translate 函数调用 AI 将输入文本翻译为所选的新语言。 有关如何使用 pandas 中的 ai.translate 的更详细指引,请参阅 本文。 有关 ai.translate PySpark,请参阅 本文

# 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)

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