Use Databricks Assistant

Important

This feature is in Public Preview.

Databricks Assistant is a context-aware AI assistant that can help you with Databricks notebooks, SQL editor, jobs, Lakeview dashboards, and file editor. Databricks Assistant assists you with data and code, when you ask for help using a conversational interface.

What you can do with Databricks Assistant

Databricks Assistant can help with the following tasks:

The assistant uses Unity Catalog metadata to understand your tables, columns, descriptions, and popular data assets across your company to provide personalized responses.

Enable Databricks Assistant

Databricks Assistant is enabled by default. See Enable or disable Databricks Assistant.

Use Databricks Assistant in a notebook cell

In a notebook, Databricks Assistant is available in the Assistant pane or inline in a code cell.

To use Databricks Assistant directly in a code cell, press Cmd + i on MacOS or Ctrl + i on Windows. A text box appears in the cell. You can type a question or comment in English and then press Enter (not Shift+Enter, like you would to run a cell) to have the assistant generate a response.

Inline assistant helps you locate and add enrichment data.

Cell action prompts

Prompt shortcuts help you create common prompts.

Prompt text What the Assistant does
/ Displays common commands
/doc Comments the code in a diff view
/explain Provides an explanation of the code in a cell
/fix Proposes a fix to any code errors in a diff view

When you use /fix or /doc, in the diff window select Accept to accept the proposed changes or Reject to keep the original code. If you accept the proposed code, the code does not automatically run. You can review the code before running it. If the generated code is not what you wanted, try again by adding more details or information to your comment. See Tips for using Databricks Assistant.

For code autocomplete, performance may be better using the Assistant pane than in a notebook cell.

The Assistant closes automatically if you Accept or Reject the code it generated.

Use the Assistant pane

This section describes the default experience of the Assistant pane.

To open the Assistant pane, click Databricks assistant icon. in the left sidebar.

Screenshot of assistant pane in use.

Type questions in the text box at the bottom of the Assistant pane and press Enter or click Enter assistant text. at the right of the text box. The Assistant displays its answer. The following screenshot shows actions you can take after the Assistant has generated code in the Assistant pane.

Icons at the top of the code box in the assistant pane.

You can run the same query again to generate another answer. To do so, hover your cursor over the answer and click Regenerate query icon..

To close the pane, click the icon again or click Close assistant icon. in the upper-right corner of the cell. You can expand the pane to full width by clicking The open full width icon.; click close full width icon to return the pane to default width.

Icons at the top of the assistant pane.

The Assistant pane keeps track of your conversations even if you close the pane or notebook. To clear previous conversations, click Clear assistant icon. at the upper-right of the Assistant pane.

Threads and query history

Conversation threads persist across the different contexts where Databricks Assistant is available. From the Assistant pane, you can create new conversation threads, view query history, and manage your Databricks Assistant experience.

Icons at the top of the assistant pane.

AI-based autocomplete

Important

This feature is in Public Preview.

AI-based autocomplete provides inline code suggestions as you type in Databricks notebooks, the SQL editor, and the file editor. Inline code suggestions are available for Python and SQL.

Enable and disable inline code suggestions

Assistant suggestions are enabled by default.

To disable the feature in your workspace:

  1. Click your profile icon in the top bar, and then click Settings.
  2. From the list on the left, click User > Developer.
  3. Under Experimental features, turn off New Assistant.

Note

Inline code suggestions are not available in Azure Government regions or workspaces with FedRAMP compliance.

Get inline code suggestions: Python and SQL examples

As you type, suggestions automatically appear. Press Tab to accept a suggestion. To manually trigger a suggestion, press Option + Shift + Space (on macOS) or Control + Shift + Space (on Windows).

Animated GIF of code completion for SQL.

Animated GIF of code completion for Python.

AI-based autocomplete can also generate code from comments:

Animated GIF of code completion from a comment.

Debug code: Python and SQL examples

To use Databricks Assistant to fix code, do any of the following:

  • Ask a question in the Assistant pane.
  • Click the Diagnose Error button that appears in the cell results when an error occurs.
  • Click Debug to interactively step through the code line-by-line, set breakpoints, inspect variables, and analyze the program’s execution.

The tabs below show examples in Python and SQL code:

Python

Assistant debugging example in Python.

SQL

Assistant debugging example in SQL.

Explain code

Databricks Assistant can provide detailed explanations of code snippets. Use the /explain prompt and include terms like “be concise” or “explain code line-by-line” to request the level of detail that you want. You can also ask Databricks Assistant to add comments to the code.

Get information from Azure Databricks documentation

In the notebook editor, Databricks Assistant can answer questions based on Databricks documentation.

Answer a question based on Databricks documentation.

Tips for using Databricks Assistant

This section includes some general tips and best practices when using Databricks Assistant.

Databricks Assistant uses context to provide better answers

Databricks Assistant has access to table and column schemas and metadata. This allows you to use natural language and generate more accurate queries. For example, if a table has a column called State, you can ask Databricks Assistant to generate a list of users who live in Michigan.

Databricks Assistant uses the following context:

  • Code or queries in the current notebook cell or Databricks SQL editor tab.
  • Table and Column names and descriptions.
  • Previous prompt questions.
  • Favorite and active tables.
  • For the diagnose error feature, the stack trace from the error output.

When selecting columns from a DataFrame, you can get more accurate results by providing a starting query. For example, provide a statement like SELECT * FROM <table_name>. This allows Databricks Assistant to get the column names and not have to guess.

Because Databricks Assistant uses your conversation history to provide better and more accurate answers, you can ask Databricks Assistant to alter the output of a previous response without having to rewrite the entire prompt. Use the Assistant’s chat history to iteratively clean, explore, filter, and slice DataFrames in the Assistant pane.

Be specific

The structure and detail that Databricks Assistant provides varies from time to time, even for the same prompt. Try to provide the assistant as much guidance as you can to help it return the information you want in the desired format, level of detail, and so on. For example:

  • “Explain this code in a couple sentences” or “Explain this code line-by-line”.
  • “Create a visualization using MatPlotLib” or “Create a visualization using Seaborn”.

Give examples of row-level data values

Because Databricks Assistant does not use row-level data, you might need to provide more detail to prompts to get the most accurate answer. Use table or column comments in Catalog Explorer to add a line of sample data. For example, suppose your height column is in the format feet-inches. To help the assistant interpret the data, add a comment such as “The height column is in string format and is separated by a hyphen. Example: ‘6-2’.” For information about table and column comments, see Document data in Catalog Explorer using markdown comments.

If you need to use column data type conversions to run an operation, you might need to provide details. For example: “convert this code from pandas to PySpark, including the code needed to convert the pandas DataFrame to a PySpark DataFrame and changing the data type of column churn from boolean to integer”.

Use Shift+Enter to add a new line in the chat text box

Use Shift+Enter to add a new line in the Assistant chat text box. This makes it easy to format and organize your messages to Databricks Assistant.

Edit and run code in Databricks Assistant chat pane

Run code in the Assistant pane to validate it or use it as a scratchpad. To run code, click run code icon in the upper-left corner of the code box in the Assistant pane.

The tabs below show examples for Python and SQL code:

Python

run code in assistant pane

SQL

run code in assistant pane

When you run code in the Assistant pane, output is displayed and the variables become usable in the notebook.

You can also edit the code that Databricks Assistant generates directly in the Assistant chat box before moving the code to the notebook.

Additional information

The following articles contain additional information about using Databricks Assistant: