Tutorial | Semantic models for agents#

Get started#

Semantic models build a foundation of context between structured datasets and the LLM that queries them.

They can help make responses from LLMs and agents more accurate.

Objectives#

In this tutorial, you will:

  • Build a semantic model with business context.

  • Learn the components of a semantic model, including entities and glossary.

  • Test the semantic model.

  • Use the model in an agent via a tool.

Prerequisites#

To complete this tutorial, you will need:

  • Dataiku 15.1 or later.

  • Data Designer, Advanced Analytics Designer, or Full Designer profile.

  • Connections and a plugin installed by your administrator:

Create the project#

  1. If needed, from the Dataiku Design homepage, use the (Switch view icon.) icon to switch to the Designer view.

  2. Click + New Project > Learning projects.

  3. Search for and select Semantic Models Lab.

  4. If needed, change the folder into which the project will be installed, and click Create.

  5. From the project homepage, click Go to Flow (or type g + f).

Note

You can also download the starter project from this website and import it as a ZIP file.

Use case summary#

Imagine that you work with sales data for an electronics retailer. You are tasked with creating an AI agent that helps your less technical colleagues answer questions from a sales database.

While testing the agent, you’ve noticed that it sometimes needs additional context to accurately answer queries. For example, users might frequently request specific aggregations or data by fiscal year, which aren’t defined in the dataset.

To help solve these issues, you can create a semantic model.

The project contains three datasets that make up the company’s sales data. The transactions dataset is the main dataset that contains details about sales transactions. It joins with the products and customers datasets.

The project also contains two datasets and a folder for testing the semantic model after you build it.

Note

This use case includes a small, simplified example of sales data to reduce the processing costs and time associated with LLM usage.

Prepare the data#

Before building a semantic model, you need to store the datasets in your SQL connection.

  1. With transactions selected in the Flow, open the right Actions panel.

  2. Create a Sync recipe.

  3. Select your SQL connection information to store the data into.

  4. Click Create Recipe.

  5. In the recipe Configuration page, click Run.

  6. Repeat this process for the customers and products datasets.

Screenshot showing how to sync a dataset to a SQL connection.

It’s also a good practice to create column descriptions before putting data into the Semantic Models Lab.

All three datasets already include metadata for the datasets and each column. Take a moment to read through the descriptions by hovering over each column header.

Screenshot showing column descriptions for the dataset.

Tip

In your own projects, if AI assistants are enabled on your instance, you can generate metadata using an LLM. Otherwise, you can add descriptions manually in the Schema panel or by clicking on each column header and selecting Edit column schema.

Create a semantic model#

First, create a new semantic model.

  1. Go to the GenAI menu (GenAI icon.) > Semantic Models.

  2. Click + New Semantic Model.

  3. Choose Create.

  4. Name the model Sales semantic model.

  5. Click Create.

Screenshot showing steps to create a new semantic model.

Generate entities#

The first step to building a semantic model is creating entities, or the tables that will be linked with specific context.

You can do this manually or by using an LLM to create entities according to your instructions. This tutorial will use the automatic mode with an LLM.

  1. Click + Add Entity.

  2. On the Select path screen, choose Generate automatically, then Next.

  3. On the Select datasets screen, the Project should load automatically. If it doesn’t, choose the current project (the key should start with TUT_SEMANTIC_MODELS_LAB).

  4. For the datasets, check the Select All box to select the three SQL datasets, and click Next.

  5. On the Attributes screen, choose the Embedding model from your available LLM connections, and click Next.

  6. On the Provide context screen, choose the LLM model used for generation.

  7. Click Generate.

Screenshot showing the steps to create entities.

Check the entities#

After the model finishes, you can inspect the draft entities it created and save them to the model.

You likely have three entities that align with the original datasets customers, products, and transactions, and have similar names. In this section, you’ll verify that each entity has a proper description, primary key, and attributes (columns). You can edit any of these as needed.

You might also notice that the model has created some metrics or filters in the entities. You’ll look at these later.

Important

LLMs can produce different output. To replicate the rest of this tutorial, you’ll need to ensure that the entity names and attributes match these instructions.

  1. Open the customers entity.

  2. Read the Description and verify that the Primary Key is customer_id.

  3. Verify that the Attributes are:

    • customer_id

    • customer_name

    • customer_status

    • region

  4. Open the products entity, read the Description and verify that the Primary Key is product_id.

  5. Verify that the Attributes are:

    • product

    • product_id

    • product_desc

    • product_type

  6. Open the transactions entity, read the Description and verify that the Primary Key is sales_id.

  7. Verify that the Attributes are:

    • sales_id

    • unit_price

    • date

    • product_id

    • customer_id

    • quantity

  8. Click Save to save all the entities to the semantic model.

