该 ai.embed 函数使用生成式 AI 将文本转换为矢量嵌入。 借助这些向量,AI 可以理解文本之间的关系,因此可以根据含义而不是确切的措辞来搜索、分组和比较内容。 使用单行代码,可以从数据帧中的列生成矢量嵌入。
注释
概述
该 ai.embed 函数可用于 Spark 数据帧。 必须将现有输入列的名称指定为参数。
该函数返回一个新的 DataFrame,其中将每行输入文本的嵌入内容放入一个输出列中。
Syntax
df.ai.embed(input_col="col1", output_col="embed")
参数
| Name | Description |
|---|---|
input_col 必选 |
一个 字符串 ,其中包含用于计算嵌入的输入文本值的现有列的名称。 |
output_col 可选 |
一个 字符串 ,其中包含用于存储每个输入文本行的计算嵌入的新列的名称。 如果未设置此参数,则为输出列生成默认名称。 |
error_col 可选 |
一个 字符串,该字符串包含新列的名称,该列存储因处理每个输入文本行而导致的任何 OpenAI 错误。 如果未设置此参数,则为错误列生成默认名称。 如果输入行没有错误,则此列具有 null 值。 |
退货
该函数返回一个 Spark 数据帧 ,其中包含一个新列,其中包含为每个输入文本行生成的嵌入。 嵌入类型为 [pyspark.ml.linalg.DenseVector])https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.linalg.DenseVector.html#densevector). DenseVector 中的元素数取决于嵌入模型的尺寸,这些维度 在 AI 函数中可配置
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)
此示例代码单元提供以下输出:
相关内容
使用 ai.analyze_sentiment检测情绪。
使用 ai.classify 对文本进行分类。
使用 ai_extract提取实体。
使用 ai.fix_grammar修复语法。
使用 ai.generate_response回答自定义用户提示。
使用ai.summarize功能汇总文本。
使用 ai.translate 翻译文本。
详细了解 完整的 AI 函数集。
自定义 AI 函数的配置。
我们错过了所需的功能吗? 在面料创意论坛上提出建议。