共用方式為


ai_query函式

適用於: 檢查標示為是 Databricks SQL 檢查標示為是 Databricks Runtime

重要

這項功能處於公開預覽狀態

叫用現有的 Azure Databricks 模型服務端點 ,並剖析並傳回其回應。

需求

  • Azure Databricks SQL 傳統無法使用此函式。
  • 預設會啟用查詢基礎模型 API。 若要查詢提供自定義模型或外部模型的端點,請在 Databricks 預覽 UI啟用自定義模型和外部模型的AI_Query。
  • 目前的 DLT 倉儲通道不會使用支援 ai_query()的最新 Databricks 執行時間版本。 pipelines.channel將資料表屬性中的 設定為 'preview' 使用 ai_query()。 請參閱 範例查詢的範例

注意

  • 在 Databricks Runtime 14.2 和更新版本中,Databricks 筆記本支援此函式,包括以 Databricks 工作流程中工作身分執行的筆記本。
  • 在 Databricks Runtime 14.1 和以下版本中,Databricks 筆記本不支援此函式。

語法

若要查詢提供外部模型或基礎模型的端點:

ai_query(endpointName, request)

若要查詢提供端點的自訂模型:

ai_query(endpointName, request, returnType)

引數

  • endpointName:STRING 常值,用於調用之相同工作區中現有馬賽克 AI 模型服務端點的名稱。 定義器必須具有端點的 CAN QUERY 許可權。
  • request:表達式,用來叫用端點的要求。
    • 如果端點是服務端點或 Databricks Foundation 模型 API 端點的外部模型,要求必須是 STRING。
    • 如果端點是提供端點的自定義模型,要求可以是單一數據行或結構表達式。 結構域名應該符合端點所預期的輸入特徵名稱。
  • returnType:表達式,來自端點的預期 returnType。 這類似於 from_json 函式中的架構參數,它同時接受 schema_of_json 函式的 STRING 運算式或調用。 查詢自定義模型服務端點的必要專案。

傳回

來自端點的已剖析回應。

範例

若要查詢服務端點的外部模型:

> SELECT ai_query(
    'my-external-model-openai-chat',
    'Describe Databricks SQL in 30 words.'
  ) AS summary

  "Databricks SQL is a cloud-based platform for data analytics and machine learning, providing a unified workspace for collaborative data exploration, analysis, and visualization using SQL queries."

若要查詢 Databricks Foundation 模型 API 所支持的基礎模型:

> SELECT *,
  ai_query(
    'databricks-meta-llama-3-1-70b-instruct',
    "Can you tell me the name of the US state that serves the provided ZIP code? zip code: " || pickup_zip
    )
  FROM samples.nyctaxi.trips
  LIMIT 10

或者,您也可以在 UDF 中包裝 對 的呼叫 ai_query() ,以便呼叫函式,如下所示:

> CREATE FUNCTION correct_grammar(text STRING)
  RETURNS STRING
  RETURN ai_query(
    'databricks-llama-2-70b-chat',
    CONCAT('Correct this to standard English:\n', text));
> GRANT EXECUTE ON correct_grammar TO ds;
- DS fixes grammar issues in a batch.
> SELECT
    * EXCEPT text,
    correct_grammar(text) AS text
  FROM articles;

若要查詢提供端點的自訂模型:


> SELECT text, ai_query(
    endpoint => 'spam-classification-endpoint',
    request => named_struct(
      'timestamp', timestamp,
      'sender', from_number,
      'text', text),
    returnType => 'BOOLEAN') AS is_spam
  FROM messages

> SELECT ai_query(
    'weekly-forecast',
    request => struct(*),
    returnType => 'FLOAT') AS predicted_revenue
  FROM retail_revenue

> SELECT ai_query(
    'custom-llama-2-7b-chat',
    request => named_struct("messages",
        ARRAY(named_struct("role", "user", "content", "What is ML?"))),
    returnType => 'STRUCT<candidates:ARRAY<STRING>>')

  {"candidates":["ML stands for Machine Learning. It's a subfield of Artificial Intelligence that involves the use of algorithms and statistical models to enable machines to learn from data, make decisions, and improve their performance on a specific task over time."]}

將 DLT 通道設定為預覽的範例查詢:

> create or replace materialized view
    ai_query_mv
    TBLPROPERTIES('pipelines.channel' = 'PREVIEW') AS
  SELECT
    ai_query("databricks-dbrx-instruct", text) as response
  FROM
    messages