Microsoft Fabric 中的 AI 功能只需一行程式碼df.ai.<function_name>(),即可對大型 pandas 或 PySpark DataFrame 套用由 LLM 驅動的單行轉換。 它們透過進行數百次非同步推論呼叫,同時保持列層級的內容隔離,實現高並發性。 這種方法能讓你能大規模豐富、分類、摘要並從非結構化文本和文件中提取資料。
請使用此表格跳轉至本概述中的範例,或是詳細的 pandas 與 PySpark 文件。
| 功能 | Description | 詳細文件 |
|---|---|---|
ai.analyze_sentiment |
偵測輸入文字的情緒狀態。 範例。 | 熊貓, PySpark |
ai.classify |
根據你的標籤分類輸入文字。 範例。 | 熊貓, PySpark |
ai.embed |
產生輸入文字的向量嵌入。 範例。 | 熊貓, PySpark |
ai.extract |
擷取地點、姓名或自訂實體等欄位。 範例。 | 熊貓, PySpark |
ai.fix_grammar |
正確的拼寫、文法和標點符號。 範例。 | 熊貓, PySpark |
ai.generate_response |
根據你的指示產生回應。 範例。 | 熊貓, PySpark |
ai.similarity |
將文字內容與某個值或另一個欄位進行比較。 範例。 | 熊貓, PySpark |
ai.summarize |
摘要文字、檔案或列資料。 範例。 | 熊貓, PySpark |
ai.translate |
將輸入文字翻譯成另一種語言。 範例。 | 熊貓, 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:使用 Dataflow Gen2 中的 Fabric AI Prompt,在 Power Query 中新增 AI 產生的資料行。
- T-SQL 程式碼 - 透過使用 T-SQL 腳本或筆記本、程序、函式或執行 T-SQL 程式碼的外部應用程式來呼叫 AI 函式,這些應用程式正在執行 T-SQL 程式碼,Fabric Data Warehouse、Lakehouse 的 SQL 端點或鏡像資料庫。 此方法讓分析師與開發者能將 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
- 若要在 Fabric 中使用搭配內建 AI 端點的 AI 函數,您的系統管理員必須啟用 Copilot 和其他由 Azure OpenAI 提供支援之功能的租用戶切換設定。
- 視您所在的位置而定,您可能需要啟用跨地理位置處理的租用戶設定。 深入瞭解 Azure OpenAI 服務的可用區域。
- 您需要付費的 Fabric 容量(F2 或更高版本,或任何 P 系列版本)。
Note
- AI 功能在 Fabric Runtime 1.3 及更新版本中受到支援。
- AI 函式 [pandas、PySpark、Dataflow Gen2 和 Datawarehouse SQL] 預設為
gpt-5-mini,並reasoning_effort設為low。 此模型擁有 400,000 代幣的上下文視窗,最大輸出為 128,000 代幣。 關於模型限制與速率,請參閱 語言模型表。 - AI 功能不會記錄或儲存使用者提示、輸入資料或輸出。
模式與供應商
AI 功能預設使用內建的 Fabric 端點。 你也可以設定 pandas 和 PySpark AI 函式,以使用任何支援 chat_completions 或 responses API 的 LLM,包括:
- Azure OpenAI 模型
- Microsoft Foundry 的模型如 Qwen、Kimi、Grok、LLaMA、Mistral 等。
關於設定選項,請參閱 使用 pandas 自訂 AI 功能 及 使用 PySpark 自訂 AI 功能。
建立 AI 功能
AI 函式支援 Python 與 PySpark 執行時中的 pandas,以及 PySpark 執行時中的 PySpark。 只安裝執行時需要的套件。
安裝依賴項
| Runtime | 依賴 |
|---|---|
| pandas(Python 執行環境) | 安裝 synapseml_internal 和 synapseml_core 方向盤檔案。 只有在需要 SDK 原生客戶端行為或 Pydantic 回應格式範例時,才安裝 openai 版本 1.99.5 或更新版本。 |
| pandas(Fabric Runtime 2.0,搭載 PySpark 4.1 與 Python 3.13) | 暫時安裝 nest_asyncio。 只有在需要 SDK 原生客戶端行為或 Pydantic 回應格式範例時,才安裝 openai 版本 1.99.5 或更新版本。 |
| pandas(其他 PySpark 執行階段) | 大多數使用不需要安裝。 只有在需要 SDK 原生客戶端行為或 Pydantic 回應格式範例時,才安裝 openai 版本 1.99.5 或更新版本。 |
| PySpark(PySpark 執行階段) | 不需要安裝。 |
| T-SQL(Data Warehouse 與 SQL Analytics 端點執行階段) | 不需要安裝。 |
Note
- 安裝
nest_asyncio是 Fabric Runtime 2.0 中 pandas AI 函數的暫時性相容性修補。 此要求將於未來更新中移除。 - PySpark AI 函式在 Fabric Spark 執行時不需要額外的安裝步驟。
Fabric Runtime 2.0 以 Python 3.13 和 PySpark 4.1 為基礎,但原生不包含 nest_asyncio 套件。 若要在此執行環境中使用 pandas AI Functions,請安裝 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 Functions 函式庫。
使用輔助函式來管理檔案和結構
AI 功能包含多模態工作流程的輔助工具:
-
aifunc.load:將資料夾中的檔案匯入結構化資料表。 你可以提供提示詞或架構。 -
aifunc.list_file_paths: 從資料夾中枚舉檔案 URL 與路徑,作為任何 AI 函式的輸入。 -
ai.infer_schema:從檔案內容推導出一個用於ai.extract的擷取結構。
例如,請參見 「在 AI 函數中使用多模態輸入」。
應用 AI 功能
以下範例展示了 pandas 與 PySpark 的核心 AI 功能。 PySpark AI 函式以分散式 Spark 轉換方式在 Fabric Spark 叢集間執行。
Note
大多數 AI 功能支援在 pandas 中使用含有 column_type="path" 的檔案路徑,或在 PySpark 中使用 input_col_type/col_types="path"。 例如,請參見 「在 AI 函數中使用多模態輸入」。
Tip
預設的 Python 模型為 gpt-5-minireasoning_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 結構,如型別欄位、枚舉、陣列、巢狀物件、可空值、必要屬性等 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.相似度:計算相似度
該 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 函式會傳回 DataFrame,並讓 df.ai 存取子維持綁定於結果結構描述。 鏈式轉換而不產生中間資料框架。
# 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 回傳一個具有以下統計數據的 DataFrame:
-
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_successful | num_exceptions | num_unevaluated | num_harmful | cached_tokens | input_tokens | output_tokens | reasoning_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 功能可在執行 progress_bar_mode="stats"時顯示代幣數量及容量單位估計。 對於 PySpark,請在結果 DataFrame 上使用 df.ai.stats。
Fabric 容量指標應用程式會將模型呼叫的耗用量顯示為 AI Functions 作業。 詳情請參見 AI 功能計費。
評估並加速
使用 AI Functions 入門筆記本 取得端對端的 pandas 和 PySpark 範例。 使用 AI 功能評估筆記本 ,在生產前評估輸出品質。
相關內容
- 使用多模態輸入搭配 AI 功能。
- 使用 PANDAS 或 PySpark 自訂 AI 功能。
- 了解 AI 功能的計費。
- 試試 AI Functions 入門筆記本 或 AI 功能評估筆記本。
- 在 倉庫或 SQL 分析端點中使用 AI 功能。
- 在 Dataflow Gen2 中使用 Fabric AI 提示詞。