|
| 1 | +# Databricks app using a Genie space |
| 2 | + |
| 3 | +This example demonstrates how to define a Databricks app that uses a Genie space in a Declarative Automation Bundle. |
| 4 | + |
| 5 | +It deploys a Genie space for the `samples.nyctaxi.trips` [table](https://docs.databricks.com/aws/en/discover/databricks-datasets#nyctaxi) and a Flask app that lets users ask the space questions in natural language through the [Genie Conversation API](https://docs.databricks.com/genie/conversation-api.html). |
| 6 | + |
| 7 | +For more information about Databricks Apps, see the [documentation](https://docs.databricks.com/aws/en/dev-tools/databricks-apps). |
| 8 | +For more information about Genie, see the [documentation](https://docs.databricks.com/genie/index.html). |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +* Databricks CLI v1.3.0 or above. |
| 13 | +* Genie spaces can only be deployed with the [direct deployment engine](https://docs.databricks.com/dev-tools/bundles/direct) (`engine: direct`), which is the default for new deployments since CLI v1.3.0. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +1. Modify `databricks.yml`: |
| 18 | + - Update the `host` field to your Databricks workspace URL |
| 19 | + - Update the `warehouse` field to the name of your SQL warehouse |
| 20 | + |
| 21 | +2. Deploy the bundle: |
| 22 | + ```sh |
| 23 | + databricks bundle deploy |
| 24 | + ``` |
| 25 | + |
| 26 | +3. Run the app: |
| 27 | + ```sh |
| 28 | + databricks bundle run genie_assistant |
| 29 | + ``` |
| 30 | + |
| 31 | +4. Open the app in your browser: |
| 32 | + ```sh |
| 33 | + databricks bundle open genie_assistant |
| 34 | + ``` |
| 35 | + Alternatively, run `databricks bundle summary` to display its URL. |
| 36 | + |
| 37 | +## How it works |
| 38 | + |
| 39 | +* `resources/nyc_taxi_genie.genie_space.yml` defines the Genie space, with its data sources, instructions, and sample questions stored in `src/nyc_taxi_genie.geniespace.json`. |
| 40 | +* `resources/genie_assistant.app.yml` declares the Genie space as an app resource. This grants the app's service principal `CAN_RUN` permission on the space: |
| 41 | + ```yaml |
| 42 | + resources: |
| 43 | + - name: "genie-space" |
| 44 | + genie_space: |
| 45 | + name: "NYC Taxi Trip Analysis" |
| 46 | + space_id: ${resources.genie_spaces.nyc_taxi_genie.space_id} |
| 47 | + permission: CAN_RUN |
| 48 | + ``` |
| 49 | +* The `config` block in `resources/genie_assistant.app.yml` injects the space ID into the app as the `GENIE_SPACE_ID` environment variable using `value_from: "genie-space"`. |
| 50 | +* `app/app.py` sends each question to the space with `w.genie.start_conversation_and_wait(...)` and renders the text answer or the generated SQL and its results. |
| 51 | + |
| 52 | +Note that the app queries Genie with its own service principal identity: in addition to the `CAN_RUN` permission on the space granted by the bundle, the service principal must be able to use the SQL warehouse and read the tables that back the space. If access to the `samples` catalog is restricted for service principals in your workspace, point the space at a table the app can read. |
0 commit comments