ai.generate_response関数では、生成 AI を使用して、独自の命令に基づくカスタム テキスト応答を 1 行のコードで生成します。
注
概要
ai.generate_response関数では、pandas DataFrame クラスと pandas Series クラスを拡張できます。
カスタム テキスト応答を行ごとに生成するには、pandas シリーズまたは pandas DataFrame 全体でこの関数を呼び出します。
pandas DataFrame 全体で関数を呼び出す場合、プロンプトはリテラル文字列にすることができ、関数は応答の生成中に DataFrame のすべての列を考慮します。 プロンプトには書式指定文字列を指定することもできます。この場合、関数は、プロンプト内の中かっこの間に表示される列値のみを考慮します。
この関数は、入力行ごとにカスタム テキスト応答を含む pandas Series を返します。 テキスト応答は、新しい DataFrame 列に格納できます。
ヒント
gpt-4.1 に関する OpenAI のプロンプトヒントに従って、より効果的なプロンプトを作成して高品質の応答を得る方法について説明します。
構文
df["response"] = df.ai.generate_response(prompt="Instructions for a custom response based on all column values")
パラメーター
| 名前 | Description |
|---|---|
prompt 必須 |
カスタム応答の入力テキスト値に適用するプロンプト命令を含む 文字列 。 |
is_prompt_template オプション |
プロンプトが書式指定文字列かリテラル文字列かを示す ブール値 。 このパラメーターが Trueに設定されている場合、関数は書式指定文字列に表示される各列名の特定の行値のみを考慮します。 この場合、これらの列名は中かっこの間に含める必要があり、他の列は無視されます。 このパラメーターが既定値の Falseに設定されている場合、関数はすべての列値を各入力行のコンテキストと見なします。 |
response_format オプション |
モデルの応答の予想される構造を指定する ディクショナリ 。
typeフィールドは、自由形式のテキストの場合は "text"、出力が有効な JSON オブジェクトであることを確認するには "json_object" に設定するか、特定の応答構造を適用するカスタム JSON スキーマに設定できます。 このパラメーターが指定されていない場合、応答はプレーン テキストとして返されます。 |
返品ポリシー
この関数は、各入力テキスト行のプロンプトに対するカスタム テキスト応答を含む pandas DataFrame を返します。
Example
# This code uses AI. Always review output for mistakes.
df = pd.DataFrame([
("Scarves"),
("Snow pants"),
("Ski goggles")
], columns=["product"])
df["response"] = df.ai.generate_response("Write a short, punchy email subject line for a winter sale.")
display(df)
このコード セルの例では、次の出力が提供されます。
応答形式の例
次の例では、 response_format パラメーターを使用して、プレーン テキスト、JSON オブジェクト、カスタム JSON スキーマなど、さまざまな応答形式を指定する方法を示します。
# This code uses AI. Always review output for mistakes.
df = pd.DataFrame([
("Alex Rivera is a 24-year-old soccer midfielder from Barcelona who scored 12 goals last season."),
("Jordan Smith, a 29-year-old basketball guard from Chicago, averaged 22 points per game."),
("William O'Connor is a 22-year-old tennis player from Dublin who won 3 ATP titles this year.")
], columns=["bio"])
# response_format : text
df["card_text"] = df.ai.generate_response(
"Create a player card with the player's details and a motivational quote",
response_format={"type": "text"}
)
# response_format : json object
df["card_json_object"] = df.ai.generate_response(
"Create a player card with the player's details and a motivational quote in JSON",
response_format={"type": "json_object"} # Requires "json" in the prompt
)
# response_format : specified json schema
df["card_json_schema"] = df.ai.generate_response(
"Create a player card with the player's details and a motivational quote",
response_format={
"type": "json_schema",
"json_schema": {
"name": "player_card_schema",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"sport": {"type": "string"},
"position": {"type": "string"},
"hometown": {"type": "string"},
"stats": {"type": "string", "description": "Key performance metrics or achievements"},
"motivational_quote": {"type": "string"}
},
"required": ["name", "age", "sport", "position", "hometown", "stats", "motivational_quote"],
"additionalProperties": False,
},
},
},
)
display(df)
このコード セルの例では、次の出力が提供されます。
関連コンテンツ
ai.analyze_sentimentを使用してセンチメントを検出します。
ai.classify を使用してテキストを分類します。
ai.embed を使用してベクトル埋め込みを生成します。
ai_extractを使用してエンティティを抽出します。
ai.fix_grammarを使用して文法を修正します。
ai.similarity を使用して類似性を計算します。
ai.summarize を使用してテキストを集計します。
ai.translate を使用してテキストを翻訳します。
AI 関数の完全なセットの詳細を確認します。
必要な機能が見逃されましたか? ファブリックアイデアフォーラムでそれを提案.