共用方式為


將 ai.classify 與 PySpark 結合使用

ai.classify 函數使用生成式 AI 根據您選擇的自定義標籤對輸入文本進行分類,只需一行代碼即可。

備註

概觀

ai.classify 函式適用於 Spark DataFrames。 您必須將現有輸入欄的名稱指定為參數,以及分類標籤清單。

函式會傳回一個新的 DataFrame,其中包含符合輸入文字每一列的標籤,並儲存在輸出資料行中。

語法

df.ai.classify(labels=["category1", "category2", "category3"], input_col="text", output_col="classification")

參數

名稱 Description
labels
為必填項目
,代表要與輸入欄中的文字值相符的分類標籤集。
input_col
為必填項目
包含現有欄名稱的 字串 ,其中包含要根據自訂標籤分類的輸入文字值。
output_col
可選
包含新資料行名稱的 字串 ,您要在其中儲存每個輸入文字列的分類標籤。 如果您未設定此參數,則會產生輸出欄的預設名稱。
error_col
可選
包含新欄位名稱的 字串。 新的欄會儲存處理每一行輸入文字時所產生的任何 OpenAI 錯誤。 如果您未設定此參數,則會為錯誤資料行產生預設名稱。 如果輸入資料列沒有任何錯誤,則此資料列中的值會 null

退貨

函式會傳回 Spark DataFrame ,其中包含符合每個輸入文字資料列之分類標籤的新資料行。 如果無法分類文字值,對應的標籤會 null

Example

# This code uses AI. Always review output for mistakes. 

df = spark.createDataFrame([
        ("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!",)
    ], ["descriptions"])
    
categories = df.ai.classify(labels=["kitchen", "bedroom", "garage", "other"], input_col="descriptions", output_col="categories")
display(categories)

此範例程式碼儲存格提供下列輸出:

具有「描述」和「類別」欄的資料框的螢幕擷取畫面。「類別」欄會列出每個描述的類別名稱。