Before you Begin

This 30-minute tutorial shows you how to create a SQL dialog with Oracle Digital Assistant.

Background

SQL Dialogs are skills that enable users to interact with databases using natural language, not SQL queries. Users don't need to know SQL or anything about the database schema to retrieve information. Instead, they can query the database using a conceptual understanding of the information. A SQL dialog skill can translate a user's natural language utterances into SQL queries, send the queries to a backend data source, and display the response. For the scenario presented in this tutorial, you're going to query an employee database through a skill to find out individual employee information, office location, and head count by location.

What Do You Need?

  • Access to Oracle Digital Assistant
  • Access to Oracle Database Cloud Service Enterprise Edition
  • Tip:

    You can request a free trial of Oracle Cloud Service Enterprise Edition instance if you don't already have access to one. To complete this tutorial, you must also provision ATP (Oracle Autonomous Transaction Processing) on your trial instance.

Create the Employee Database

To create the database tables, you need to execute a SQL script from your Oracle Database Cloud Service Enterprise Edition instance (workload type Oracle Autonomous Transaction Processing).

  1. Go to your Autonomous Database instance and then click Database Actions.
  2. Description of this  follows
    Description of the illustration
  3. Select SQL.
  4. Description of this  follows
    Description of the illustration
  5. Select New File and then select Worksheet.
  6. Description of the image follows
    Description of the illustration
  7. Paste this script into the worksheet.
  8. Click Run Script and then select Run as SQL.
  9. Click Download Client Connections (Wallet).
  10. The Download Client Connections (Wallet)option
  11. Download the cloud wallet file.

    Note:

    For Oracle Digital Assistant integration, a wallet password must be at least 15 characters, but no more than 64 characters. It must contain at least one uppercase character, one lowercase character, one special character, and one number. It also can't start with a digit. Remember the user name and the user password. You will need them to connect the database to Oracle Digital Assistant.
  12. Description of the image follows
    Description of the illustration

Connect Digital Assistant to the Data Service

Before you can access a data service from any SQL Skill, you need to add a data service integration that enables Oracle Digital Assistant to access the data service. You only need one integration per data service.

Note:

