DEV Community

Cover image for Connect ChatGPT to your SQL Database
Thomas Hansen for AINIRO.IO

Posted on β€’ Edited on β€’ Originally published at ainiro.io

11 3 4 3 3

Connect ChatGPT to your SQL Database

With our latest real time information release, we're now able to connect ChatGPT to any SQL database you have, and transmit the result of your SQL to ChatGPT, for then to have it analyse your data, answer your question, and return an answer accordingly.

However, before I start explaining the process, let's first enjoy a screenshot 😁

Connect ChatGPT with your SQL database

What you're looking at

The above ChatGPT answer was created by executing the following SQL

select f.title as Film, count(fa.actor_id) as Actors
    from film as f
    join film_actor as fa on f.film_id = fa.film_id
    group by f.title
    order by Actors desc
    limit 10
Enter fullscreen mode Exit fullscreen mode

The above SQL of course is a part of a Hyperlambda snippet, that looks as follows;

data.connect:[generic|sakila]
   data.select:@"select f.title as Film, count(fa.actor_id) as Actors
    from film as f
    join film_actor as fa on f.film_id = fa.film_id
    group by f.title
    order by Actors desc
    limit 10;"
   .result:
   for-each:x:@data.select/*
      set-value:x:@.result
         strings.concat
            get-value:x:@.result
            .:"Film "
            get-value:x:@.dp/#/*/Film
            .:" "
            .:"Actor count "
            get-value:x:@.dp/#/*/Actors
            .:"\r\n"
   return:x:@.result
Enter fullscreen mode Exit fullscreen mode

The above Hyperlambda snippet again is using the Hyperlambda [strings.mixin] slot, which combines natural language text with the result of executing Hyperlambda code. Below is a screenshot of the complete training snippet if you're interested.

Executin SQL to create ChatGPT context data

Once the context is semantically matched towards the query, being e.g.

How many actors played in the movie MUMMY CREATURES?

The SQL extracts the following data;

Film MUMMY CREATURES Actor count 11
Film BOONDOCK BALLROOM Actor count 11
Film LAMBS CINCINATTI Actor count 10
Film HELLFIGHTERS SIERRA Actor count 10
Film ARABIA DOGMA Actor count 10
Film WIZARD COLDBLOODED Actor count 9
Film SUBMARINE BED Actor count 9
Film RANDOM GO Actor count 9
Film LESSON CLEOPATRA Actor count 9
Film IMAGE PRINCESS Actor count 9
Enter fullscreen mode Exit fullscreen mode

Then our OpenAI machine learning functions submits the above data to OpenAI's APIs, which then uses the above as its "real time data foundation for answering the question", the end result being something such as follows;

There was 11 actora playing in the movie MUMMY CREATURES

In the video below I am walking you through the entire process

Pretty kick ass if you ask me 😁

API Trace View

Struggling with slow API calls? πŸ•’

Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (8)

Collapse
 
joshua_hargreaves profile image
Joshua Hargreaves β€’

Nice one , really cool !!

Collapse
 
polterguy profile image
Thomas Hansen β€’

Thx Joshua ^_^

Collapse
 
arianygard profile image
AriaNygard β€’

This is great! What kind of use cases do you think would be the best fit for this?

Collapse
 
polterguy profile image
Thomas Hansen β€’

Integrations with in-house software systems, such as CRM systems, ERP systems, ticket (support) systems, etc - I suspect it would for the most parts be interesting for the AI Expert System ^_^

Collapse
 
arianygard profile image
AriaNygard β€’

Great! So many possibilities for this

Thread Thread
 
polterguy profile image
Thomas Hansen β€’

If you've got the data, we can transform it using natural language ^_^

Collapse
 
tageleander profile image
TageLeander β€’

I can think of so many use cases for this!

Collapse
 
polterguy profile image
Thomas Hansen β€’

It opens up a completely new door related to AI and GPT machine learning yes ^_^

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay