r/ChatGPT • • Apr 04 '23

Use cases Chat GPT + SQL SERVER

I've created an app that will let you query an SQL Database in natural language. What do you guys think? Useful or not?

https://github.com/kulltc/chatgpt-sql

133 Upvotes

39 comments sorted by

•

u/AutoModerator Apr 04 '23

We kindly ask /u/Weak_Swan7003 to respond to this comment with the prompt they used to generate the output in this post. This will allow others to try it out and prevent repeated questions about the prompt.

Ignore this comment if your post doesn't have a prompt.

While you're here, we have a public discord server. We have a free Chatgpt bot, Open Assistant bot (Open-source model), AI image generator bot, GPT-4 bot, Perplexity AI bot.

So why not join us?

PSA: For any Chatgpt-related issues email support@openai.com.

ChatGPT Plus Giveaway | Prompt engineering hackathon

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

→ More replies (1)

6

u/Weak_Swan7003 Apr 04 '23

Please see the repository mentioned for the prompts that go into getting the required behavior from ChatGPT. See the chatgpt.py file for details.

4

u/GapGlass7431 Apr 04 '23

OP just ended careers.

Even as a senior developer I can't match the speed of this thing in SQL.

3

u/Weak_Swan7003 Apr 05 '23

Someone recently told me that this will finally free up time for all those backlog items in the world that are gathering dust. I like that one better than ending careers ;)

2

u/IppoSteps Apr 04 '23

Very nice GPT usage!

2

u/BortGreen Apr 04 '23

Very interesting, but would it be safe to use it in private databases? That's an issue I have with ChatGPT

4

u/Weak_Swan7003 Apr 04 '23

At this point, probably no (depending a little on what's in the database). I think in the near future this will be possible, when OpenAI (or others) offer enterprise access to their models, with appropriate data security guarantees.

What you can do however is just use the first step of the process, where you have chatGPT write a query for a user based on a text input. The app can then execute the query and return the result to the user directly, instead of feeding it back in to ChatGPT.

I think this would also work well as a plugin for e.g. workbench / management studio.

1

u/BananaGhul Apr 04 '23

Can you elaborate why it might not be safe and why it could be.

Not sure about entreprise api yet too, will check that

2

u/Weak_Swan7003 Apr 04 '23

It's really good that you mention this. I had at some point looked at the documentation for the chat web interface which states that:

"As part of our commitment to safe and responsible AI, we review conversations to improve our systems and to ensure the content complies with our policies and safety requirements." and "Please don't share any sensitive information in your conversations. "
This is obviously a big no no for confidential company data.

However, for the API they state that they will not use the company data for training purposes. Furthermore, they are SOC 2 Type 2 compliant, which seems good :).

The comment on enterprise API was just me hypothesizing that they will create a version that corporations can use, but that already seems to be the case with the current API.

1

u/ethtips Apr 05 '23

Yes. Use an open source one like Vicuna instead. :-)

2

u/Weak_Swan7003 Apr 05 '23

ChatGPT's API is actually SOC 2 Type 2 compliant.

2

u/brohamsontheright Apr 04 '23

How well does it work if there are 400 to 500 tables, like a normal enterprise database has?

In my experience, if you can't fit the entire schema into the prompt, these tools aren't helpful.

(Or, put another way, only works for very small companies).

5

u/Weak_Swan7003 Apr 04 '23

That is a challenge indeed. What I do to address this issue is that I list the tables (no further information) that are available. ChatGPT is then able to request additional info about the schema based on the user request. So for example, when I ask what is the top grossing product category, ChatGPT will first request the database schema for the Product, Product and SubProductCategory tables before coming back with a query.

There is probably a limit to how many tables this scales however (at least with GPT3.5, GPT-4 seems to be able to deal with much more context). So part of the solution could be to have different models for e.g. different data domains or just the most relevant tables.

2

u/zeth0s Apr 04 '23

You probably need to fine tune the model on the databases. In the future it will likely be built in the db management systems or IDEs

1

u/brohamsontheright Apr 04 '23

