ai_generate_text function

Applies to: check marked yes Databricks SQL

Preview

This feature is in Public Preview.

Returns text generated by a selected large language model (LLM) given the prompt.

Requirements

  • This function is only available on Databricks SQL Pro and Serverless.

  • This feature is in public preview. To enroll in the public preview, please populate and submit the AI Functions Public Preview enrollment form.

Syntax

ai_generate_text(prompt, modelName[, param1, value1] [...])

Arguments

  • prompt: A string expression, the text prompt that passed to selected LLM.

  • modelName: A STRING literal, only 'openai/gpt-3.5-turbo' and 'azure_openai/gpt-35-turbo' are supported.

  • paramN and valueN: key-value pairs to authenticate and configure the selected LLM. The keys must be string literals and are case-sensitive. The types of the values depend on the following keys:

    • Model 'openai/gpt-3.5-turbo' uses the chat completion API from Open AI. It supports the following parameters:

      • 'apiKey': Required. The OpenAI API key to access the model endpoint. You must use the secret function to specify its value.

      • 'temperature': The sampling temperature to use. Its value is a numeric literal between 0 and 2. The default value is 1.0.

      • stop: Stop strings. Its value is a STRING literal or an ARRAY<STRING> of up to 4 string literals. The default value is null.

    • Model 'azure_openai/gpt-35-turbo' uses the chat completion API from the Azure OpenAI Service. It accepts all parameters from the above 'openai/gpt-3.5-turbo' model and any additional parameters to construct the endpoint URL. Databricks only supports API Key authentication.

      • 'resourceName': Required. Its value is a string literal to specify the resource name.

      • 'deploymentName': Required. Its value is a string literal to specify the deployment name.

      • 'apiVersion': Required. Its value is a string literal to specify the API version to use.

Returns

A string expression representing the text regenerated from the selected LLM.

Examples

> 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', 'sg-xxxxxxxxxxxxxxxxxxxxxx',
    'temperature', 0.1);

 Error: DATATYPE_MISMATCH.INVALID_SECRET
 The value expression of API_TOKEN can only be a `secret(<scope>, <key>)` function; however, got StringLiteral sg-xxxxxxxxxxxxxxxxxxxxxx.