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:
An SQL connection.
A connection to one or more Generative AI models. At least one must support embedding.
The Semantic Models Lab plugin (version 1.3.0 or above).
Knowledge of Simple Visual Agents in Dataiku.
Create the project#
If needed, from the Dataiku Design homepage, use the (
) icon to switch to the Designer view.
Click + New Project > Learning projects.
Search for and select Semantic Models Lab.
If needed, change the folder into which the project will be installed, and click Create.
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.
With transactions selected in the Flow, open the right Actions panel.
Create a Sync recipe.
Select your SQL connection information to store the data into.
Click Create Recipe.
In the recipe Configuration page, click Run.
Repeat this process for the customers and products datasets.
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.
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.
Go to the GenAI menu (
) > Semantic Models.
Click + New Semantic Model.
Choose Create.
Name the model
Sales semantic model.Click Create.
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.
Click + Add Entity.
On the Select path screen, choose Generate automatically, then Next.
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).For the datasets, check the Select All box to select the three SQL datasets, and click Next.
On the Attributes screen, choose the Embedding model from your available LLM connections, and click Next.
On the Provide context screen, choose the LLM model used for generation.
Click Generate.
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.
Open the customers entity.
Read the Description and verify that the Primary Key is customer_id.
Verify that the Attributes are:
customer_id
customer_name
customer_status
region
Open the products entity, read the Description and verify that the Primary Key is product_id.
Verify that the Attributes are:
product
product_id
product_desc
product_type
Open the transactions entity, read the Description and verify that the Primary Key is sales_id.
Verify that the Attributes are:
sales_id
unit_price
date
product_id
customer_id
quantity
Click Save to save all the entities to the semantic model.
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.
Navigate to Indexing in the left panel.
The Embedding LLM should be preselected with the model you chose when creating entities. Make any changes if you’d like.
Click Start Indexing.
Wait for the page to display Indexing finished successfully.
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.
Navigate to the transactions entity.
Under Metrics, click + Add Metric.
Name this metric
total_sales.In the SQL expression window, type
SUM(quantity * unit_price).In the description window, type
Total sales value (before returns).Add another metric with these attributes:
Name:
avg_rev_per_saleSQL expression:
SUM(quantity * unit_price) / COUNT(DISTINCT sales_id)Description:
Average revenue per sales transaction
Click Save.
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.
Navigate to Relationships in the left panel.
If relationships are set, check that they match the ones below. If not, add these two relationships manually and Save:
transactionsrelates tocustomersontransactions.customer_id = customers.customer_idtransactionsrelates toproductsontransactions.product_id = products.product_id
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.
Go to the Glossary in the left panel.
Click + Add Terms and choose Add Manually.
Add the Term
region.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
In Synonyms, write
area.Click Add.
Add another term:
Term:
fiscal yearDescription:
The fiscal year is from July 1 to June 30.Synonyms:
business year, financial year, accounting year
Click Add and Done.
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.
In the Glossary section, click on the Used tab.
Click + Use Terms and choose Manually.
Make the following selections:
Term:
regionEntity:
customersTarget Type:
AttributeTarget Element:
region
Click OK.
Under General Terms, click in the Search and add box, then add fiscal year.
Click Save.
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.
Navigate to Instructions in the left panel.
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.
Click Save.
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.
Navigate to Playground in the left panel.
Choose an LLM from your available connections.
Copy and paste this Question to test the metric for total sales, and Run Query:
What is the total of all sales by region?
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.
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.
Click Save as Golden Query.
Give the query a name, such as
total sales by region.Click Save.
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.
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.
Navigate to Settings in the left panel.
Scroll to Testing Settings, click the dropdown menu next to Storage, and choose the folder model-testing.
For Diagnosis model, choose an LLM connection.
Click Save.
Next, create a prompt set that defines the questions and expected SQL queries to test the model against.
Navigate to Testing in the left panel.
Click + New Prompt Set.
Choose Import.
In the Import pairs window, click on the Dataset dropdown and choose model_testing_examples.
Map the columns:
Question column: question.
Expected SQL column: expected_sql.
Expected Natural Language answer column: expected_answer.
Click Import.
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.
Click on the pencil icon next to New prompt set.
Rename it
Prompt set 1.Click Save.
Create a test run#
With setup complete, you can create a test run.
Click on the Test Runs tab.
Click + New Test Run.
Under Prompt set, select Prompt set 1.
Under LLM, select from among your LLM connections.
Click Next.
On the Accuracy methods page, leave the defaults.
Click Run.
When the run finishes, review the results.
Review the overall results, including the Pass rate, Estimated total cost, Total duration, and more.
Scroll down and view results for the Questions.
When you click through the questions, view details for each to the right.
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.
With the first question highlighted, click the Diagnose button.
Review results in the Diagnosis box.
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.
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.
Navigate to the Golden Query in the left panel. This opens the query you saved from the Playground.
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.
Copy these two table names. You’ll use them to update the expected SQL queries.
Navigate to the provided dataset model_testing_examples_edited. This is an editable dataset where you can update the expected SQL queries.
Go to the Actions panel and select Edit.
In each row of the expected_sql column, replace the table names with your own:
transactions_copy→<PREFIX>_TRANSACTIONS_COPYcustomers_copy→<PREFIX>_CUSTOMERS_COPYproducts_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
If your diagnoses from the previous steps suggested other edits, you may add those too.
Now create a second test run that uses the new data.
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.
Rename the prompt set
Prompt set 2.In the Test run tab, create a new test run, this time using Prompt set 2.
Run the test and review the results.
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.
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.
Go to the GenAI menu (
) > Agents.
Click + New Agent.
Select Simple Visual Agent, give it a name, such as
sales data agentand click Create.In the agent Design page, choose an LLM connection.
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.
Create the query tool#
Now, you can create a tool that queries the data using the semantic model.
Click + Create Tool.
Choose a Semantic Model Query tool.
Give it the name
query_dataand click Create.In the tool Design page, select the Semantic Model name.
Under LLM Configuration, select from your available LLM and Embedding LLM connections.
Click Save.
Next, add the tool to the data analysis agent.
Return to the Agents page in the GenAI menu.
Click on the sales data agent name to open the agent.
Click + Add Tool and select the query_data tool.
Click Save in the top right corner.
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.
Copy and paste this question into the Chat window.
What was the average revenue per sale in 2024?
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?
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.