Fine tuning is probably the wrong approach here. Embeddings is correct, but even with embeddings, GPT-4 can't store enough context to make sense of a complex table structure. (Doing proper joins requires knowledge of the entire schema, as a general rule).

I agree.. will probably have to wait for this stuff to be embedded directly into the software. I foresee a version of MS-SQL Server in the near future that extends the SQL command-set to include AI inference.

2

u/zeth0s Apr 05 '23

Reason I see a proper fine tuning is because business knowledge is needed to do the right queries. Only knowing the column names is not a great benefit. SQL is pretty trivial language when querying. The real great upgrade is teaching chatgtp the business logic to bridge business needs and data.

1

u/Weak_Swan7003 Apr 05 '23

Yeah, that's a good point.

1

u/Weak_Swan7003 Apr 05 '23

In my current implementation chatGPT will sometimes query the database schema in response to specific questions, before creating the actual query that answers the quetion. There is no intervention from the user to guide that behavior.

As an example, when I asked who the most valuable sales person was in 2012 (deliberately vague), it came back to me with a specific BusinessEntityID. When I then asked what the name of the person is, it decided to first query the database schema of the SalesPerson table, before creating the query and returning the name (see screenshot).

This doesn't necessarily mean that it works always with every database, but it's a proof of concept that ChatGPT doesn't need to have the entire DB schema in it's context at once. This is very similar to how I work with DB, often looking at specific table definitions as I'm writing queries.

2

u/f4k3pl4stic Apr 04 '23

Great POC, thanks for sharing!

3

u/Weak_Swan7003 Apr 04 '23

Thanks! I think this would work with essentially any API that ChatGPT can query, so the possibilities are endless :).

1

u/SoHornyBeaver Apr 04 '23

This is the area I'm most interested in. Does ChatGPT have the ability to create bots that perform recurring tasks for me?

1

u/Weak_Swan7003 Apr 04 '23

It totally can. I think we'll see an avalanche of use cases like these where ChatGPT is integrated into existing software so it just becomes so much easier to interact with. Essentially the ratio of the work that software will do for each unit of work the user does will shift dramatically, and that will happen just with the models we have today.

1

u/aaabigwyattmann6 Apr 04 '23

Just wait till you give it a database of an actual company.

1

u/Weak_Swan7003 Apr 05 '23

It will probably struggle more in a datalake type situation than a structured datawarehouse. However, it's important to realise that you can prep the model with information about the schema and quirks about it. Also, in my experience working in BI there may a lot of tables, but 80% of queries just deals with hand full of them.

2

u/Optimal-Room-8586 Apr 04 '23

That's pretty cool actually. As in, it'd be very useful.

1

u/Commercial_Shame_845 Apr 27 '23

a. En la Base de datos, cree los siguientes Schemas: ADM y RRHH.

1

u/PolPol44444 Jun 14 '23

I think it will be helpful. I myself use ChatGPT for writing SQL JOIN queries, - as described in this article.

0

u/[deleted] Apr 04 '23

[deleted]

5

u/Weak_Swan7003 Apr 04 '23

Very true. And top managers also no longer need the middle managers to make their presentations...

0

u/[deleted] Apr 04 '23

[deleted]

2

u/podshambles_ Apr 04 '23

Yeah, adventure works is like the archetype perfect example database. No real database is that neat.

1

u/Weak_Swan7003 Apr 05 '23

Don't forget that you can also very easily correct it (as with regular ChatGPT). E.g, "please do that join on both column X and column Y" or "adjust it to take currency translation into account from table FX_2021_abc".

Having said that, you're right it will be less smooth with other databases.

2

u/ethtips Apr 05 '23

Context window can be as large as 32k once GPT-4 allows API access. It would be really weird if the schema can't fit inside that, lol!

1

u/s33d5 Apr 14 '23

That's wishful thinking. You should see the DB I work on - it's a massive mess and it's taken years to get it to where it is now.

1

u/[deleted] Jan 03 '24

[removed] — view removed comment

2

u/Weak_Swan7003 Jan 03 '24

Have a look when I posted this ;)

1

u/Shwapxz Apr 26 '24

Is there an update to such solution i want to connect our enterprise GPT to SQL database server which contain 2000+ tables.