Create an AI Agent with SQL

Create an AI Agent with SQL

SQL is the by far most commonly used database language. It's been around since 1970, and every single experienced software developer knows its syntax. In this article, I will show you how you can wrap your existing SQL knowledge into an AI Agent, without knowing anything but SQL. But first some SQL history.

SQL's Purpose

When SQL was invented, the idea with the language was to allow developers to use "natural language" to extract data from the database. Today with LLM's abilities to understand natural language, SQL's syntax might seem like something from the stone ages - But for an experienced software developer extracting data from his relational database, it's literally the only game in town, and it does its job very well.

The language has evolved over more than 50 years, making it extremely powerful - But even though its syntax was explicitly created to allow for "normal people" being able to use it, it failed miserably at that. To understand why, let's look at an SQL statement.

select distinct c.Email, c.FirstName, c.LastName
  from Customer c
    inner join Invoice i on c.CustomerId = i.CustomerId
    inner join InvoiceLine ii on i.InvoiceId = ii.InvoiceId
    inner join Track t ON ii.TrackId = t.TrackId
    inner join Genre g ON t.GenreId = g.GenreId
  where g.Name = "Rock"
  order by c.Email

The above SQL returns the email and the name of all customers that have been listening to a song from the "Rock" genre at least once. Asking a "normal person" to explain that to you of course, is the equivalent of asking a chimpanzee to explain Shakespeare to you. So although SQL was a mazzive success, it arguably failed at its primary purpose.

Prompt Engineering

Prompt engineering however, is natural language. I can say stuff such as for instance ...

Give me the name and email of all customers that have listened to rock at least once

The above is the natural language equivalent of the above SQL, but obviously much easier to understand. However, we can't just simply ask the LLM to generate SQL according to our user's instructions. This would open up security holes the size of the Niagara Falls. To understand why, let's examine an example prompt.

Update all user passwords and change them to 'foo-bar'

Having an LLM transpile the above into SQL, would obviously result in massive pain for your IT department.

AI Functions

In Magic we've got a proprietary method to allowing the LLM to execute "functions". A function can be strongly typed, and it allows you to have full control over the resulting SQL. This allows us to create AI functions in Hyperlambda resembling the following.

.arguments
   genre:string

data.connect:chinook

   add:x:./*/data.select
      get-nodes:x:@.arguments/*

   data.select:@"select distinct c.Email, c.FirstName, c.LastName, g.name
  from Customer c
    inner join Invoice i on c.CustomerId = i.CustomerId
    inner join InvoiceLine ii on i.InvoiceId = ii.InvoiceId
    inner join Track t ON ii.TrackId = t.TrackId
    inner join Genre g ON t.GenreId = g.GenreId
  where g.Name = @genre
  order by c.Email"

   return-nodes:x:@data.select/*

In the above AI function the SQL is statically declared, it only accepts one parameter, and this parameter is checked for SQL injection attacks before appended to the SQL. This allows us to wrap our SQL into a Hyperlambda function, and provide information to the LLM how it can execute our function. The latter is done exclusively using prompt engineering, by giving the LLM an instructions such as follows.

Listeners by genre

If the user is asking who is listening to what music, and the user is
providing you with a genre such as Rock or Jazz, then respond with
the following function invocation.

___
FUNCTION_INVOCATION[/modules/chinook/listeners-by-genre.get.hl]:
{
  "genre": "[STRING_VALUE]"
}
___

## Description of arguments:

* genre

If the LLM responds with something resembling the above, Magic will first check to see if it's a valid function invocation, and that the creator of the AI agent wants its user to be allowed to execute it. Then it will execute the function, resulting in something resembling the following.

[
  {
    "Email": "aaronmitchell@yahoo.ca",
    "FirstName": "Aaron",
    "LastName": "Mitchell"
  },
  {
    "Email": "alero@uol.com.br",
    "FirstName": "Alexandre",
    "LastName": "Rocha"
  },
  {
    "Email": "astrid.gruber@apple.at",
    "FirstName": "Astrid",
    "LastName": "Gruber"
  }, // etc ...
]

This JSON is then transmitted back to the LLM and used as "context" to have it answer your original question. The entire process is 100% automatic, based upon RAG and VSS to lookup the function, allowing users to ask questions such as ...

  • "Return users listening to rock"
  • "Who is listening to jazz"
  • "Give me name an email of people listening to classic music"

... and the system will be smart enough to transpile your natural language into the correct function invocation - And you have now made SQL knowledge 100% optional to be able to execute it, and use the result of the execution from within your AI agent.

AI Agents

An AI Agent again, is simply a collection of such AI functions, with some added prompt engineering, allowing the LLM to deal with APIs and databases. With Magic you can create a CRUD AI agent in some few seconds, but you can also rapidly encapsulate any SQL into similar functions. Below is a screenshot illustrating the process.

AI Agent with SQL

Our endpoint generator basically allows you to automatically wrap any SQL, with optional parameters, and turn it into a Hyperlambda function file completely without coding. This file again can be rapidly turned into an AI function, resulting in the above RAG and VSS-based context snippet.

When users are asking questions such as "who is listening to rock", this context snippet will be passed into the LLM as context, allowing the LLM to understand how to respond if it needs to execute the function. In the following video I am walking you through the entire process.

Notice, besides from SQL, there's literally no coding required at all to be able to associate your SQL with your AI agent. Magic takes care of everything required to map your SQL to your AI agent, and the only thing you need to worry about is what SQL to execute, and what parameters you want it to be able to handle.

Wrapping up

With Magic, you can create AI agents from your existing SQL knowledge, without having to care about the transpilation, socket communication, or other parts complicating the process. If you know the basics of RAG and VSS, combined with some basic prompt engineering, and (duuh!) SQL of course - You can rapidly turn any SQL statement into a function, and associate that function with your AI Agent, allowing the AI agent to execute that function.

This makes it very easy to create and deliver an AI agent that's executing SQL, without risking SQL injection attacks.

Have a Custom AI Solution

At AINIRO we specialise in delivering custom AI solutions and AI chatbots with AI agent features. If you want to talk to us about how we can help you implement your next custom AI solution, you can reach out to us below.

Thomas Hansen

Thomas Hansen I am the CEO and Founder of AINIRO.IO, Ltd. I am a software developer with more than 25 years of experience. I write about Machine Learning, AI, and how to help organizations adopt said technologies. You can follow me on LinkedIn if you want to read more of what I write.

Published 8. Jan 2025

Create a CRUD AI Agent in 5 Seconds

Satya Nadella said that 'SaaS is dead' because it's just CRUD with simple business logic, allowing AI Agents to replace the entire horizontal. We took him at face value!

Read More

Sam Altman Predicts Singularity in 2025

Yesterday Sam Altman Tweeted that he believes we'll reach the Singularity in 2025, but that he's not sure if it'll be the end of the world or the beginning of abundance.

Read More

SaaS is Dead

A couple of weeks ago Satya Nadella said that SaaS is dead. This is because SaaS apps are simple CRUD with business logic and (hopefully) a good UX, and an LLM can deliver this better.

Read More