适用于:
Databricks SQL
重要
此功能目前以公共预览版提供。
警告
AI 函数 ai_generate_text() 已弃用。 Databricks 建议将 ai_query 与外部模型配合使用。
在提供提示的情况下,返回选定的大型语言模型 (LLM) 生成的文本。
要求
- 此函数仅在 Databricks SQL Pro 和无服务器版本上提供。
- 不推荐使用此函数。 Databricks 建议在外部模型中使用 ai_query。
语法
ai_generate_text(prompt, modelName[, param1, value1] [...])
参数
prompt:字符串表达式,即传递给所选 LLM 的文本提示。modelName:字符串字面量,仅支持'openai/gpt-3.5-turbo'和'azure_openai/gpt-35-turbo'。paramN和valueN:用于验证和配置所选 LLM 的键值对。 键必须是字符串字面量,并且区分大小写。 值的类型取决于以下键:- 模型
'openai/gpt-3.5-turbo'使用 Open AI 中的聊天完成 API。 它支持以下参数:'apiKey':必需。 用于访问模型终结点的 OpenAI API 密钥。 指定的值不能是显式常量字符串。 建议的值包括 secret(scope, key) 函数和SELECT ...标量子查询。'temperature':要使用的采样温度。 其值是0到2之间的数字文本。 默认值为1.0。stop:停止字符串。 其值是一个STRING文本或一个最多 4 个字符串字面量的ARRAY<STRING>。 默认值为 null。
- 模型
'azure_openai/gpt-35-turbo'使用 Azure OpenAI 服务的聊天完成 API。 它接受上述'openai/gpt-3.5-turbo'模型中的所有参数以及用于构造终结点 URL 的任何其他参数。 Databricks 仅支持 API 密钥身份验证。'resourceName':必需。 其值是一个用于指定资源名称的字符串字面量。'deploymentName':必需。 其值是一个用于指定部署名称的字符串字面量。'apiVersion':必需。 其值是一个用于指定要使用的 API 版本的字符串字面量。
- 模型
返回
一个字符串表达式,表示从所选 LLM 重新生成的文本。
示例
请参阅使用 ai_generate_text() 和 OpenAI 分析客户评论,了解 ai_generate_text 函数的示例用例。
> SELECT ai_generate_text('Hello', 'openai/gpt-3.5-turbo',
'apiKey', secret('ml', 'key'),
'temperature', 0.1);
Hello! How can I assist you today?
> SELECT ai_generate_text('Hello',
'azure_openai/gpt-35-turbo',
'apiKey', secret('ml', 'key'),
'resouceName', 'resource',
'deploymentName', 'deploy',
'apiVersion', '2023-03-15-preview',
'temperature', 0.1);
Hello! How can I assist you today?
> SELECT ai_generate_text('Hello', 'openai/gpt-3.5-turbo',
'apiKey', (SELECT secret FROM secrets.open_ai_tokens LIMIT 1),
'temperature', 0.1);
Hello! How can I assist you today?
> CREATE FUNCTION summarize(text STRING)
RETURNS STRING
RETURN AI_GENERATE_TEXT(
CONCAT('Summarize the following text: ',
text),
'openai/gpt-3.5-turbo',
'apiKey', (SELECT secret FROM secrets.open_ai_tokens LIMIT 1),
'temperature', 0.1
);
SELECT summarize('This is the text to be summarized.')
This is the summarization.
> SELECT ai_generate_text('Hello',
'openai/gpt-3.5-turbo',
'apiKey', 'sg-xxxxxxxxxxxxxxxxxxxxxx',
'temperature', 0.1);
Error: DATATYPE_MISMATCH.INVALID_SECRET
The parameter value of the "apiKey" argument to the ai_generate_text function can not be a constant 'sg-xxxxxxxxxxxxxxxxxxxxxx'. Recommended usages include `secret(scope, key)` function or a `SELECT ...` subquery.