Microsoft Fabric中的AI功能只需一行代码df.ai.<function_name>(),即可对大型熊猫或PySpark DataFrame进行一行、由LLM驱动的转换。 它们通过进行数百个异步推理调用,同时保持行级内容隔离,实现高并发运行。 这种方法使你能够大规模丰富、分类、总结和提取非结构化文本和文档中的数据。
使用此表跳转到本概述或详细的 pandas 和 PySpark 文档中的示例。
| 功能 | 说明 | 详细文档 |
|---|---|---|
ai.analyze_sentiment |
检测输入文本的情感状态。 示例。 | pandas、 PySpark |
ai.classify |
根据标签对输入文本进行分类。 示例。 | pandas、 PySpark |
ai.embed |
为输入文本生成矢量嵌入。 示例。 | pandas、 PySpark |
ai.extract |
提取位置、名称或自定义实体等字段。 示例。 | pandas、 PySpark |
ai.fix_grammar |
更正拼写、语法和标点符号。 示例。 | pandas、 PySpark |
ai.generate_response |
根据说明生成响应。 示例。 | pandas、 PySpark |
ai.similarity |
将文本含义与一个值或另一列进行比较。 示例。 | pandas、 PySpark |
ai.summarize |
汇总文本、文件或行数据。 示例。 | pandas、 PySpark |
ai.translate |
将输入文本翻译为其他语言。 示例。 | pandas、 PySpark |
可以在使用 pandas 或 PySpark 的笔记本中、SQL 查询中以及 Dataflow Gen2 中使用 AI 函数。 Fabric处理内置模型的终结点设置。
在 Fabric 各项体验中使用 AI 函数
AI 函数在多个Fabric体验中可用:
- 笔记本:使用 pandas 和 PySpark API 在数据科学和数据工程工作流中丰富数据帧。
-
仓库和 SQL 分析终结点:使用仓库或 SQL 分析终结点中的 AI 函数,可在 T-SQL 查询中直接调用 SQL 风格函数,例如
ai_summarize、ai_classify和ai_generate_response。 - Dataflow Gen2:在数据流 Gen2 中使用 Fabric AI 提示符在Power Query中添加 AI 生成的列。
- T-SQL 代码——通过使用 T-SQL 脚本或笔记本、过程、函数或在 Fabric Data Warehouse、Lakehouse 的 SQL 端点或镜像数据库中执行 T-SQL 代码的外部应用程序调用 AI 函数。 这种方法允许分析师和开发者将AI能力直接嵌入基于SQL的数据管道和查询中,无需切换到笔记本环境即可访问。
使用多模态 AI 函数
多模式 AI 函数除了处理文本值外,还处理图像、PDF 和文本文件。 使用它们汇总 PDF、分类图像、提取文档字段或生成基于文件内容的响应。
支持的文件类型包括 JPG/JPEG、PNG、静态 GIF、WebP、PDF、MD、TXT、CSV、TSV、JSON、XML、PY 和其他文本文件。 在 pandas 中设置 column_type="path",或者在 PySpark 中设置 input_col_type 或 col_types。 有关示例,请参阅 将多模式输入与 AI 函数配合使用。
Prerequisites
- 若要将 AI Functions 与 Fabric 中的内置 AI 终结点配合使用,管理员需要启用 Copilot 的租户切换,以及由 Azure OpenAI 提供支持的其他功能。
- 可能需要为跨地理位置处理启用租户设置,具体取决于你的位置。 详细了解 Azure OpenAI 服务的可用区域。
- 需要付费的 Fabric 容量(F2 或更高版本,或任何 P 版本)。
Note
- Fabric Runtime 1.3 及更高版本中支持 AI 函数。
- AI 函数[pandas、PySpark、Dataflow Gen2 和 Datawarehouse SQL] 默认为
gpt-5-mini,设置为reasoning_effortlow。 该模型具有 40 万 token 的上下文窗口,最大输出为 12.8 万 token。 有关模型限制和速率,请参阅 语言模型表。 - AI Functions 不会记录或存储用户提示、输入数据或输出。
模型和供应商
默认情况下,AI 函数使用内置Fabric终结点。 你还可以将 pandas 和 PySpark AI Functions 配置为使用任何支持 chat_completions 或 responses API 的 LLM,包括:
- Azure OpenAI 模型。
- Microsoft Foundry 模型,例如 Qwen、Kimi、Grok、LLaMA、Mistral 等。
有关配置选项,请参阅 使用 pandas 自定义 AI 函数 , 以及使用 PySpark 自定义 AI 函数。
设置 AI 函数
AI Functions 分别支持在 Python 和 PySpark 运行时中使用 pandas,以及在 PySpark 运行时中使用 PySpark。 仅安装运行时所需的包。
安装依赖项
| Runtime | 依赖关系 |
|---|---|
| pandas (Python运行时) | 安装 synapseml_internal 和 synapseml_core wheel 文件。 仅在需要 SDK 原生客户端行为或 Pydantic 响应格式示例时,才安装 1.99.5 或更高版本的 openai。 |
| pandas(Fabric 运行时 2.0,支持 PySpark 4.1 和 Python 3.13) | 临时安装 nest_asyncio。 仅在需要 SDK 原生客户端行为或 Pydantic 响应格式示例时,才安装 1.99.5 或更高版本的 openai。 |
| pandas(其他 PySpark 运行环境) | 大多数用法都不需要安装。 仅在需要 SDK 原生客户端行为或 Pydantic 响应格式示例时,才安装 1.99.5 或更高版本的 openai。 |
| PySpark(PySpark 运行时) | 无需安装。 |
| T-SQL(Data Warehouse 和 SQL Analytics 端点运行时) | 无需安装。 |
Note
- 安装
nest_asyncio是用于 Fabric Runtime 2.0 中 pandas AI 函数的临时兼容性补丁。 这一要求将在未来的更新中被取消。 - PySpark AI 函数在 Fabric Spark 运行时不需要额外的安装步骤。
基于 Python 3.13 和 PySpark 4.1 的 Fabric 运行时 2.0 原生不包含该nest_asyncio包。 要在此运行时使用 pandas AI 功能,请作为临时兼容性补丁安装 nest_asyncio 。 未来更新中,这一要求将被取消,因为 pandas AI Functions 可以使用预装在 Fabric Runtime 2.0 中的更新nest_asyncio2包。
# Temporary compatibility patch for Fabric Runtime 2.0.
%pip install -q nest_asyncio 2>/dev/null
# Optional: install openai version 1.99.5 or later for SDK-native client behavior.
%pip install -q openai 2>/dev/null
# Optional: install openai version 1.99.5 or later for SDK-native client behavior.
%pip install -q openai 2>/dev/null
# Install latest versions of AI Functions library whl
!wget -q https://aka.ms/fabric-aifunctions-whl -O synapseml_internal-latest-py3-none-any.whl
!wget -q https://aka.ms/fabric-synapseml-core-whl -O synapseml_core-latest-py3-none-any.whl
# openai version 1.99.5 or later is included for SDK-native client behavior.
# To keep the environment lightweight, remove "openai" from the install command.
%pip install -q openai synapseml_internal-latest-py3-none-any.whl synapseml_core-latest-py3-none-any.whl
导入所需的库
为你的运行时环境导入 AI 函数库。
对文件和架构使用辅助函数
AI 功能包括用于多模态工作流的辅助工具:
-
aifunc.load:将文件夹中的文件引入到结构化表中。 您可以提供提示词或模式。 -
aifunc.list_file_paths:枚举文件夹中的文件 URL 和路径,以用作任何 AI 函数的输入。 -
ai.infer_schema:根据文件内容推断供ai.extract使用的提取模式。
有关示例,请参阅 将多模式输入与 AI 函数配合使用。
应用 AI 函数
以下示例演示 pandas 和 PySpark 的核心 AI 函数。 PySpark AI Functions 以分布式 Spark 转换的形式在 Fabric Spark 群集上运行。
Note
大多数 AI 函数支持在 pandas 中使用带有 column_type="path" 的文件路径,或在 PySpark 中使用带有 input_col_type/col_types="path" 的文件路径。 有关示例,请参阅 将多模式输入与 AI 函数配合使用。
Tip
默认的 Python 模型是 gpt-5-mini,并带有 reasoning_effort="low"。 若要更改模型或优化设置,请参阅 pandas 配置 或 PySpark 配置。
ai.analyze_sentiment:检测情绪
该 ai.analyze_sentiment 函数将每个输入标记为正、负、混合或中性。 还可以提供自定义标签。
# This code uses AI. Always review output for mistakes.
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)
ai.classify:对文本进行分类
该 ai.classify 函数使用提供的标签对输入文本进行分类。
# This code uses AI. Always review output for mistakes.
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)
ai.embed:生成矢量嵌入
该 ai.embed 函数将文本转换为捕获语义含义的数字向量。 将嵌入内容用于相似性搜索、检索和机器学习工作流。
# This code uses AI. Always review output for mistakes.
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["embed"] = df["descriptions"].ai.embed()
display(df)
ai.extract:提取实体
该 ai.extract 函数从输入文本中提取名称、位置或自定义实体等字段。
结构化标签
需要类型化提取时使用 ExtractLabel 。 它支持 JSON 架构构造,例如类型化字段、枚举、数组、嵌套对象、可为 null 值、必需属性和 additionalProperties=false。 有关示例,请参阅 pandas 或 PySpark。
# This code uses AI. Always review output for mistakes.
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)
ai.fix_grammar:修复语法
该 ai.fix_grammar 函数更正拼写、语法和标点符号。
# This code uses AI. Always review output for mistakes.
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)
ai.generate_response:应用自定义用户提示
ai.generate_response 函数根据提示词和行数据生成自定义文本。
可选参数
需要结构化输出(包括 JSON 对象、JSON 架构或 Pydantic 模型)时使用 response_format 。 有关示例,请参阅 pandas 或 PySpark。
# This code uses AI. Always review output for mistakes.
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(表示含义完全相同)。
# This code uses AI. Always review output for mistakes.
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 函数汇总文本、文件内容、单个列或每行中的所有列。
通过指令自定义摘要
用于 instructions 控制音调、长度、受众或焦点。 有关示例,请参阅 pandas 或 PySpark。
# This code uses AI. Always review output for mistakes.
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)
ai.translate:翻译文本
该 ai.translate 函数将文本翻译为其他语言。
# This code uses AI. Always review output for mistakes.
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)
串联 PySpark AI 函数
PySpark AI Functions 返回将 df.ai 访问器绑定到结果架构的 DataFrame。 无需生成中间 DataFrame 即可进行链式转换。
# This code uses AI. Always review output for mistakes.
output = (
df
.ai.summarize(input_col="review_text", output_col="summary")
.ai.classify(
labels=["service", "cleanliness", "location", "other"],
input_col="summary",
output_col="category",
)
)
display(output)
使用 ai.stats 查看使用情况统计信息
对 AI 生成的 Series 或 DataFrame 使用 ai.stats,以查看使用情况和执行指标。
ai.stats 返回包含统计信息的数据帧,例如:
-
num_successful:AI 函数成功处理的行数。 -
num_exceptions:执行过程中遇到异常的行数。 这些行被表示为aifunc.ExceptionResult的实例。 -
num_unevaluated:未处理的行数,因为先前的异常使得无法继续评估。 这些行被表示为aifunc.NotEvaluatedResult的实例。 -
num_harmful:Azure OpenAI 内容筛选器阻止的行数。 这些行被表示为aifunc.FilterResult的实例。 -
cached_tokens:缓存的输入令牌总数。 -
input_tokens:用于 AI 函数调用的输入令牌总数。 -
output_tokens:模型生成的输出令牌总数。 -
reasoning_tokens:推理模型使用的推理令牌总数。 -
model:用于 AI 函数调用的模型部署名称。
输出结果可能如下表所示:
| 成功数量 | num_exceptions | 未评估数量 | num_harmful | 缓存的令牌 | input_tokens | output_tokens | 推理标记 | 客户端类型 | 输入类型 | 模型 |
|---|---|---|---|---|---|---|---|---|---|---|
| 2 | 0 | 0 | 0 | 0 | 555 | 4 | 0 | fabric_llm_endpoint | {"text": 2} |
gpt-5-mini |
Tip
使用 ai.stats 来跟踪使用情况、错误模式和令牌消耗。
达到容量限制的行会显示为 aifunc.CapacityExceededResult 的实例。 在 pandas 工作流中,使用 aifunc.split_results 将成功输出结果与非结果分开,以便检查受容量限制的行,并在容量可用或限制问题得到解决后重试这些行。
成本透明度
pandas AI Functions 可以在执行过程中通过 progress_bar_mode="stats" 显示令牌计数和容量单位估计值。 对于 PySpark,请在结果 DataFrame 上使用 df.ai.stats。
Fabric 容量指标应用将模型调用消耗量记为 AI Functions 操作。 有关详细信息,请参阅 AI 函数的计费。
评估和加速
使用 AI Functions 入门笔记本 获取涵盖端到端流程的 pandas 和 PySpark 示例。 使用 AI Functions Eval Notebook 评估生产前的输出质量。
相关内容
- 将多模式输入与 AI Functions 配合使用。
- 使用 pandas 或 PySpark 自定义 AI 函数。
- 了解AI 函数的计费。
- 尝试 使用 AI Functions Starter Notebook 或 AI Functions Eval Notebook。
- 在 仓库或 SQL 分析终结点中使用 AI 函数。
- 在数据流 Gen2 中使用 Fabric AI 提示符。