ai_generate_text 函式

適用於:check marked yes 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' 的 STRING 常值。
  • paramNvalueN:要驗證和設定所選 LLM 的機碼/值組。 索引鍵必須是字串常值,而且區分大小寫。 值的類型取決於下列索引鍵:
    • 模型 'openai/gpt-3.5-turbo' 會使用 Open AI 中的聊天完成 API。 它支援下列參數:
      • 'apiKey':必填。 用來存取模型端點的 OpenAI API 金鑰。 指定的值不能是明確的常數位符串。 建議的值包括 secret(scope、key) 函式和 SELECT ... 純量子查詢。
      • 'temperature':要使用的取樣溫度。 其值是和2之間的0數值常值。 預設值是 1.0
      • stop:停止字串。 其值是最多 4 個STRINGARRAY<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案例,請參閱使用 ai_generate_text() 和 OpenAI 分析客戶評論。


> 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.