使用 AI 函式執行批次 LLM 推斷
重要
這項功能處於公開預覽狀態。
本文說明如何使用 AI Functions來執行批次推斷。
您可以使用 工作特定的 AI 函式 或一般用途函式,ai_query
執行批次推斷。 本文中的範例著重於 ai_query
的彈性,以及如何在批次推斷管線和工作流程中使用。
使用 ai_query
進行批次推斷有兩種主要方式:
- 使用
ai_query
和 由 Databricks 承載的基礎模型進行批次推斷:當您使用此方法時,Databricks 會設定模型服務端點,該端點能根據工作負載自動擴展。 查看哪些預先布建的 LLM 得到支援。 - 使用
ai_query
和 您自行配置的模型伺服端點進行批次推斷:對於那些使用裝載於 Databricks 外部的基礎模型、經過微調的基礎模型或傳統 ML 模型的批次推斷工作流程,此方法是必須的。 部署之後,端點可以直接與ai_query
搭配使用。 請參閱 批次推斷使用自訂模型或微調的基礎模型。
需求
- 位於支援 Foundation Model API 的地區內的工作區。
- 查詢 Unity 目錄中 Delta 資料表的許可權,以使用所需的資料。
- 將資料表屬性中的
pipelines.channel
設定為 'preview' 以使用ai_query()
。 如需範例查詢,請參閱 需求。
批次 LLM 推論使用 ai_query
和由 Databricks 裝載的基礎模型
當您使用 Databricks 裝載和預先布建的基礎模型進行批次推斷時,Databricks 會代表您設定布建的輸送量端點,以根據工作負載自動調整。
若要使用此方法進行批次推斷,請在您的要求中指定下列專案:
- 您想要在
ai_query
中使用的預配置 LLM。 從支援的預先配置的 LLM中選取。 - Unity Catalog 輸入資料表和輸出資料表。
- 模型提示和任何模型參數。
SELECT text, ai_query(
"databricks-meta-llama-3-1-8b-instruct",
"Summarize the given text comprehensively, covering key points and main ideas concisely while retaining relevant details and examples. Ensure clarity and accuracy without unnecessary repetition or omissions: " || text
) AS summary
FROM uc_catalog.schema.table;
部署批次推理流程
本節說明如何將 AI Functions 整合到其他 Databricks 數據和 AI 產品,以建置完整的批次推斷管線。 這些管線可以執行端對端工作流程,包括擷取、前置處理、推斷和後置處理。 管線可以在 SQL 或 Python 中撰寫,並部署為:
- 使用 Databricks 工作流程進行排程
- 使用結構化串流串流推斷工作流程
使用 Databricks 工作流程將批次推斷作業自動化
排程批次推論作業,並將 AI 管線自動化。
SQL
SELECT
*,
ai_query('databricks-meta-llama-3-3-70b-instruct', request => concat("You are an opinion mining service. Given a piece of text, output an array of json results that extracts key user opinions, a classification, and a Positive, Negative, Neutral, or Mixed sentiment about that subject.
AVAILABLE CLASSIFICATIONS
Quality, Service, Design, Safety, Efficiency, Usability, Price
Examples below:
DOCUMENT
I got soup. It really did take only 20 minutes to make some pretty good soup. The noises it makes when it's blending are somewhat terrifying, but it gives a little beep to warn you before it does that. It made three or four large servings of soup. It's a single layer of steel, so the outside gets pretty hot. It can be hard to unplug the lid without knocking the blender against the side, which is not a nice sound. The soup was good and the recipes it comes with look delicious, but I'm not sure I'll use it often. 20 minutes of scary noises from the kitchen when I already need comfort food is not ideal for me. But if you aren't sensitive to loud sounds it does exactly what it says it does..
RESULT
[
{'Classification': 'Efficiency', 'Comment': 'only 20 minutes','Sentiment': 'Positive'},
{'Classification': 'Quality','Comment': 'pretty good soup','Sentiment': 'Positive'},
{'Classification': 'Usability', 'Comment': 'noises it makes when it's blending are somewhat terrifying', 'Sentiment': 'Negative'},
{'Classification': 'Safety','Comment': 'outside gets pretty hot','Sentiment': 'Negative'},
{'Classification': 'Design','Comment': 'Hard to unplug the lid without knocking the blender against the side, which is not a nice sound', 'Sentiment': 'Negative'}
]
DOCUMENT
", REVIEW_TEXT, '\n\nRESULT\n')) as result
FROM catalog.schema.product_reviews
LIMIT 10
Python(程式語言)
import json
from pyspark.sql.functions import expr
# Define the opinion mining prompt as a multi-line string.
opinion_prompt = """You are an opinion mining service. Given a piece of text, output an array of json results that extracts key user opinions, a classification, and a Positive, Negative, Neutral, or Mixed sentiment about that subject.
AVAILABLE CLASSIFICATIONS
Quality, Service, Design, Safety, Efficiency, Usability, Price
Examples below:
DOCUMENT
I got soup. It really did take only 20 minutes to make some pretty good soup.The noises it makes when it's blending are somewhat terrifying, but it gives a little beep to warn you before it does that.It made three or four large servings of soup.It's a single layer of steel, so the outside gets pretty hot. It can be hard to unplug the lid without knocking the blender against the side, which is not a nice sound.The soup was good and the recipes it comes with look delicious, but I'm not sure I'll use it often. 20 minutes of scary noises from the kitchen when I already need comfort food is not ideal for me. But if you aren't sensitive to loud sounds it does exactly what it says it does.
RESULT
[
{'Classification': 'Efficiency', 'Comment': 'only 20 minutes','Sentiment': 'Positive'},
{'Classification': 'Quality','Comment': 'pretty good soup','Sentiment': 'Positive'},
{'Classification': 'Usability', 'Comment': 'noises it makes when it's blending are somewhat terrifying', 'Sentiment': 'Negative'},
{'Classification': 'Safety','Comment': 'outside gets pretty hot','Sentiment': 'Negative'},
{'Classification': 'Design','Comment': 'Hard to unplug the lid without knocking the blender against the side, which is not a nice sound', 'Sentiment': 'Negative'}
]
DOCUMENT
"""
# Escape the prompt so it can be safely embedded in the SQL expression.
escaped_prompt = json.dumps(opinion_prompt)
# Read the source table and limit to 10 rows.
df = spark.table("catalog.schema.product_reviews").limit(10)
# Apply the LLM inference to each row, concatenating the prompt, the review text, and the tail string.
result_df = df.withColumn(
"result",
expr(f"ai_query('databricks-meta-llama-3-3-70b-instruct', request => concat({escaped_prompt}, REVIEW_TEXT, '\\n\\nRESULT\\n'))")
)
# Display the result DataFrame.
display(result_df)
使用結構化串流的 AI 函式
使用 ai_query
和 結構化串流,在近乎即時或微批次案例中套用 AI 推斷。
步驟 1. 讀取靜態 Delta 數據表
讀取靜態 Delta 數據表,就像是數據流一樣
from pyspark.sql import SparkSession
import pyspark.sql.functions as F
spark = SparkSession.builder.getOrCreate()
# Spark processes all existing rows exactly once in the first micro-batch.
df = spark.table("enterprise.docs") # Replace with your table name containing enterprise documents
df.repartition(50).write.format("delta").mode("overwrite").saveAsTable("enterprise.docs")
df_stream = spark.readStream.format("delta").option("maxBytesPerTrigger", "50K").table("enterprise.docs")
# Define the prompt outside the SQL expression.
prompt = (
"You are provided with an enterprise document. Summarize the key points in a concise paragraph. "
"Do not include extra commentary or suggestions. Document: "
)
步驟 2. 套用 ai_query
Spark 只會處理靜態數據一次,除非新的數據列到達數據表。
df_transformed = df_stream.select(
"document_text",
F.expr(f"""
ai_query(
'llama_3_8b',
CONCAT('{prompt}', document_text)
)
""").alias("summary")
)
步驟 3:撰寫摘要輸出
將摘要輸出寫入另一個 Delta 資料表
# Time-based triggers apply, but only the first trigger processes all existing static data.
query = df_transformed.writeStream \
.format("delta") \
.option("checkpointLocation", "/tmp/checkpoints/_docs_summary") \
.outputMode("append") \
.toTable("enterprise.docs_summary")
query.awaitTermination()
使用自定義模型或微調的基礎模型進行批次推斷
本節中的筆記本範例示範批次推斷工作負載,這些工作負載會使用自定義或微調的基礎模型來處理多個輸入。 這些範例需要一個使用 基礎模型 API 布建的輸送量的現有模型服務端點。
使用自定義基礎模型進行 LLM 批次推斷
下列範例筆記本會建立預配置的吞吐量端點,並使用 Python 和 Meta Llama 3.1 70B 模型執行批次 LLM 推論。 它也提供對批次推斷工作負載進行基準測試和建立預配置的吞吐量模型服務端點的指引。
使用自定義基礎模型和布建的輸送量端點筆記本進行 LLM 批次推斷
使用內嵌模型進行 LLM 批次推斷
下列範例筆記本會建立預配置的吞吐量端點,並使用 Python 進行批次 LLM 推斷,您可以選擇使用 GTE 大型(英文)或 BGE 大型(英文)內嵌模型。
LLM 批次推斷內嵌與布建的輸送量端點筆記本
批次推斷和結構化數據擷取
下列範例筆記本示範如何使用 ai_query
執行基本結構化數據擷取,透過自動化擷取技術,將未經結構化的原始數據轉換成有組織的、可使用的資訊。 此筆記本也會示範如何利用 Mosaic AI 代理評估工具,透過真實數據來評估準確性。
批次推斷和結構化數據擷取筆記本
使用 BERT 進行具名實體辨識的批次推斷
下列筆記本顯示使用 BERT 的傳統 ML 模型批次推斷範例。