PySpark で ai.embed を使用する

ai.embed関数は、テキストを意味を表すベクター埋め込み関数に変換します。 埋め込みを使用して、正確な表現ではなく、意味によってコンテンツを検索、グループ化、比較します。

  • この記事では、PySpark の ai.embed について説明します。 pandas については、「 pandas で ai.embed を使用する」を参照してください。
  • すべての AI 関数と前提条件については、 AI Functions の概要に関するページを参照してください。
  • PySpark を使用して AI Functions の既定の構成を変更します。

概要

ai.embed関数は Spark DataFrames で使用できます。 既存の入力列の名前をパラメーターとして指定する必要があります。

この関数は、入力テキストの各行の埋め込みを出力列に含む新しい DataFrame を返します。

構文

df.ai.embed(input_col="col1", output_col="embed")

パラメーター

名前 Description
input_col
必須
埋め込みの計算に使用する入力テキスト値を持つ既存の列の名前を含む 文字列
output_col
オプション
入力テキスト行ごとに計算された埋め込みを格納する新しい列の名前を含む 文字列 。 このパラメーターを設定しない場合は、出力列に既定の名前が生成されます。
error_col
オプション
各入力テキスト行の処理に起因する OpenAI エラーを格納する新しい列の名前を含む 文字列。 このパラメーターを設定しない場合は、エラー列の既定の名前が生成されます。 入力行にエラーがない場合、この列には null 値があります。

返品ポリシー

この関数は、入力行ごとに生成された埋め込みを含む新しい列を含む Spark DataFrame を返します。 埋め込み値は pyspark.ml.linalg.DenseVector 値です。 ベクター サイズは、 AI Functions で構成可能な埋め込みモデルのディメンションに依存します。

Example

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

embed = df.ai.embed(input_col="descriptions", output_col="embed")
display(embed)

Output: