How to run AI queries using SQL

In this article, you'll learn how to set up an AI-powered SQL chatbot that allows you to manage SQL databases using just your words—no coding or SQL knowledge required. By following step-by-step instructions, you'll download and organize the necessary model from Hugging Face, configure the chatbot in Ozeki AI Studio, and connect it to a MySQL database. You’ll also set up a chat interface where you can interact with the bot, making database management as simple as asking questions in natural language.

Prepare the database

The video below showcases preparing a MySQL database for AI usage. First, the viewer is taught to create tables, then record some data in said tables.

First, create tables for your database, as seen in Figure 1.

Create database tables
Figure 1 - Create database tables

Next, record some data to the tables, illustrated by Figure 2.

Insert data into tables
Figure 2 - Insert data into tables

Download the model

The video provides a step-by-step guide for downloading a specific AI model from Hugging Face and saving it to your local machine. It begins by navigating to the model page on Hugging Face's website. From there, the tutorial shows how to access the "Files and versions" tab to download the correct file (llama-3-sqlcoder-8b-Q4_K_M.gguf). Once the download starts, viewers are instructed to wait for it to complete. After that, the video explains how to create a new folder, C:\AIModels, on your computer, where the downloaded file will be placed for easy access.

Head over to the Hugging Face website, and download the following model (Figure 3):

llama-3-sqlcoder-8b-Q4_K_M.gguf

Download model GGUF file
Figure 3 - Download model GGUF file

Create the a folder with the exact path as follows:

C:\AIModels

Locate the GGUF file you've just downloaded, and place it inside this new folder, as you can see in Figure 4.

Copy model file to C://AIModels
Figure 4 - Copy model file to C://AIModels

AI Studio configurations

This video walks through the process of setting up an AI-powered SQL chatbot and testing it with a prompt. It starts in Ozeki AI Studio, guiding you to create a new AI model by selecting the GGUF format and choosing the llama-3-sqlcoder-8b model file. Afterward, the video shows how to create a new chatbot by selecting AI SQL and configuring it for a MySQL database. You'll see how to set up the connection, create tables using SQL commands, and activate the chatbot. Finally, the video demonstrates sending a database-related query to the bot and verifying its response, followed by checking the chat logs under the Events tab.

Inside OZEKI 10, open AI Studio, highlighted by a red arrow in Figure 5.

Open AI Studio
Figure 5 - Open AI Studio

Click AI models, then Create new AI Model. Select GGUF from the options in the right panel, demonstrated by Figure 6.

Create new GGUF model
Figure 6 - Create new GGUF model

Select the model file you've downloaded from the dropdown list, as shown in Figure 7, and press Ok.

Important: set the temperature (creativity) of this model to 0. The reason why is that this way, the model's answers will be completely deterministic, which is ideal for SQL-related tasks.

To do that, click on your newly created model. In the right panel, navigate to the HW tab. Scroll down, until you get to the Creativity section. Tick the box next to Enable temperature customization, and change the value next to Creativity from (by default) 1 to 0.

Select downloaded model
Figure 7 - Select downloaded model

Click Chat bots, then Create new Chat bot. Select AI SQL from the options in the right panel (Figure 8).

Create new AI SQL chatbot
Figure 8 - Create new AI SQL chatbot

Select your desired SQL database type. For today's tutorial, we'll go with MySQL.

Select SQL database type
Figure 9 - Select SQL database type

Configure the connection settings, as depicted by Figure 10.

Configure database connection details
Figure 10 - Configure database connection details

Open the SQL tab, and insert your create table statements, highlighted by a red frame in Figure 11, then click Ok.

Insert create table statements
Figure 11 - Insert create table statements

Open the chat with the bot. Set the bot's status to online by sliding the toggle in the left panel to the right.

Ask something about the database. Check if the answer is correct, as seen in Figure 12.

Chat with AI
Figure 12 - Chat with AI

Open your chat bot's Events page to check the logs (Figure 13).

AI chat log
Figure 13 - AI chat log

Run AI queries using SQL

This video demonstrates how to set up a myozeki.com account for an AI chatbot using Ozeki's Chat Gateway, create routing rules for incoming and outgoing messages, and test the connection. It begins by opening the Chat Gateway within OZEKI 10, where you'll add a new chat account by installing the "Ozeki chat account" option and entering the bot's login credentials. After setting the account to online, the video walks through creating routing rules for both incoming and outgoing messages. Once the setup is complete, you'll log into the myozeki.com chat interface, send a test message to the chatbot, and verify the connection by checking the bot's event logs for activity.

Open AI studio
Figure 14 - Open AI studio

Select Chat Bot
Figure 15 - Select Chat Bot

Open chat confiugration tab
Figure 16 - Open chat confiugration tab

Configure myozeki user account
Figure 17 - Configure myozeki user account

Chat with SQL AI
Figure 18 - Chat with SQL AI

AI chat log
Figure 19 - AI chat log

Conclusion

By the end of this guide, you've learned how to harness the power of AI to manage SQL databases with ease, even without prior coding or SQL expertise. You successfully downloaded and installed the necessary AI model, created and configured an SQL chatbot in Ozeki AI Studio, and linked it to a MySQL database. After setting up chat routing in Ozeki’s Chat Gateway, you tested the connection and sent queries to the chatbot, all in natural language. Now, managing your SQL databases is as simple as chatting with the AI—making complex database tasks more accessible than ever.

More information