6

解放程序员双手!GPT-3自动生成SQL语句 | 代码开源

 3 years ago
source link: https://www.qbitai.com/2021/01/21424.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

作者赞叹:妙啊!

金磊 发自 凹非寺

量子位 报道 | 公众号 QbitAI

“无所不能”的GPT-3,现在又来 解放程序员们的双手 了。

像这样,只需用 简单的英文 问下GPT-3“上个月注册了多少个用户”。

GPT-3便会迅速作答,给出对应的 SQL语句

SELECT COUNT(*) FROM users

WHERE signup_time > now() – interval ‘1 month’

MBVR7j2.gif!mobile

更神奇的是,GPT-3是在不知道“数据库模式”的情况下,默认存在“users”这个表;以及在用户注册过程中,存在“signup_time”这样的字段。

相当的智能了!

就连开发者本人都连连惊叹道: 妙啊

更复杂的SQL语句,GPT-3 hold得住吗?

当然,刚才提到的提到的例子,只是非常简单的提问。

那当GPT-3面对更加复杂的SQL查询时,还能如此智能吗?

为此,作者给它“投喂”了更多的上下文,以及与“数据库模式”相关的信息:

输入一个问题,GPT-3需要回答正确的 PostgreSQL

数据库包含2个表,分别是“users”和“charges”。

“users”表包含的字段有:id (integer), signup_dt (timestamp), email (character varying)和 plan_type (character varying)。

“charges”表包含的字段有:amount (bigint), user_id (integer), 以及charge_dt (timestamp)。

OK,信息“投喂”完毕,现在来看下GPT-3的表现。

输入:

how much revenue did we have in the past 7 days?

GPT-3:

SELECT SUM(amount) FROM charges WHERE chargedt > now() – interval ‘7 days’

Qbmeeme.gif!mobile

不难看出,GPT-3正确地推断出了”amount” = “revenue”,并且知道为了回答问题,要用SUM()来汇总过去7天的费用。

看来GPT-3已经掌握了基本的 SQL语法

但作者也表示,GPT-3有的时候喜欢自己“发明”一些字段,所以现在就用“例子引发”的方式,让它变得更加智能。

先让GPT-3做些简单的例子实验,例如让它从“users”和“charges”2个表中,提取所有可用的字段。

基于这样的引导之后,再向GPT-3提问:

what was the largest payment amount made in the past 30 days?

GPT-3回答道:

SELECT MAX(amount) FROM charges WHERE charge_dt > NOW() – INTERVAL ‘30 days’

这个小实验的结果看着还是挺不错的,但当作者加大难度后, 问题还是出现了

作者提出了更加复杂的问题:

when did user with email [email protected] make his last payment?

这次GPT-3的回答是:

SELECT MAX(charge_dt) FROM charges WHERE user_id = 1 AND email = ‘[email protected]

很明显,在“charges”表中,并没有“email”这样的字段,而是存在于“users”表中。

也就是说,对于SQL经常用到的 联合查询 ,GPT-3还没有get到。

让GPT-3一步步学会“联合查询”

于是,作者还是通过“例子”的方式,一步一步让它“学”会联合查询。

输入:how many users signed up in the past 30 days?

GPT-3:

SELECT COUNT(*) FROM users WHERE signup_dt >= now() – interval ‘30 days’

输入:when did user with email [email protected] sign up?

GPT-3:

SELECT signup_dt FROM users WHERE email = ‘[email protected]

输入:how much revenue did we have in the past 7 days?

GPT-3:

SELECT SUM(amount) from charges WHERE charge_dt >= now() – interval ‘7 days’

输入:how much revenue did we have from 10-01-20 through 11-15-20?

GPT-3:

SELECT SUM(case when charge_dt>= ‘10-01-20’::date and charge_dt < ‘11-15-20’::date then amount else 0 end) as revenue

FROM charges

输入:how much revenue have we had from users that signed up in the last 6 months?

GPT-3:

SELECT SUM(charges.amount) FROM users INNER JOIN

charges ON users.id = charges.user_id

WHERE users.signup_dt>= now() – interval ‘6 months’

输入:when did user with email [email protected] make his first payment?

GPT-3:

SELECT MIN(charge_dt) as last_payment_dt from users INNER JOIN

charges ON users.id = charges.user_id

WHERE users.email = ‘[email protected]

输入:how many new users signed up in each of the last 2 months?

GPT-3:

SELECT sum(case when signup_dt>= now() – interval ‘1 month’ then 1 else 0 end) as signups_this_month,

sum(case when signup_dt>= now() – interval ‘2 months’ and signup_dt < now() – interval ‘1 month’ then 1 else 0 end) as signups_last_month

FROM users

在经过这样一番“学习”之后,回过头来,再去问GPT-3刚刚出错的问题:

how many payments has user with email [email protected] made?

这次它的回答是:

SELECT COUNT(*) FROM users INNER JOIN charges ON users.id = charges.user_id WHERE users.email = ‘[email protected]

瞧瞧!GPT-3学会“内联”了,很酷有木有!

当然,除了用“例子”的方式让GPT-3变得更加聪明外,作者表示还可以通过 调参 的方式。

同时作者还强调了一点,他发现,起码在“让GPT-3写SQL语句”这件事上, GPT-3 Instruct 要比泛化(generalized)的GPT-3引擎好用得多。

为什么要搞这个项目?

这个项目的作者是一位 分析师 ,目前就职于一家叫做SeekWell的公司。

他每天日常的工作,就是编写大量的SQL语句,来回答与业务相关的问题。

AvaEvyJ.jpg!mobile

这就让他萌生了 “自动化” 的想法。

与此同时,他也注意网友们用大火的GPT-3,做了各种各样有创意的项目。

例如自动生成HTML、CSS代码等等,于是他便操刀开始训练GPT-3生成SQL语句。

当然,效果也是让他震惊不已,在博客中也是连连发出“Cool”的声音。

……

最后,作者将这个项目的代码在GitHub中开源了,感兴趣的读者可戳下方链接。

参考链接:

https://blog.seekwell.io/gpt3

GitHub项目地址:

https://github.com/bkane1/gpt3-instruct-sandbox

版权所有,未经授权不得以任何形式转载及使用,违者必究。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK