Loading Databricks updates...
← All video guides
Generative AI & Agents

SQL LLMs with AI_QUERY: Sentiment Analysis & Extraction

Bring LLMs directly into SQL queries using the AIQUERY function in Databricks SQL.

In short

Bring LLMs directly into SQL queries using the AIQUERY function in Databricks SQL. Three practical examples: sentiment analysis of reviews using Databricks Foundation Models (MPT-3B Instruct), sentiment classification with external LLM endpoints (OpenAI GPT-4), and structured data extraction (player nationality, team, position).

Source: official Databricks documentation · Databricks announcement

Key takeaways

  • AI_QUERY function brings LLMs directly into SQL queries without leaving the SQL editor.
  • Model options include Databricks Foundation Models, external endpoints like OpenAI GPT-4, or custom hosted models.
  • Sentiment analysis example uses MPT-3B to classify reviews as positive or negative directly in SQL.
  • Structured data extraction example uses GPT-4 to pull nationality, team, and position from player names.
  • Databricks SQL serverless compute provides best performance for AI_QUERY function execution.
  • External models require API key storage in Databricks Secrets and endpoint configuration.

Chapters

  • 0:00 — AI_QUERY Function Overview & Model Options
  • 0:32 — Databricks SQL Serverless Performance
  • 1:03 — Sentiment Analysis with Foundation Models (MPT-3B)
  • 2:06 — External LLM Setup: OpenAI GPT-4 Configuration
  • 2:38 — Structured Data Extraction with AI_QUERY

Frequently asked questions

What model options are available for AI_QUERY?

Databricks Foundation Models like MPT-3B Instruct, external endpoints like OpenAI GPT-4, and custom models hosted on serving endpoints. Each option requires different configuration.

How do you set up an external LLM endpoint like GPT-4?

Create an external model by specifying host URL, endpoint name, provider (e.g., OpenAI), and API key stored in Databricks Secrets. Remember the model name for use in AI_QUERY.

Why use serverless for AI_QUERY functions?

Databricks SQL serverless provides the best performance for LLM-based queries without requiring cluster management.

What are practical use cases for AI_QUERY?

Sentiment analysis of reviews, entity extraction from text, classification tasks, and any NLP operation that can be parameterized. Results can be used directly in subsequent SQL queries or analysis.

Full transcript

Auto-generated from the video and lightly edited for readability. Timestamps link to the moment in the video.

Show full transcript (10 segments)

0:01hi I'm here today in order to answer one simple question how can I enable SQL analyst to Leverage The Power of large language models and the answer is easy and stretchable use the function AI query which is a SQL built-in function in data break SQL this function will allow you

0:23to make models accessible from SQL queries whether they are custom models hosted uh by a model serving endpoint models hosted by datab BR Foundation model apis or external models of course make sure to use datab bre SQL serverless to benefit from the best performance let me show let me show you

0:46the first example where I will leverage uh data breas Foundation model apis so the first thing I will do go to uh Ser endpoint over here click on query and and then copy the name of the model which is data breaks mpt3 billion instruct and I will go back to the SQL

1:09editor where we can see that I have a number of reviews for a specific restaurant so the next step would be to use the aior query with the model uh uh with the MPT model to score the reviews to see whether they are positive or negative so I added the prompt over here as you can see I provided the customer

1:32review in back six and I asked the model to classify the number of reviews in two categories whether it's positive or negative so let's run this query and you will see okay I have revie so we see walls love this place it's a positive CR is not good it's negative so this is the example where I where you use Foundation

1:55model apis if you want to use an external model you need to create an external model endpoint and to do so you need to to follow uh the example below you need to have the host the URL the name of uh your endpoint the provider op AI uh you need

2:17also to store the API keys for my case I use datab break secrets to store the API key and of course the name of the model for my case is gp4 you run this query and then just remember the name of your model youf openi chat and if I go back to external model I will use the same function Usef open a chat and I will uh

2:42again provide with a prompt to make sure that my model classifies uh the refuse same thing positive negative and so on and because I'm a huge fan of football I have a list of players hakeim Z Asim Char Kim almed and so on and in the next

3:05example I asked uh the model to get the nationality of the player the team he played with in the season 2020 and the position and if I run this query you will see I have the response so zash is Moroccan tied for Chelsea and he's a right winger and as you can see

3:30very easy to get started with llms using SQL queries

Official Databricks resources

Related

Databricks Answers

Looking for a quick, source-backed answer? Browse Databricks Answers on GenAI, agents & Vector Search — direct, documentation-validated answers to common Databricks questions, curated by Youssef Mrini, Senior Developer Advocate at Databricks.