|
| 1 | +# Creating a NL2SQL Agent on OCI |
| 2 | + |
| 3 | +**1. Introduction** |
| 4 | + |
| 5 | +- **Objective**: Step-by-step instructions for building and implementing |
| 6 | + a SQL Agent. |
| 7 | + |
| 8 | +- **Overview**: SQL Agent simplifies data tasks by converting natural |
| 9 | + language queries into SQL, leveraging large language models (LLMs). |
| 10 | + Beyond language capabilities like summarization and text analysis, |
| 11 | + LLMs generate SQL, enabling users to interact with databases |
| 12 | + effortlessly, without requiring SQL expertise. |
| 13 | + |
| 14 | +**2. Prerequisites** |
| 15 | + |
| 16 | +- **Database Setup:** |
| 17 | + |
| 18 | + Create the database (Oracle Base Database or Autonomous Database). |
| 19 | + Database should be in a created in a private subnet (preferred) or |
| 20 | + protected by access control from VCN. |
| 21 | + |
| 22 | +- **Create Table and Load Data** |
| 23 | + |
| 24 | + Create tables in the above database and load data. You can follow instructions |
| 25 | + [here](https://docs.public.content.oci.oraclecloud.com/en-us/iaas/autonomous-database-serverless/doc/load-data-autonomous.html#GUID-BCD09013-DFC9-4850-BBD2-6A90E9AE884B) |
| 26 | + to connect to the DB and load data. |
| 27 | + |
| 28 | +- **Database Tool Connection:** |
| 29 | + |
| 30 | + Create Database Tool Connection to connect to the above database. |
| 31 | + Database Tool Connection should use a Private Endpoint to connect to database, You can follow this [doc](https://docs.public.content.oci.oraclecloud.com/en-us/iaas/database-tools/home.htm) for creating a DB tool connection to any Oracle database service in OCI. |
| 32 | + |
| 33 | +- **Prepare Tool Configuration Data** |
| 34 | + |
| 35 | +> User must keep the table DDLs handy before creating agent. Optionally, |
| 36 | +> they can also prepare Table Column Descriptions, In Context Examples |
| 37 | +> and Custom Instructions. These can be provided to inline to the Agent |
| 38 | +> creation wizard. Alternately, these can be uploaded Object Storage |
| 39 | +> Bucket and same be used in Agent creation wizard. |
| 40 | +
|
| 41 | +- **Policies** |
| 42 | + |
| 43 | + - Create a dynamic group with the below matching rules. |
| 44 | + <img src="./media/image1.png" |
| 45 | + style="width:6.26806in;height:1.30556in" /> |
| 46 | + |
| 47 | + - Add Policies to allow above dynamic group to access resources**:** |
| 48 | + |
| 49 | +allow dynamic-group \<dg-name\> to read database-tools-family in |
| 50 | +compartment \<compartment_name\> |
| 51 | +allow dynamic-group \<dg-name\> to use database-tools-connections in |
| 52 | +compartment \<compartment_name\> |
| 53 | +allow dynamic-group \<dg-name\> to read secret-family in compartment |
| 54 | +\<compartment_name\> |
| 55 | +allow dynamic-group \<dg-name\> to read objects in tenancy |
| 56 | + |
| 57 | +**3. Setting Up the OCI Environment** |
| 58 | + |
| 59 | +**3.1 Logging into OCI Console** |
| 60 | + |
| 61 | +1. Navigate to the [OCI Console](https://www.oracle.com/cloud/). |
| 62 | + |
| 63 | +2. <img src="./media/image2.png" |
| 64 | + style="width:6.34306in;height:3.30694in" />Enter your credentials |
| 65 | + and click **Sign In** and open Generative AI Agents service. |
| 66 | + |
| 67 | +**4. Creating an agent** |
| 68 | + |
| 69 | +1. Click on **Agent** and select the appropriate **Compartment** where |
| 70 | + you want to create the agent. |
| 71 | + |
| 72 | +2. Click on the **Create Agent** button and complete the required |
| 73 | + fields: |
| 74 | + |
| 75 | + 1. Basic Information |
| 76 | + |
| 77 | + 1. Enter the mandatory fields: **Name** and **Welcome |
| 78 | + Message**. The **Description** field is optional. |
| 79 | + |
| 80 | + 2. Check the box to create endpoints for the agent. |
| 81 | + |
| 82 | + 3. <img src="./media/image3.png" |
| 83 | + style="width:6.94583in;height:3.29028in" />You can also |
| 84 | + allow to add tags to organise this resource. For more info |
| 85 | + check |
| 86 | + [this](https://docs.oracle.com/en-us/iaas/Content/Tagging/Concepts/taggingoverview.htm). |
| 87 | + Refer below screenshot. |
| 88 | + |
| 89 | + 2. **Knowledge bases – Not required for SQL.** |
| 90 | + |
| 91 | + 3. **Tools**: Select Conserve with your structured data in |
| 92 | + database(SQL). Fill in the configuration as described below. |
| 93 | + |
| 94 | + 1. **Name**: You need to give a meaningful name to your agent. |
| 95 | + |
| 96 | + 2. **Description**: Provide a detailed summary of the data to |
| 97 | + be used or query via this tool. |
| 98 | + |
| 99 | + 3. **Database schema**: There are two options available to |
| 100 | + import the database schema configuration for this tool. |
| 101 | + |
| 102 | + - **Inline** –you can directly paste the SQL schema in the |
| 103 | + text box. |
| 104 | + <img src="./media/image4.png" style="width:6.5in;height:3.79375in" /> |
| 105 | + |
| 106 | + - **Choose from object storage** – You can also import the |
| 107 | + database schema configuration from an object storage |
| 108 | + bucket. Ensure you have the correct access permissions for |
| 109 | + this. |
| 110 | + <img src="./media/image5.png" style="width:6.55947in;height:3.06in" /> |
| 111 | + |
| 112 | + 4. **Model customization** : It has two option. Choose based on |
| 113 | + your preference. |
| 114 | + |
| 115 | + - Small(cost-effective) – takes less time to create but less |
| 116 | + accuracy. |
| 117 | + |
| 118 | + - Large(more accurate) – longer time but better accuracy. |
| 119 | + |
| 120 | + 5. **Dialect** : slightly different variations of SQL, called |
| 121 | + dialects. While the core SQL syntax is consistent. we |
| 122 | + provide two option for this as well. |
| 123 | + |
| 124 | + - SQLite |
| 125 | + |
| 126 | + - Oracle SQL |
| 127 | + <img src="./media/image6.png" style="width:6.21798in;height:2.8782in" /> |
| 128 | + |
| 129 | + 6. **In-Context Learning Examples:** Provide examples to help |
| 130 | + the tool generate more accurate queries. This step is |
| 131 | + optional. You can import examples either inline or from an |
| 132 | + object storage bucket. <img src="./media/image7.png" |
| 133 | + style="width:6.89744in;height:1.08578in" />** |
| 134 | + ** |
| 135 | + |
| 136 | + 7. **Database tool connection:** you need to select and test |
| 137 | + appropriate connection tool present in your compartment. |
| 138 | + <img src="./media/image8.png" |
| 139 | + style="width:6.73316in;height:3.89387in" /> |
| 140 | + |
| 141 | + 8. **Enable SQL Execution**: To enable SQL execution, turn on |
| 142 | + the **Enable SQL Execution** button. This allows the SQL |
| 143 | + Agent to execute the query and return the result. If |
| 144 | + disabled, the agent will not be able to execute the query. |
| 145 | + |
| 146 | + 9. **Enable Self-Correction**: Enable **Self-Correction** by |
| 147 | + turning on the **Enable Self-Correction** button. This |
| 148 | + allows the SQL Agent to automatically correct the query if |
| 149 | + any corrections are needed. |
| 150 | + <img src="./media/image9.png" |
| 151 | + style="width:4.37304in;height:2.68482in" /> |
| 152 | + |
| 153 | + 10. **Custom instructions** : you can give custom instructions |
| 154 | + like below examples. Or can leave it empty. |
| 155 | + <img src="./media/image10.png" |
| 156 | + style="width:6.52429in;height:0.98215in" /> |
| 157 | + |
| 158 | + 11. D**escription of tables and columns:** You can use this |
| 159 | + option to provide descriptions for tables and columns. |
| 160 | + Descriptions can be imported from object storage, added |
| 161 | + inline, or left blank (none). |
| 162 | + <img src="./media/image11.png" style="width:6.5in;height:2.02917in" /> |
| 163 | + |
| 164 | +3. <img src="./media/image12.png" |
| 165 | + style="width:6.91319in;height:3.10278in" />Click on next button, |
| 166 | + review and create an agent. |
| 167 | + |
| 168 | +**4. Launch chat** |
| 169 | + |
| 170 | +1. Once the agent is ready and tool is created. You can launch the |
| 171 | + chat. |
| 172 | + <img src="./media/image13.png" style="width:6.26806in;height:3.1in" /> |
| 173 | + |
| 174 | + 2. This is how the chat interface will appear, displaying the same |
| 175 | + **Welcome Message** that you provided earlier. |
| 176 | + <img src="./media/image14.png" |
| 177 | + style="width:6.1604in;height:2.68086in" /> |
| 178 | + |
| 179 | +2. You can start asking some basic question to get SQL query like |
| 180 | + this. |
| 181 | + <img src="./media/image15.png" |
| 182 | + style="width:6.26806in;height:2.51389in" /> |
| 183 | + |
| 184 | +3. You can reset chat session to start new chat. |
0 commit comments