次の方法で共有


ai.classify 関数を使用してテキストを分類する

ai.classify 関数では、Generative AI を使用して、選択したカスタム ラベルに従って入力テキストを分類し、すべて 1 行のコードで分類します。

AI 関数は、Fabric の組み込みの大規模言語モデルの力を手に入れることで、データ エンジニアリングをターボチャージします。 詳細については、この概要記事 参照してください。

重要

この機能は、Fabric 1.3 ランタイム 以降で使用するために、プレビューにあります。

  • この概要記事 の前提条件 (AI 関数を使用するために一時的に必要な ライブラリのインストール など) を確認します。
  • 既定では、AI 関数は現在、gpt-3.5-turbo (0125) モデルを利用しています。 課金と従量課金レートの詳細については、こちらの記事を参照してください。
  • 基になるモデルは複数の言語を処理できますが、ほとんどの AI 関数は英語のテキストで使用できるように最適化されています。
  • AI 関数の最初のロールアウト中、ユーザーは Fabric の組み込み AI エンドポイントを使用して 1 分あたり 1,000 件の要求に一時的に制限されます。

ヒント

少なくとも 2 つの入力ラベルで ai.classify 関数を使用することをお勧めします。

pandas で ai.classify を使用する

ai.classify 関数は、pandas Series クラスを拡張します。 pandas DataFrame のテキスト列で関数を呼び出して、ユーザー指定のラベルを各入力行に割り当てます。

この関数は、新しい DataFrame 列に格納できる分類ラベルを含む pandas Series を返します。

構文

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

パラメーター

名前 説明
labels
必須
入力テキスト値と照合する分類ラベルのセットを表す 1 つ以上の 文字列

収益

この関数は、各入力テキスト行の分類ラベルを含む pandas Series を返します。 テキスト値を分類できない場合は、対応するラベルが null

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

PySpark で ai.classify を使用する

ai.classify 関数は、Spark DataFramesでも使用できます。 既存の入力列の名前は、分類ラベルの一覧と共にパラメーターとして指定する必要があります。

この関数は、出力列に格納されている入力テキストの各行に一致するラベルを持つ新しい DataFrame を返します。

構文

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

パラメーター

名前 説明
labels
必須
入力列内のテキスト値と照合する分類ラベルのセットを表す文字列配列
input_col
必須
既存の列の名前を含む 文字列 は、カスタムラベルに従って分類するための入力テキスト値を持ちます。
output_col
任意
入力テキスト行ごとに分類ラベルを格納する新しい列の名前を含む 文字列。 このパラメーターが設定されていない場合は、出力列の既定の名前が生成されます。
error_col
任意
新しい列の名前を含む 文字列。 新しい列には、入力テキストの各行を処理した結果として発生する OpenAI エラーが格納されます。 このパラメーターが設定されていない場合は、エラー列の既定の名前が生成されます。 入力行にエラーがない場合、この列の値は null

収益

この関数は、各入力テキスト行に一致する分類ラベルを含む新しい列を含む Spark DataFrame を返します。 テキスト値を分類できない場合は、対応するラベルが null

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

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)