Funzione ai_query

Si applica a: segno di spunta sì Databricks SQL segno di spunta sì Databricks Runtime

Importante

Questa funzionalità è disponibile in anteprima pubblica.

Richiama un endpoint di gestione del modello di Azure Databricks esistente e ne analizza e restituisce la risposta.

Requisiti

Nota

  • In Databricks Runtime 14.2 e versioni successive questa funzione è supportata nei notebook di Databricks, inclusi i notebook eseguiti come attività in un flusso di lavoro di Databricks.
  • In Databricks Runtime 14.1 e versioni successive questa funzione non è supportata nei notebook di Databricks.

Sintassi

Per eseguire query su un endpoint che gestisce un modello esterno o un modello di base:

ai_query(endpointName, request)

Per eseguire query su un endpoint di gestione di un modello personalizzato:

ai_query(endpointName, request, returnType)

Argomenti

  • endpointName: valore letterale STRING, il nome dell'endpoint di gestione del modello di intelligenza artificiale mosaico esistente nella stessa area di lavoro per le chiamate. Il definer deve disporre dell'autorizzazione CAN QUERY per l'endpoint.
  • request: espressione, richiesta usata per richiamare l'endpoint.
    • Se l'endpoint è un modello esterno che gestisce un endpoint o un endpoint api del modello di Databricks Foundation, la richiesta deve essere una stringa.
    • Se l'endpoint è un endpoint di gestione del modello personalizzato, la richiesta può essere una singola colonna o un'espressione di struct. I nomi dei campi dello struct devono corrispondere ai nomi delle funzionalità di input previsti dall'endpoint.
  • returnType: espressione, returnType prevista dall'endpoint. È simile al parametro dello schema nella funzione from_json, che accetta sia un'espressione STRING che una chiamata di schema_of_json funzione. Obbligatorio per l'esecuzione di query su un endpoint di gestione del modello personalizzato.

Valori restituiti

Risposta analizzata dall'endpoint.

Esempi

Per eseguire query su un endpoint di gestione di un modello esterno:

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

Per eseguire query su un modello di base supportato dalle API del modello di Databricks Foundation:

> 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

Facoltativamente, è anche possibile eseguire il wrapping di una chiamata a ai_query() in una funzione definita dall'utente per chiamare la funzione come indicato di seguito:

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

Per eseguire query su un endpoint di gestione di un modello personalizzato:


> 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."]}

Query di esempio per impostare il canale DLT su anteprima:

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