Screenshot showing the steps to check entities.

Index the values#

The next step is to index the distinct values for each entity’s attributes. Indexing is necessary for the model’s term resolution, which is when a user’s query terms are fuzzy-matched to values from these attributes.

  1. Navigate to Indexing in the left panel.

  2. The Embedding LLM should be preselected with the model you chose when creating entities. Make any changes if you’d like.

  3. Click Start Indexing.

  4. Wait for the page to display Indexing finished successfully.

Screenshot showing the steps to create entities.

Add metrics#

Within each entity, you can use SQL expressions to define metrics, which are aggregations of the attributes. For this sales data, it would be useful to add metrics for total sales across the entire table and revenue per transaction (among others).

Important

Your model might have added some metrics automatically. If so, it might have already created one or more of these and you can just change the name to match these instructions.

  1. Navigate to the transactions entity.

  2. Under Metrics, click + Add Metric.

  3. Name this metric total_sales.

  4. In the SQL expression window, type SUM(quantity * unit_price).

  5. In the description window, type Total sales value (before returns).

  6. Add another metric with these attributes:

    • Name: avg_rev_per_sale

    • SQL expression: SUM(quantity * unit_price) / COUNT(DISTINCT sales_id)

    • Description: Average revenue per sales transaction

  7. Click Save.

Screenshot showing steps to add metrics to an entity.

Add relationships#

To ensure that queries will work correctly, you can define the relationships between tables.

In this case, the LLM should have automatically created relationships when it set up the entities.

  1. Navigate to Relationships in the left panel.

  2. If relationships are set, check that they match the ones below. If not, add these two relationships manually and Save:

    • transactions relates to customers on transactions.customer_id = customers.customer_id

    • transactions relates to products on transactions.product_id = products.product_id

Screenshot showing steps to check or add relationships among the entities.

Add a Glossary#

The Glossary is a collection of terms, along with their definitions and synonyms, that build business context around the data. You can add terms automatically from business documents or manually, as you will do in this tutorial.

  1. Go to the Glossary in the left panel.

  2. Click + Add Terms and choose Add Manually.

  3. Add the Term region.

  4. For the Description, copy and paste this text:

    Region is an area of the United States comprised of certain states. Each region is defined as:
    
    Northeast: Connecticut, Maine, Massachusetts, New Hampshire, Rhode Island, Vermont, New Jersey, New York, Pennsylvania, Delaware, Maryland
    Midwest: Illinois, Indiana, Michigan, Ohio, Wisconsin, Iowa, Kansas, Minnesota, Missouri, Nebraska, North Dakota, South Dakota
    South: Florida, Georgia, North Carolina, South Carolina, Virginia, West Virginia, Alabama, Kentucky, Mississippi, Tennessee, Arkansas, Louisiana, Oklahoma, Texas
    Southwest: Arizona, Colorado, Idaho, Nevada, New Mexico, Utah
    West: Montana, Wyoming, California, Oregon, Washington, Alaska, Hawaii
    
  5. In Synonyms, write area.

  6. Click Add.

  7. Add another term:

    • Term: fiscal year

    • Description: The fiscal year is from July 1 to June 30.

    • Synonyms: business year, financial year, accounting year

  8. Click Add and Done.

Screenshot showing steps to add terms to the glossary of a semantic model.

In this case, the region term should be linked to an attribute so the model understands that it defines the regions listed in the data. The fiscal year term should remain unlinked so the model uses it as general context when it answers queries.

  1. In the Glossary section, click on the Used tab.

  2. Click + Use Terms and choose Manually.

  3. Make the following selections:

    • Term: region

    • Entity: customers

    • Target Type: Attribute

    • Target Element: region

  4. Click OK.

  5. Under General Terms, click in the Search and add box, then add fiscal year.

  6. Click Save.

Screenshot showing steps to link glossary terms to the semantic model.

The model now has access to both terms from the glossary.

Add instructions#

You can add instructions that will help guide the LLM in every query. In this example, the instructions will be simple, though in the real world they can be much more complex.

  1. Navigate to Instructions in the left panel.

  2. Copy and paste the following instructions:

    Use the sales data to answer questions about sales by product, region, customer, etc.
    
    If a user asks about sales for a state, use the region associated with that state and specify that data is available only by region, not by state.
    
  3. Click Save.

