> For the complete documentation index, see [llms.txt](https://docs.karini.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.karini.ai/agentic-ai-workshops/ai-foundation-labs/generative-bi-agentic-assistant.md).

# Generative BI Agentic Assistant

Build a sales analytics copilot that turns plain-language questions into SQL over a Redshift data warehouse and returns the answers, with no SQL skills required.

**Lab time:** about 45 to 60 minutes

## The Problem

A sales manager wants to know which events generated the most revenue last quarter, and who the top sellers were. The data is all in the warehouse, but getting the answer means knowing which tables hold it, how they join, and how to write the SQL, or filing a request with the analytics team and waiting. Every follow-up question starts the wait over, so most questions never get asked.

## What You Will Build

An Enterprise Sales Assistant: a copilot that answers sales questions in plain language from ticket sales data in Amazon Redshift. Ask it a question and it will:

1. Search the data catalog for the tables and columns that hold the answer
2. Write a SQL query from your question
3. Run the query on the data warehouse
4. Return the answer, and suggest follow-up questions

## What You Will Learn

* Build a data catalog from a Redshift schema and generate business metadata for each table
* Give an agent a catalog tool that finds the right tables and columns for a question
* Give an agent a database tool that runs SQL and returns results
* Compare models side by side and pick a primary and fallback model
* Keep conversation context so follow-up questions build on earlier answers
* Deploy the agent as a copilot with feedback and traces

## Build Order

Build the catalog first, then the agent that uses it, then the recipe, and the copilot last.

| Step | You create                                | Contains                                                           |
| ---- | ----------------------------------------- | ------------------------------------------------------------------ |
| 1    | Catalog: Enterprise-Sale-Data             | Imported Redshift schema with business metadata                    |
| 2    | Agent prompt                              | 2 tools: Enterprise Sales Catalog, Sales Performance Datawarehouse |
| 3    | Workflow recipe: Enterprise Sales Advisor | Chat, Start, Agent, End                                            |
| 4    | Copilot: Enterprise Sales Assistant       | Chat page for end users                                            |
| 5    | End-to-end test                           | Three sales questions                                              |

## Before You Start

* Access to the Karini AI platform
* Access to the Redshift catalog for enterprise sales data, with credentials provided separately
* The **Enterprise Sales Agent-Redshift** prompt template, available in Prompt Playground

## Words You Will See

* **Generative BI:** business intelligence where you ask questions in plain language and AI writes and runs the query
* **Catalog:** a description of a database's tables and columns that helps the agent find the right data
* **Business metadata:** plain-language descriptions of each table and column, generated so the agent understands what the data means
* **Prompt template:** a ready-made prompt stored in Karini. You load it instead of typing it.
* **Tool:** something an agent can call, such as a catalog or a database
* **Recipe:** the workflow that connects everything on a canvas
* **Copilot:** the chat assistant end users open to use the recipe
* **Publish:** saves a version that recipes can use. Saved but unpublished changes are not used.

## Step 1: Create the Catalog

1. Navigate to **Catalog Schema** in the menu bar on the left side.
2. Click **Add new** in the top right corner and configure:

| Field          | Value                               |
| -------------- | ----------------------------------- |
| Name           | Enterprise-Sale-Data                |
| Connector type | Redshift                            |
| Credentials    | The credentials provided separately |

3. Save the catalog, then open it.
4. **Browse**, select all the tables, and click **Import schema** on the right side.
5. In **Catalog explorer**, generate business metadata for each table:
   1. Select each table individually.
   2. Enable the checkboxes to generate metadata.
   3. Click **Generate Metadata** on the right side.
6. Publish the catalog.

The business metadata is what lets the agent match a question like "top sellers" to the right tables and columns, so don't skip it.

Refer to the video tutorial for creating the catalog.

{% embed url="<https://www.loom.com/share/55028e48089d4aa3bb6a1b3581d3051a?sid=7af1d731-ea84-4f52-8516-6c5ded5a26b1>" %}

## Step 2: Create the Agent Prompt

### 2.1 Create the prompt

1. Navigate to **Prompt Playground** on the left side.
2. Click **Add new** in the top right corner.
3. Click **Prompt templates** on the right side and select the **Enterprise Sales Agent-Redshift** template.
4. Increase **Max State Updates** from **3** to **20** for complex reasoning.
5. Scroll down to the **Agent input** box and add this test query:

```
What is the total sales amount for each seller?
```

6. Scroll up and rename your prompt, then save it. You'll return to Prompt Playground; select your prompt.

Save your changes before you navigate away to add tools, or they are lost.

### 2.2 Add tool 1: Enterprise Sales Catalog

1. Open the **Tools** tab, click **Add new**, and fill in:

| Field       | Value                                                                                                        |
| ----------- | ------------------------------------------------------------------------------------------------------------ |
| Name        | Enterprise Sales Catalog                                                                                     |
| Description | The catalog tool uses natural language questions to search relevant tables and columns to create a SQL query |
| Type        | Catalog                                                                                                      |
| Catalog     | The catalog you created in Step 1                                                                            |

2. **Save** the tool.

### 2.3 Add tool 2: Sales Performance Datawarehouse

1. Click **Add new** and fill in:

| Field       | Value                                                                                            |
| ----------- | ------------------------------------------------------------------------------------------------ |
| Name        | Sales Performance Datawarehouse                                                                  |
| Description | This tool lets you run SQL queries on a specified database and table, then retrieve the results. |
| Type        | Database                                                                                         |
| Connector   | Redshift                                                                                         |
| Credentials | The Redshift data warehouse credentials provided                                                 |

2. **Save** the tool.

### 2.4 Test & Compare

1. Open the Test & Compare tab.
2. Select different models. Compare Claude Sonnet 3.7, Claude Sonnet 3.5 v2, and Anthropic Claude Sonnet 4.
3. Click **Test**.
4. Compare the output of the selected models.
5. Click **Select as best answer** to set the best-performing model as the Primary Model.
6. Optional: click **Select as best answer** on another output to assign a Fallback Model.
7. Click **Save prompt run** in the right corner.
8. **Save** and **publish** the prompt.

## Step 3: Create a Workflow Recipe

### 3.1 Create the recipe

1. Navigate to **Recipe** on the left side.
2. Click **Add New** in the top corner and configure:

| Field       | Value                                         |
| ----------- | --------------------------------------------- |
| Name        | Enterprise Sales Advisor                      |
| Description | Intelligent assistant for sales data analysis |
| Type        | Workflow                                      |
| Runtime     | Karini                                        |

### 3.2 Add and connect the nodes

Drag each element onto the recipe canvas, then click it to configure it.

| # | Element | Settings                                                                 |
| - | ------- | ------------------------------------------------------------------------ |
| 1 | Chat    | Conversation history 4 messages. Generate follow-up questions on         |
| 2 | Start   | None                                                                     |
| 3 | Agent   | Prompt = the agent prompt you created in Step 2. Messages = All Messages |
| 4 | End     | None                                                                     |

Connect them in a straight line: Chat → Start → Agent → End.

All Messages keeps the conversation context, so a follow-up like "now just for Shows" builds on the previous answer.

### 3.3 Recipe settings, publish, and test

1. On the right side, set **Number of state updates** to a higher value, such as 75.
2. Click the canvas, then select **Save**.
3. Select your recipe from the recipe table and click **Publish**. By default, the V1 version is deployed once it is published.
4. For later versions, click **Actions** and select **Deploy**.
5. Click **Test** on the right side and try these questions:

| Question                                               |
| ------------------------------------------------------ |
| What is the average sales amount among top 10 sellers? |
| What is the total sales amount for top 10 sellers?     |
| What is the total sales amount for each seller?        |

Refer to the video to create and test the recipe.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7ZrVuiAUMyuYVvrK5KaB%2Fuploads%2FXHdOgju3hkyfbIO59CSF%2FTexr-2-sql.mp4?alt=media&token=9dd54544-38a1-43c5-abf0-600dd8482fae>" %}

## Step 4: Create Your Copilot

Make sure you have **published** the recipe before you start this step.

1. On the recipe page, click **Actions** in the top right corner and select **Create Copilot**.
2. On the Copilots page, click **Add new** and fill in:

| Field           | Value                                                                                                                                                             |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Copilot name    | Enterprise Sales Assistant                                                                                                                                        |
| Welcome message | As preferred                                                                                                                                                      |
| Description     | The Enterprise Sales Advisor Copilot is designed to help you analyze and optimize your sales data.                                                                |
| Seed questions  | What is the average sales amount among top 10 sellers? · What is the total sales amount for the top 10 sellers? · What is the total sales amount for each seller? |
| Error message   | As preferred                                                                                                                                                      |
| Feedback        | Thumbs up and thumbs down on, custom feedback on                                                                                                                  |
| Show traces     | On                                                                                                                                                                |

3. Click **Save**.

Open the copilot in one of two ways:

* **From the saved copilot:** click the **<** icon on the right panel, then click **Visit Copilot**
* **From the left menu:** open **Copilots** in the vertical menu

## Step 5: Test Your Sales Advisor

Open your copilot from Copilots and ask:

| Question                                                                                                  | What it exercises                            |
| --------------------------------------------------------------------------------------------------------- | -------------------------------------------- |
| How much total revenue was generated from ticket sales for events in the "Shows" category by each seller? | Filtering by category and grouping by seller |
| Which events have generated the most revenue, based on sales from associated listings?                    | Joining events, listings, and sales          |
| What is the total sales amount by each user, including their name and email?                              | Joining sales with user details              |

With Show traces on, open the trace on any answer to see the tables the catalog tool found and the SQL the agent ran.

Refer to the video to test the copilot.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7ZrVuiAUMyuYVvrK5KaB%2Fuploads%2F8N9ejV0JE9XSoWHMgBIs%2FText-2-sql%20copilot.mp4?alt=media&token=f94da1fa-97fe-468d-a8c7-efacc06b82aa>" %}
