AI 関数でマルチモーダル入力を使用する (プレビュー)

Important

この機能は プレビュー段階です

AI Functions は、1 行の LLM を利用した変換を、既定でコンカレンシーの高い大規模な pandas または PySpark DataFrames に適用します。 マルチモーダル入力を使用すると、画像、PDF、テキスト ファイルを処理して、ドキュメントの分類、PDF の要約、画像からの情報の抽出などを行うこともできます。

次の表を使用して、マルチモーダルの例と詳細なドキュメントに進みます。

Function 説明 詳細なドキュメント
ai.analyze_sentiment ファイル内のセンチメントを検出します。 pandasPySpark
ai.classify ラベルを使用してファイルを分類します。 pandasPySpark
ai.extract ファイルからフィールドを抽出します。 pandasPySpark
ai.fix_grammar ファイル内のスペル、文法、句読点を修正します。 pandasPySpark
ai.generate_response ファイル コンテンツに固定された応答を生成します。 pandasPySpark
ai.summarize ファイルの内容を要約します。 pandasPySpark
ai.translate ファイルの内容を翻訳します。 pandasPySpark
aifunc.load フォルダーから構造化テーブルにファイルを読み込みます。 構文とパラメーター
aifunc.list_file_paths フォルダーからファイル パスを取得します。 構文とパラメーター
ai.infer_schema ファイルの内容から抽出スキーマを推論します。 構文とパラメーター

サポートされているファイルの種類

マルチモーダル AI Functions では、次の種類のファイルがサポートされます。

  • 画像: jpg、jpeg、png、gif、webp
  • ドキュメント: pdf
  • テキスト ファイル: md、txt、csv、tsv、json、xml、py、およびその他のテキスト ファイル

  • ファイル パス入力を使用したマルチモーダル呼び出しは、既定の responses API で動作します。 api_typeをファイル パス入力のchat_completionsに設定しないでください。
  • Office ファイル形式 (.docx、.pptx、.xlsxなど) は現在サポートされていません。
  • マルチモーダル AI Functions で使用する前に、.docx ファイルと .pptx ファイルを PDF に、.xlsx ファイルを CSV に変換できます。
  • 各入力ファイルのサイズは 50 MB に制限されています。

サポートされている URL プロトコル

マルチモーダル入力は、次のいずれかの URL プロトコルを使用する文字列です。

  • ローカル ファイル パス
  • http(s)
  • wasbs
  • abfs(s)

前提条件

マルチモーダル AI Functions は、テキストベースの AI 関数と同じ前提条件を共有します。 完全な一覧については、「 前提条件」を参照してください。

ファイルを設定する

パスまたは glob スタイルの文字列で参照できるフォルダーにファイルを整理します。

ヒント

すべての AI 関数を使用するエンド ツー エンドの AI Functions の例には、AI Functions スターター ノートブック を使用します。 スターター ノートブックには、Pandas 用のノートブックと PySpark 用のノートブックが 1 つ含まれています。

ノートブックに接続されている Lakehouse にファイルを格納できます。

folder_path = "/lakehouse/default/Files"

ファイルを読み込む

マルチモーダル入力で AI Functions を使用するには、ファイルの内容を構造化テーブルに読み込むか、DataFrame で直接ファイル パスを参照します。 次の例は、両方の方法を示しています。

テーブルにファイルを読み込む

aifunc.load関数を使用して、フォルダーからファイルを読み取り、構造化テーブルを生成します。 この関数は、テーブル構造を単独で推論したり、抽出をガイドするプロンプトを提供したり、一貫性のある構造のスキーマを提供したりすることもできます。 このアプローチは、AI がファイルから特定の情報を抽出し、構造化された形式で表示する場合に便利です。

df, schema = aifunc.load(folder_path)
# or
df, schema = aifunc.load(folder_path, prompt="Give me candidate's name and the most recent company they worked for.")
display(df)

列へのファイル パスの読み込み

または、 aifunc.list_file_paths を使用してフォルダーからファイル パスの一覧を取得し、それらを DataFrame 列に読み込むことができます。 この方法は、各ファイルで AI Functions を実行する場合に便利です。

ほとんどのマルチモーダル関数は、pandas または PySpark のcolumn_type="path"input_col_type/でcol_types="path"を持つファイル パスを受け入れます。

file_path_series = aifunc.list_file_paths(folder_path)
df = pd.DataFrame({"file_path": file_path_series}).reset_index(drop=True)
display(df)

Important

ファイル パスが DataFrame 列に文字列 URL として格納されている場合は、値をプレーン テキストではなくファイル パスとして扱うように AI 関数に明示的に指示する必要があります。

系列レベルの AI Functions (1 つの列で動作) の場合は、column_type パラメーターを設定します。

df["result"] = df["file_path"].ai.analyze_sentiment(column_type="path")

DataFrame レベルの AI Functions (複数の列で動作) の場合は、column_type_dict パラメーターを使用します。

df["result"] = df.ai.generate_response(
    prompt="Describe the content.",
    column_type_dict={"file_path": "path"},
)

aifunc.list_file_paths()を使用してファイル パス列を作成すると、返されたyarl.URL オブジェクトがファイル パスとして自動的に検出されます。 列にプレーンな文字列 URL が含まれている場合にのみ、 column_type="path" を指定する必要があります。

新しいマルチモーダル関数

aifunc.load: テーブルにファイルを読み込む

aifunc.load関数は、フォルダー パスからすべてのファイルを読み取り、その内容から構造化テーブルを生成します。 必要に応じて、抽出をガイドするプロンプト、または一貫性のある構造のスキーマを指定できます。

構文

df, schema = aifunc.load(folder_path, prompt=None, schema=None)

パラメーター

名前 説明
folder_path (必須) フォルダーまたは glob スタイルのパターン マッチング ファイルへの文字列パス。
prompt (省略可能) テーブル生成プロセスをガイドする文字列。 これを使用して、ファイルから抽出するフィールドを指定します。
schema (省略可能) テーブル構造を定義するスキーマ オブジェクト (前の load 呼び出しによって返されます)。 指定すると、関数はこのスキーマを直接使用します。

返品

(DataFrame, schema)のタプル。 DataFrame には、ファイルから抽出された構造化データが含まれています。 スキーマは、後続の load 呼び出しで再利用して、一貫性のある結果を得ることができます。

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

# Basic load – let the AI infer the table structure
df, schema = aifunc.load(folder_path)
display(df)
# This code uses AI. Always review output for mistakes.

# Guided load – provide a prompt to specify what to extract
guided_df, guided_schema = aifunc.load(
    folder_path,
    prompt="Give me candidate's name and the most recent company they worked for.",
)
display(guided_df)

aifunc.list_file_paths: ファイルを一覧表示する

aifunc.list_file_paths関数は、指定したフォルダーからすべての有効なファイル パスをフェッチします。 返されたファイル パスは、任意のマルチモーダル AI 関数への入力として使用できます。 この関数は glob スタイルのパターンもサポートしています。

構文

file_path_series = aifunc.list_file_paths(folder_path)

パラメーター

名前 説明
folder_path (必須) フォルダーまたは glob スタイルのパターン マッチング ファイルへの文字列パス。

返品

pandas シリーズyarl.URL オブジェクト。文字列表現によってインデックスが付けられます。 これらの yarl.URL オブジェクトは、AI Functions によってファイル パスとして自動的に扱われるので、 column_type="path"を指定する必要はありません。

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

file_path_series = aifunc.list_file_paths(folder_path)
custom_df = pd.DataFrame({"file_path": file_path_series}).reset_index(drop=True)
display(custom_df)

ai.infer_schema: ファイルからスキーマを推論する

ai.infer_schema関数は、ファイルの内容から共通スキーマを推論します。 推論されたスキーマは、構造化データ抽出のためにaifunc.ExtractLabelに直接渡すことができるai.extract オブジェクトの一覧として表されます。

構文

schema = df["file_path"].ai.infer_schema(column_type="path")

パラメーター

名前 説明
prompt (省略可能) スキーマ推論をガイドする文字列。 指定しない場合、関数はファイルの内容から単独でスキーマを推論します。
n_samples (省略可能) 推論のためにサンプリングする項目の数を指定する整数。 既定値は 3 です。
column_type (省略可能) 列の値をファイル パスとして扱う場合は、 "path" に設定します。

返品

推論されたスキーマを記述する aifunc.ExtractLabel オブジェクトの一覧。 このリストを ai.extract に渡して、ファイルから構造化データを抽出できます。

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

# Infer a schema from file contents
schema = df["file_path"].ai.infer_schema(column_type="path")
for label in schema:
    print(label)

# Use the inferred schema with ai.extract
extracted_df = df["file_path"].ai.extract(*schema, column_type="path")
display(extracted_df)

既存の AI 関数でマルチモーダル入力を使用する

次の例は、サポートされている各 AI 関数でマルチモーダル入力を使用する方法を示しています。

ai.analyze_sentiment: ファイルからセンチメントを検出する

完全なパラメーターについては、 pandas または PySpark を参照してください。

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

animal_urls = [
    "<image-url-golden-retriever>",  # Replace with URL to an image of a golden retriever
    "<image-url-giant-panda>",  # Replace with URL to an image of a giant panda
    "<image-url-bald-eagle>",  # Replace with URL to an image of a bald eagle
]
animal_df = pd.DataFrame({"file_path": animal_urls})

animal_df["sentiment"] = animal_df["file_path"].ai.analyze_sentiment(column_type="path")
display(animal_df)

ai.classify: ファイルを分類する

完全なパラメーターについては、 pandas または PySpark を参照してください。

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

custom_df["highest_degree"] = custom_df["file_path"].ai.classify(
    "Master", "PhD", "Bachelor", "Other",
    column_type="path",
)
display(custom_df)

ai.extract: ファイルからエンティティを抽出する

完全なパラメーターについては、 pandas または PySpark を参照してください。

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

extracted = custom_df["file_path"].ai.extract(
    aifunc.ExtractLabel(
        "name",
        description="The full name of the candidate, first letter capitalized.",
        max_items=1,
    ),
    "companies_worked_for",
    aifunc.ExtractLabel(
        "year_of_experience",
        description="The total years of professional work experience the candidate has, excluding internships.",
        type="integer",
        max_items=1,
    ),
    column_type="path",
)
display(extracted)

ai.fix_grammar: ファイルの文法を修正する

完全なパラメーターについては、 pandas または PySpark を参照してください。

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

custom_df["corrections"] = custom_df["file_path"].ai.fix_grammar(column_type="path")
display(custom_df)

ai.generate_response: ファイルにカスタム プロンプトを適用する

完全なパラメーターについては、 pandas または PySpark を参照してください。

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

# Series-level: generate a response from each file
animal_df["animal_name"] = animal_df["file_path"].ai.generate_response(
    prompt="What type of animal is in this image? Give me only the animal's common name.",
    column_type="path",
)
display(animal_df)
# This code uses AI. Always review output for mistakes.

# DataFrame-level: use all columns as context
animal_df["description"] = animal_df.ai.generate_response(
    prompt="Describe this animal's natural habitat and one interesting fact about it.",
    column_type_dict={"file_path": "path"},
)
display(animal_df)

ai.summarize: ファイルの要約

完全なパラメーターについては、 pandas または PySpark を参照してください。

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

# Summarize file content from a single column
custom_df["summary"] = custom_df["file_path"].ai.summarize(
    instructions="Summarize this file in one sentence for a support analyst.",
    column_type="path",
)
display(custom_df)

入力列を省略し、 column_type_dict (pandas) または col_types (PySpark) を使用してファイル パス列を指定することで、DataFrame 内のすべての列の値を集計できます。

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

custom_df["summary"] = custom_df.ai.summarize(
    column_type_dict={"file_path": "path"},
)
display(custom_df)

ai.translate: ファイルを翻訳する

完全なパラメーターについては、 pandas または PySpark を参照してください。

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

custom_df["chinese_version"] = custom_df["file_path"].ai.translate(
    "Chinese",
    column_type="path",
)
display(custom_df)

出力品質を評価する

LLM-as-a-Judge を使用してマルチモーダル出力を評価し、精度、精度、再現率、F1、一貫性、整合性、関連性などの計算メトリックを評価する構造化ワークフローには、 AI Functions Eval Notebooks を使用します。 これらのワークフローを使用して、運用環境に移行する前に、分類、抽出、要約、およびその他の AI 関数の結果の品質を検証できます。

コストと容量の使用状況を監視する

AI Functions の課金で、コスト、ランタイム使用状況、容量監視を把握できます。