Screenshot showing steps to add instructions for an LLM to use the semantic model.

Test the model#

The model is now ready to test. The Semantic Model Editor provides two ways to test a model: the Playground and the Testing tab.

First, in the Playground, you can send example user queries to see how the model responds. Then, in the Testing panel, you can provide expected outputs and test the model’s responses against them.

Try queries in the Playground#

In this case, you can use some queries designed to test the metrics, glossary definitions, and instructions you set up.

  1. Navigate to Playground in the left panel.

  2. Choose an LLM from your available connections.

  3. Copy and paste this Question to test the metric for total sales, and Run Query:

    What is the total of all sales by region?
    
  4. When the query finishes, review the results. Scrolling down the page, you can see how the model uses glossary terms and the generated SQL query behind the results it returned.

Screenshot showing steps to test a semantic model.

If you like the results, you can save this as a golden query, which can be used to save the expected output of frequent or complex queries.

  1. Click Save as Golden Query.

  2. Give the query a name, such as total sales by region.

  3. Click Save.

  4. Navigate to Golden Queries in the left panel and check that the query has been saved.

Note

You might need to refresh your browser page to see the Golden Query saved.

Screenshot showing steps to save a golden query in a semantic model.

You can continue testing the model with a few other queries. Here are some to copy, or try your own!

  • Test the metric for average revenue per sale:

What was the average revenue per sale in 2024?
  • Test the definition of region, and the instructions to use region instead of state:

What products are sold the most in Massachusetts?
  • Test the definition of fiscal year:

What is the total sales value for fiscal year 2024-25?

Important

Responses to the queries will vary depending on the LLM. In addition to testing multiple queries, you can use the Playground to test responses from various LLMs if you have multiple connections.

Test against expected results#

The Testing panel provides a space for more robust and structured testing. You provide expected output to test against the model’s generated results.

Add testing settings#

First, connect to a managed folder where Dataiku will store prompt sets and test run data after you create them. You can also add an LLM to help diagnose problems identified in test runs.

  1. Navigate to Settings in the left panel.

  2. Scroll to Testing Settings, click the dropdown menu next to Storage, and choose the folder model-testing.

  3. For Diagnosis model, choose an LLM connection.

  4. Click Save.

Screenshot showing steps to add settings for Testing mode.

Next, create a prompt set that defines the questions and expected SQL queries to test the model against.

  1. Navigate to Testing in the left panel.

  2. Click + New Prompt Set.

  3. Choose Import.

  4. In the Import pairs window, click on the Dataset dropdown and choose model_testing_examples.

  5. Map the columns:

    • Question column: question.

    • Expected SQL column: expected_sql.

    • Expected Natural Language answer column: expected_answer.

  6. Click Import.

Screenshot showing steps to import a dataset for testing the model.

Note

The testing dataset contains only five rows to keep LLM costs low for this tutorial. In the real world, you would likely include more data to test against.

After the data imports, you can update the default name of the prompt set to stay organized.

  1. Click on the pencil icon next to New prompt set.

  2. Rename it Prompt set 1.

  3. Click Save.

Screenshot showing steps to rename a prompt set.

Create a test run#

With setup complete, you can create a test run.

  1. Click on the Test Runs tab.

  2. Click + New Test Run.

  3. Under Prompt set, select Prompt set 1.

  4. Under LLM, select from among your LLM connections.

  5. Click Next.

  6. On the Accuracy methods page, leave the defaults.

  7. Click Run.

Screenshot showing steps to set up a new test run.

When the run finishes, review the results.

  1. Review the overall results, including the Pass rate, Estimated total cost, Total duration, and more.

  2. Scroll down and view results for the Questions.

  3. When you click through the questions, view details for each to the right.

Screenshot showing steps to review a test run.

Because Dataiku renames the tables according to your infrastructure, each of the five rows should return a Fail status. You will also see a score that shows how similar the generated SQL is to the expected SQL — in this case, the score is 0.33.

This low performance is to be expected for this tutorial and emphasizes the importance of writing accurate expected SQL with appropriate table names in your real projects.

Diagnose issues#

You can use the Diagnosis tool to dig further into the problem. This uses an LLM to review the expected vs. generated SQL and recommend changes, if any, to the semantic model.

  1. With the first question highlighted, click the Diagnose button.

  2. Review results in the Diagnosis box.

Screenshot showing steps to diagnose an issue.

