多年以来,编程一直是我生命最重要的快乐来源之一,我从没细想过这份快乐能伴随我多久。但就在几天前,在观摩了 ChatGPT 替我编写一个 Python 程序的全过程后,我突然有种强烈的感觉:不远的将来,人们能从编程中获得的乐趣可能会逐渐消失。
换句话说,ChatGPT 正在缓慢地“杀死”编程里的乐趣。在解释这个观点之前,让我先带你简单回顾一下:ChatGPT 到底帮我写了个什么程序。
用 ChatGPT 编写程序
先简单介绍一下 ChatGPT。ChatGPT 是人工智能公司 OpenAI 在 2022 年 11 月份推出的基于 GPT 自然语言生成模型的聊天机器人。它功能强大,能轻松完成许多在人类眼中非常复杂的任务,比方说:根据你描述的需求写一个可运行的程序。
前天晚上十一点左右,我正坐在床上刷手机,突然想起一个给路由器用的工具脚本,已经拖延了几个月都没动工。既然自己不愿写,不如这次试试让 ChatGPT 来帮我完成。
这个工具的需求简述如下:
接收一个 URL 地址作为参数,将该 URL 里的域名,以及该 URL 所返回的页面里的所有域名,一并转换成 dnsmasq 服务的配置条目,写入到 /etc/dnsmasq.conf 文件中(需要考虑重复写入)。
为了让 ChatGPT 写代码,我需要用自然语言清楚描述程序的用途和大概的执行流程,这个过程花了我 5 分钟左右。
需求描述如下:
Write a Python program, it accepts an URL address url from command line input, the program extract the domain name from url and stores it in a variable called src_domain, then it starts to request the given URL address and parse the response as HTML, it should find out all valid domain names in the response and stores them in a set called secondary_domains. Now print the src_domain and secondary_domains to inform user.
Let's start to update dnsmasq's config file, combile src_domain and secondary_domains into a big set called domains. Iterate over domains...(之后的内容省略)
将它发给 ChatGPT 后,我瞬间就收到了回复:一长串 Python 代码。
图:ChatGPT 聊天界面
其中一部分摘录如下:
def main():
# ...
# Extract the source and secondary domains from the HTML response
src_domains = {src_domain}
secondary_domains = extract_domains(html)
domains = src_domains | secondary_domains
domains = sorted(list(domains))
# Create the configuration entries for each domain
domain_configs = create_domain_configs(domains)
# Write the configuration entries to the dnsmasq config file
dnsmasq_conf_path = '/etc/dnsmasq.conf'
write_config_to_file(domain_configs, dnsmasq_conf_path)
# ...
分享说明:转发分享请注明出处。