After you create the service, you can't change it. Should the password change, you'll need to delete and recreate the data service integration.
  1. Log into Oracle Digital Assistant.
  2. Click menu icon in the top left corner to open the side menu.
  3. Services menu
  4. Expand Settings and then choose Additional Services.
  5. Additional Services menu option
  6. Open the Data tab then click + Add Service to open the New Data Service dialog.
  7. Enter the following into the Basic Info page:
    Property Value
    Name Enter a unique name for the service.
    Data Service Description An optional description of the data service integration such as a description of the database or the purpose.
    User Name Ask your database administrator for the user name and password.
    Password The user password. For Oracle Digital Assistant integration, a password must be at least 14 characters but no more than 64 characters. It must contain at least one upper case character, one lowercase character and one number. It also can't start with a digit.
  8. Description of this  follows
    Description of the illustration
  9. Click Continue to skip past the End User Authentication page and navigate to the Connection Details page.
  10. Select Basic or Cloud Wallet Connection for the authentication service.>
    • For the Basic connections:
      Name Value
      Host Name Enter the host for the data service. Do not include the scheme (https://). For example, just enter example.com.
      Port The port that allows client connections to the database.
      Service Name Do one of the following:
      • Select SID and enter the Oracle system identifier of the database instance.
      • Select Service Name and enter the service name for the database.
    • Description of this  follows
      Description of the illustration
    • For Cloud Wallet connections:
      Name Value
      Wallet File Navigate to, and select, the cloud wallet file that contains the client credentials, or drag and drop it into the field.
      Wallet Password Enter the password that was provided when the wallet file was downloaded. For Oracle Digital Assistant integration, a wallet password must be at least 15 characters, but no more than 64 characters. It must contain at least one uppercase character, one lowercase character, one special character, and one number. It also can't start with a digit.
      Service Select the name of the database service.

    • Description of this  follows
      Description of the illustration
  11. Click Add Service. With the connection complete, you can import the database schema into SQL skills to create query entities, which enable users to query the database using natural language.
  12. Description of this  follows
    Description of the illustration

Create the SQL Dialog Skill

Create a skill with the dialog mode set to Visual (the default setting).

  1. In the left menu, click Development, then choose Skills.
  2. Additional Services menu option
  3. Click + New Skill.
  4. Hide the side menu by clicking Skills option again.
  5. Name the skill.
  6. Ensure that Visual (the default setting) is selected as the Dialog Mode. You can only create SQL dialogs for skills written in visual mode.
  7. Click Create. You are now ready to import the database schema, which, in turn, creates query entities that model the data service.
  8. Description of this  image follows
    Description of the illustration

Import the Database Schema

You enable data service queries in an SQL skill by creating a logical model that's based on the data service's physical model (the tables and columns). You create the base logical model by importing information about the tables from the data service. During the import, the skill adds query entities, which represent the tables, to the logical model.

When you train your skill, it uses the information from the query entities to build a model for the natural language parser, which enables the skill to translate user utterances into OMRQL (Oracle Meaning Representation Query Language). OMRQL is a query language that's like SQL but is based on object models, which, in this case, are the query entities.

To create query entities for the desired tables in your data service:

  1. Click Entities Entities Menu Icon in the left navbar.
  2. Click More, then select Import from Data Service.
  3. Description of this image follows
    Description of the illustration
  4. Select the data service that you created, and then select the EMP and DEPT tables.
  5. Description of this image follows
    Description of the illustration
  6. Click Import. The skill adds EMP and DEPT query entities for the selected tables.
  7. Open the Configuration tab and then select both entities to verify that primary keys were set.
  8. Description of this image follows
    Description of the illustration
  9. (Optional) For each query entity, set the default order on the General tab, and set the default and minimum attributes on the Configuration tab.
  10. Test the queries:
    • Click Train and then select Trainer Tm.
    • The Train option.
    • After training completes, click Test Queries to open the Query Tester.
    • The Test Queries option.
    • Try out utterances, such as "show all emp", "how many emp are there", "show all dept", and "what is the ename of emp 7499" to review the OMRQL query generated by the skill.
    • Description of this image follows
      Description of the illustration
  11. Click Test Query in the Conversation Tester.
  12. The Test Query in the Conversation Tester option.
    Review the data returned by the query.
    Description of this image follows
    Description of the illustration

Add Natural Language Names and Synonyms for the Query Entities

In this step, you're going to help the skill associate natural language names with the underlying data structure (the physical model) by identifying common names for each query entity and by identifying other ways (synonyms) that people may refer to the query entities.

  1. Select the Emp entity.
  2. Open the Configuration tab. If needed, expand Natural Language.
  3. In the Primary Name field, change the value to employee.
  4. Description of this image follows
    Description of the illustration
  5. Select the Dept entity.
  6. Enter department in the Primary Name field.
  7. Click Edit (located beneath Synonyms).
    The Edit option.
  8. For the Dept Query entity, enter the following synonyms:
    • division
    • organization
    • org

    Note:

    Click Enter after each entry.
  9. To save your changes and close the editor, click The Save and Close icon.
  10. Description of this image follows
    Description of the illustration

Add Natural Language Names and Synonyms for the Attributes

In this step, you're going to identify the commonly used names for each attribute.

To add natural language names and synonyms:

  1. Select the Dept query entity.
  2. Open the Attributes tab.
  3. Description of this image follows
    Description of the illustration
  4. Select the deptno attribute.
  5. Click Edit The Edit Icon to open the Edit Attribute dialog.
  6. Open the Natural Language tab.
  7. Description of this image follows
    Description of the illustration
  8. Enter department in the Primary Name field.
  9. Click Apply.
  10. Repeat these steps to complete the primary name values for the remaining Dept query entity attributes and to add new primary names Emp query entity attributes.
    • For the Dept query entity, update the primary values as follows:
      For This Attribute... Update the Primary Name Using This Value
      dname name
      emp employee
      loc location
    • For the Emp query entity attributes, update the primary values as follows:
      For this Attribute... ...Update the Primary Name Using This Value
      comm commission
      dept department name
      empno employee number
  11. Description of this image follows
    Description of the illustration

Associate Attributes with Value Lists

To improve accuracy, you can create value lists that contain sample values from the data service. The skill automatically associates these lists with their respective attributes, which helps the natural language parser understand the kinds of values those attributes can hold.

To create a value list entity from an attribute:

  1. Select the DEPT query entity.
  2. Open the Attributes tab.
  3. Select the loc attribute and then click Edit The Edit Icon to open the Edit Attribute dialog.
  4. Description of this image follows
    Description of the illustration
  5. Open the General Information tab.
  6. Select Entity from the Type menu to change the attribute's type.
  7. Description of this image follows
    Description of the illustration
  8. Click the If the desired entity doesn't exist, you can generate a value-list entity based on the backend mapping by clicking here link to create a value list entity based on the values found in the data service's schema.
    If the desired entity doesn't exist, you can generate a value-list entity based on the backend mapping by clicking here link
    After a few seconds, a new value list is created named <query entity_name>_<attribute_name> and is automatically selected in the Referenced Entity field. For the loc attribute, the value is dept_loc.
  9. Description of this image follows
    Description of the illustration
  10. Click Apply.
  11. Repeat these steps to create value list entities for the dname attribute. The value list entities generated for the attributes display in the entities list.
  12. Description of this image follows
    Description of the illustration

Add Synonyms to the Value Lists

In this step, you'll add synonyms to the value list entities so that users can refer to the values in different ways.

  1. Select the dept_loc value list entity from the entities list.
  2. Description of this image follows
    Description of the illustration
  3. In the Value (Primary Language) section, select BOSTON, then click Edit The Edit Icon to open the Edit Value dialog.
  4. Description of this image follows
    Description of the illustration
  5. Enter mass in the Synonyms field and then click Enter. Then enter massachusetts and click Enter.
  6. Description of this image follows
    Description of the illustration
  7. Click Update.
  8. Repeat these steps to add synonyms to the CHICAGO, DALLAS, and NEW YORK values.
    For This Value... ...Add This Synonym
    CHICAGO illinois
    DALLAS texas
    NEW YORK big apple
    Description of this image follows
    Description of the illustration

Test Queries Using the Primary Names, Synonyms, and Value List Values

After you add the primary names, synonyms, and value lists, re-train the skill, and then click Preview and test how these help users make queries using a more natural language. Here are some example queries you can try in the tester:

  1. Click Train and then select Trainer Tm.
  2. The Train option.
  3. After training completes, click Preview to open conversation tester.
  4. Description of this image follows
  5. Enter the following queries into the tester. Click Reset after each conversation.
    • List the organizations
    • How many orgs are there?
    • How many divisions are there?
    • Show me department 10
    • Which department is located in the big apple?
    • How many orgs are there in Texas?
    • How many employees work in the big apple?
    • Description of this image follows
      Description of the illustration

      Notice that on the SQL Dialogs tab, you can see the generated OMRQL and SQL for the utterance.

      Description of this image follows
      Description of the illustration

  6. Close the Conversation Tester.

Optional Step: Train with Custom Data

There may be natural language utterances that the skill can't translate to OMRQL. It might be a misinterpretation, or perhaps the model can't handle the domain-specific synonyms that don't seem to be closely related to the primary name. Another example is when the model is not able to distinguish between two similar entities. When this happens, you can use the query entity dataset to teach the skill how to correctly parse the utterance.

For any of these scenarios, we recommend that you start with 20 utterances and add more as needed. Because too many examples might cause the model to over predict attributes and operators, you should focus on a smaller set of diverse utterances rather than a large set of similar, lesser quality ones.

To add custom data from the Dataset tab:

  1. On the Entities page, click the Dataset tab, and click the Query Entities tab.
  2. Description of this image follows
    Description of the illustration
  3. Open the Training Data tab.
  4. The Training Data tab
  5. Click Add Utterance to open the Create Utterance dialog.
  6. Enter the utterance and then click Continue.
  7. Description of this image follows
    Description of the illustration
  8. Review the generated OMRQL query. For example, try the following utterances:
    • show the average commission that a salesperson earns
    • For employees who have the salesperson job, what is the average commission
    • tell me what the average commission is for a person in the salesperson role
    • what is the average commission of people with the role of salesman
    • what's the avg salesperson's commission
    • Which employee has the highest salary?
    • Show employee name and department name ordered by the salary in ascending order
    If the query isn't correct, of if a query couldn't be determined, provide the correct query. Refer to the OMRQL Keyword Reference.
  9. Description of this image follows
    Description of the illustration
  10. Click Done to add the utterance to the dataset.
  11. When you're done, retrain the skill and then try these queries -- along with similar ones -- in the Query Tester or the Conversation Tester.

OMRQL Keyword Reference

Here are the OMRQL keywords that you can use when you define OMRQL queries for the utterances for the data query entities dataset.

Note:

You can use the canonical names from the database schema. You cannot use primary names and synonyms.
Component OMRQL Keywords OMRQL Example
Basic Components  
  • SELECT
  • *
  • FROM
SELECT * FROM emp
Filtering WHERE

Currently, you can't add custom training data that filters by date or date-time.

SELECT * FROM emp WHERE comm > 0
Linking entities (link to attributes) . (period) SELECT * FROM emp WHERE dept.loc = 'NYC'
Ordering  
  • ORDER BY
  • LIMIT
  • ASC
  • DESC
SELECT name FROM emp ORDER BY hiredate DESC LIMIT 10
Aggregate Functions  
  • COUNT
  • DISTINCT
  • AVG
  • SUM
  • MIN
  • MAX
SELECT AVG(sal) from emp
Grouping  
  • GROUP BY
  • HAVING
SELECT COUNT(*) FROM emp GROUP BY dept.loc HAVING dept.loc = 'NYC'
Comparison Operators  
  • =
  • !=
  • <>
  • >
  • <=
  • LIKE
  • NOT LIKE
  • BETWEEN
  • IN
  • NOT IN
SELECT * from dept WHERE name IN ('SALES', 'HR')
Logical Operators  
  • AND
  • OR
  • NOT
SELECT name FROM emp WHERE sal > 100000 AND role = 'VP'

With the exception of linking entities, the OMRQL components are similar to SQL. Instead of an SQL JOIN, you use a pair of link attributes to link one entity to another. Attribute links have primary names and synonyms that define the relationship between the entities. For example an employee/department attribute link with a one-to-one relationship can have a primary name, department, and synonyms works in, belongs to, and team. A department/employees attribute link with a one-to-many relationship can have a primary name employees and synonyms members, and workers.

Besides the typical primary key/foreign key link attributes, you also can have these types of link attributes:

  • Multiple link attributes from one entity to another that define multiple semantic relationships.
  • A link attribute from an entity to itself that implies a self join.

Here are some examples of how to write OMRQL for your utterances:

Utterance SQL OMRQL Comments
Show me all employees who work as a sales associate SELECT * FROM emp WHERE job = 'SALESMAN' SELECT * FROM emp WHERE job = 'SALESMAN' OMRQL is identical to SQL.
Show me all employees who work in sales department SELECT * FROM emp AS T1 JOIN dept AS T2 ON T1.deptno = T2.deptno WHERE T2.dname = 'SALES' SELECT * FROM emp WHERE dept.name = 'SALES' Instead of a JOIN, use "link_attribute.attribute_name" to refer to an attribute from another entity.
Adams is a member of what department? SELECT * FROM dept AS T1 JOIN emp AS T2 ON T1.deptno = T2.deptno WHERE T2.ename = 'ADAMS' SELECT * FROM dept WHERE emp.name = 'ADAMS' Instead of a JOIN, use "link_attribute.attribute_name" to refer to an attribute from another entity.
What is the department location and job role of employee Adams SELECT T1.LOC, T2.JOB FROM DEPT T1 JOIN EMP T2 ON T1.DEPTNO = T2.DEPTNO WHERE T2.ENAME = 'ADAMS' SELECT loc, emp.job FROM dept WHERE emp.ename = 'ADAMS' Note how the OMRQL is simpler to write than the SQL.
How many employees are there for every job role? SELECT COUNT(*), job FROM Emp GROUP BY job SELECT COUNT(*), job FROM Emp GROUP BY job The OMRQL is identical to SQL.
Which employee has the highest salary? SELECT * FROM emp ORDER BY salary DESC LIMIT 1 SELECT * FROM emp ORDER BY salary DESC LIMIT 1 The OMRQL is identical to SQL.
Show employee name and department name ordered by the salary in ascending order SELECT T1.ename, T2.dname FROM Emp AS T1 JOIN Dept AS T2 ON T1.deptno = T2.deptno ORDER BY T1.sal ASC SELECT ename, dept.name FROM Emp ORDER BY salary ASC Note how the OMRQL is simpler to write than the SQL.

Learn More