რედაქტირება

AI_EXTRACT (Transact-SQL)

Applies to: SQL analytics endpoint in Microsoft Fabric and Warehouse in Microsoft Fabric

The AI_EXTRACT function extracts values from input text using the classes you provide, and returns the result as a JSON object.

Note

  • AI_EXTRACT is in preview.
  • AI_EXTRACT is available only in SQL analytics endpoint and Warehouse in Microsoft Fabric.

Syntax

Transact-SQL syntax conventions

AI_EXTRACT ( text, class1, class2 [ , ...n ] )

Arguments

text

An expression of a character type, for example nvarchar, varchar, nchar, or char.

class1, class2, ...n

One or more property names to extract from the text.

Return types

Returns nvarchar(max) containing JSON text.

Remarks

AI functions return NULL if the AI model can't process the text. Common reasons include:

  • Responsible AI rules block inappropriate content in the input text.
  • Input text exceeds token limits. The current model supports up to 15 KB of text.

Examples

A. Extract properties from a sentence

SELECT ai_extract('Check-in was late and room dirty', 'sentiment', 'problem') AS extraction;

Expected result: {"sentiment":"Negative","problem":"Dirty room"}

B. Parse extracted JSON into columns

SELECT sentiment, time_reported, problem
FROM dbo.hotel_reviews
CROSS APPLY OPENJSON(
    ai_extract(review_text, 'sentiment', 'time_reported', 'problem')
) WITH (
    sentiment VARCHAR(1000),
    time_reported VARCHAR(100),
    problem VARCHAR(1000)
);