该 ai.generate_response 函数利用生成式人工智能,根据你自己的指令生成自定义文本响应,只需一行代码即可实现。
注释
概述
该 ai.generate_response 函数可以扩展 pandas DataFrame 类和 pandas Series 类。
若要逐行生成自定义文本响应,可以在 pandas 系列或整个 pandas 数据帧上调用此函数。
如果在整个 pandas DataFrame 上调用函数,则提示可以是文本字符串,并且该函数在生成响应时会考虑 DataFrame 的所有列。 提示也可以是格式字符串,其中函数只考虑在提示中的大括号之间显示的列值。
该函数返回一个 pandas Series,其中包含每行输入的自定义文本响应。 文本响应可以存储在新的 DataFrame 列中。
小窍门
了解如何遵循 OpenAI 的 gpt-4.1 提示,创建更有效的提示以获取更高质量的响应。
Syntax
df["response"] = df.ai.generate_response(prompt="Instructions for a custom response based on all column values")
参数
| Name | Description |
|---|---|
prompt 必选 |
一个包含提示说明的 字符串,用于应用于输入文本值以生成自定义响应。 |
is_prompt_template 可选 |
一个 布尔值 ,指示提示是格式字符串还是文本字符串。 如果此参数设置为 True,则该函数仅考虑格式字符串中显示的每个列名中的特定行值。 在这种情况下,这些列名必须出现在大括号之间,而其他列将被忽略。 如果此参数设置为其默认值 False,则该函数会将所有列值视为每个输入行的上下文。 |
response_format 可选 |
一个 字典 ,指定模型响应的预期结构。 可以将该 type 字段设置为自由格式文本的“text”、“json_object”,以确保输出是有效的 JSON 对象,或自定义 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.summarize功能汇总文本。
使用 ai.translate 翻译文本。
详细了解 完整的 AI 函数集。
自定义 AI 函数的配置。
我们错过了所需的功能吗? 在面料创意论坛上提出建议。