In this case, the LLM has correctly diagnosed that the table name is the problem and recommends no changes to the semantic model. You might also have some other differences depending on your LLM.

You can also compare the expected vs. generated answer to further evaluate the model’s performance. Here, the answers are substantially the same, although the natural language around them is a bit different.

Screenshot showing steps to compare expected vs. generated answers.

Fix the expected SQL#

To resolve the problem, you’ll first find your unique table names by using the Playground. Then, you can update the expected queries in the dataset and retest.

  1. Navigate to the Golden Query in the left panel. This opens the query you saved from the Playground.

  2. From the generated SQL, note the table naming convention for the transactions_copy and customers_copy tables. This should include your project key and any infrastructure-specific conventions, depending on your SQL connection.

  3. Copy these two table names. You’ll use them to update the expected SQL queries.

  4. Navigate to the provided dataset model_testing_examples_edited. This is an editable dataset where you can update the expected SQL queries.

  5. Go to the Actions panel and select Edit.

  6. In each row of the expected_sql column, replace the table names with your own:

    • transactions_copy → <PREFIX>_TRANSACTIONS_COPY

    • customers_copy → <PREFIX>_CUSTOMERS_COPY

    • products_copy → <PREFIX>_PRODUCTS_COPY. This table doesn’t appear in the golden query, so build its name with the same prefix.

    For example:

    -- Before
    FROM transactions_copy t
    JOIN customers_copy c ON t.customer_id = c.customer_id
    
    -- After
    FROM <PREFIX>_TRANSACTIONS_COPY t
    JOIN <PREFIX>_CUSTOMERS_COPY c ON t.customer_id = c.customer_id
    
  7. If your diagnoses from the previous steps suggested other edits, you may add those too.

Screenshot showing steps to update the expected SQL.

Now create a second test run that uses the new data.

  1. Back in the Testing panel of your semantic model, create a new prompt set as before, this time importing the dataset model_testing_examples_edited.

  2. Rename the prompt set Prompt set 2.

  3. In the Test run tab, create a new test run, this time using Prompt set 2.

  4. Run the test and review the results.

Screenshot showing the second test run.

The results of this test should be improved, although some test cases might not fully pass. In this example, three cases passed and two partially passed. Your results may differ.

You can iterate on the expected SQL in the editable dataset as needed to improve your results.

Screenshot showing results of the second test run.

Tip

When creating multiple test runs, you can use the Compare runs button to view the results side-by-side. This is useful for testing different versions of a semantic model or the performance of different LLMs, for example.

Use the model in an agent#

After you’ve tested the semantic model and are satisfied with it, you can use it in an agent with the Semantic Model Query tool. The tool queries structured data with SQL, using the semantic model as a context layer.

Create a new agent#

First, create the agent.

  1. Go to the GenAI menu (GenAI icon.) > Agents.

  2. Click + New Agent.

  3. Select Simple Visual Agent, give it a name, such as sales data agent and click Create.

  4. In the agent Design page, choose an LLM connection.

  5. In the Instructions window, copy and paste this text:

You are a helpful assistant who queries sales data and provides answers to non-technical users. If the user asks a question that is not about the provided sales data, respond that you are unable to answer.
Screenshot showing steps to create a new agent.

Create the query tool#

Now, you can create a tool that queries the data using the semantic model.

  1. Click + Create Tool.

  2. Choose a Semantic Model Query tool.

  3. Give it the name query_data and click Create.

  4. In the tool Design page, select the Semantic Model name.

  5. Under LLM Configuration, select from your available LLM and Embedding LLM connections.

  6. Click Save.

Screenshot showing steps to create a Semantic Model Query tool.

Next, add the tool to the data analysis agent.

  1. Return to the Agents page in the GenAI menu.

  2. Click on the sales data agent name to open the agent.

  3. Click + Add Tool and select the query_data tool.

  4. Click Save in the top right corner.

Screenshot showing steps to add a tool to the agent.

Test the agent#

Chat with the agent to test it within the agent Design page. You might want to start with some of the same test queries from the semantic model.

  1. Copy and paste this question into the Chat window.

What was the average revenue per sale in 2024?
  1. Continue to test the agent with some of these queries, or try your own:

Which product generated the most total sales value in the Northeast region?
List the average revenue per sale for each region in 2025.
What is the total quantity sold for each product type?
Screenshot showing steps to test the agent that uses the semantic model.

Next steps#

Congratulations! You have successfully built a semantic model via the Semantic Model Lab and used it in an agent!

Your next steps might be to learn about using agents in Agent Hub.