#ChatGPTvàCoding #SửdụngChatGPTđểViếtCode #AIvàViếtCode
Tác giả David Gewirtz từ ZDNET đã phát hiện ra rằng ChatGPT có thể viết code khá tốt. Ông đã thử nghiệm điều này vào tháng Hai và yêu cầu ChatGPT viết một plugin WordPress mà vợ ông có thể sử dụng trên trang web của mình. Kết quả là ChatGPT đã làm rất tốt, nhưng đó là một dự án rất đơn giản. Vậy làm thế nào để sử dụng ChatGPT để viết code trong thực hành lập trình hàng ngày của bạn? Chúng tôi sẽ khám phá điều đó ở đây.
Có những loại mã nào mà ChatGPT có thể làm tốt? Điều quan trọng đầu tiên về ChatGPT và mã là nó có thể viết mã hữu ích. Điều thứ hai là nó có thể đi vào vòng lặp và tạo ra rác không thể sử dụng được. Vì vậy, nếu bạn yêu cầu ChatGPT tạo ra một ứng dụng hoàn chỉnh, điều đó sẽ khó thành công. Một phần mở rộng của quan sát này là nếu bạn một người không biết gì về mã hóa và muốn ChatGPT xây dựng cho bạn một cái gì đó, nó sẽ thất bại. ChatGPT thành công trong việc giúp một người đã biết cách viết mã để xây dựng các chương trình và hoàn thành các nhiệm vụ cụ thể.
Vì vậy, làm thế nào để sử dụng ChatGPT để viết mã? Bước đầu tiên là quyết định những gì bạn sẽ yêu cầu ChatGPT – nhưng chưa hỏi nó bất cứ điều gì. Quyết định những gì bạn muốn chức năng hoặc thủ tục của mình thực hiện, hoặc những gì bạn muốn học để tích hợp vào mã của mình. Quyết định các thông số mà bạn sẽ chuyển vào mã của mình và những gì bạn muốn đạt được. Sau đó, xem xét cách mô tả nó.
Hãy tưởng tượng rằng bạn đang trả tiền cho một lập trình viên để làm điều này. Bạn có cung cấp đủ thông tin cho người đó để họ có thể làm việc trên bài tập của bạn không? Hay bạn quá mơ hồ và người bạn trả tiền sẽ có xu hướng yêu cầu tối đa hoặc hoàn toàn không liên quan đến những gì bạn muốn? Hãy xem xét một ví dụ: Giả sử bạn muốn tóm tắt bất kỳ trang web nào. Bạn muốn cung cấp cho nó một URL trang web và nhận lại một bản tóm tắt ngắn gọn, được xem xét kỹ lưỡng và phù hợp. Ở đây là một bước mẫu đầu tiên: Hãy miêu tả mười thư viện trí tuệ nhân tạo mã nguồn mở khác nhau (và ngôn ngữ chúng hoạt động) mà tôi có thể sử dụng để tạo ra một bản tóm tắt của nội dung chủ đề trang web nào đó, bỏ qua bất kỳ quảng cáo hoặc tài liệu nhúng. Nó đã cho tôi chính xác những gì tôi muốn, bao gồm một đề cập đến các sản phẩm của OpenAI. Nhưng giả sử tôi không muốn trả tiền cho API. Vì vậy, hãy xem xét cách tương tác với ChatGPT để tìm hiểu cách sử dụng một công cụ như vậy, miễn phí, với một dự án chạy trong PHP.
Những bước cụ thể để sử dụng ChatGPT để viết code bao gồm: Quyết định những gì bạn muốn yêu cầu ChatGPT. Quyết định thông số mà bạn chuyển vào. Tìm kiếm các thư viện phù hợp để sử dụng. Liên hệ với ChatGPT để tìm kiếm các tài liệu hướng dẫn được sử dụng để tích hợp thư viện vào dự án của bạn. Và cuối cùng, có một cuộc trò chuyện với ChatGPT để đảm bảo rằng bạn hiểu được các bước để xây dựng chức năng của mình. Với các bước trên, bạn có thể sử dụng ChatGPT để viết code của mình một cách hiệu quả.
Nguồn: https://www.zdnet.com/article/how-to-use-chatgpt-to-write-code/#ftag=RSSbaffb68
One of the more intriguing discoveries about ChatGPT is that it can write pretty good code. I tested this out in February when I asked it to write a WordPress plugin my wife could use on her website. It did a fine job, but it was a very simple project.
How can you use ChatGPT to write code as part of your daily coding practice? That’s what we’re going to explore here.
What types of coding can ChatGPT do well?
There are two important facts about ChatGPT and coding. The first is that it can, in fact, write useful code. The second is that it can get completely lost, fall down the rabbit hole, chase its own tail, and produce absolutely unusable garbage.
Also: I’m using ChatGPT to help me fix code faster, but at what cost?
I found this out the hard way. After I finished the WordPress plugin, I decided to see how far ChatGPT could go. I wrote out a very careful prompt for a Mac application, including detailed descriptions of user interface elements, interactions, what would be provided in settings, how they would work, and so on. Then I fed it to ChatGPT.
ChatGPT responded with just a flood of text and code. Then it stopped mid-code. When I asked it to continue, it vomited out even more code and text. I requested continue after continue and it dumped out more and more code. But… none of it was usable. It didn’t identify where the code should go, how to construct the project, and — when I looked carefully at the code produced — it left out major operations I requested, leaving in simple text descriptions stating “program logic goes here.”
Also: Okay, so ChatGPT just debugged my code. For real.
After a bunch of repeated tests, it became clear to me that if you ask ChatGPT to deliver a complete application, it will fail. A corollary to this observation is that if you know nothing of coding and want ChatGPT to build you something, it will fail.
Where ChatGPT succeeds, and does so very well, is helping someone who already knows how to code to build specific routines and get specific tasks done. Don’t ask for an app that runs on the menu bar. But if you ask ChatGPT for a routine to put a menu on the menu bar, and then paste that into your project, it will go quite well.
Also: How to use ChatGPT to create an app
Also keep in mind that while it appears ChatGPT has a tremendous amount of domain-specific knowledge (and it often does) it lacks wisdom. As such, it may be able to write code, but it won’t be able to write code containing the nuances for very specific or complex problems that requires deep experience to understand.
Use ChatGPT to demo techniques, write small algorithms, and write subroutines. You can even get ChatGPT to help you break down a bigger project into chunks, and then you can ask it to help you code those chunks.
Also: How does ChatGPT work?
So, with that in mind, let’s look at some specific steps for how to use ChatGPT to write code.
How ChatGPT can help you write code
This first step is where you decide what you are going to ask of ChatGPT — but not yet ask it anything. Decide on what you want your function or routine to do, or what you want to learn about to incorporate into your code. Decide on the parameters you’re going to pass into your code and what you want to get out. And then look at how you’re going to describe it.
Also: How to write better ChatGPT prompts
Imagine you’re paying a human programmer to do this. Are you giving that person enough information to be able to work on your assignment? Or are you too vague and the person you’re paying is more likely to either ask questions or turn in something entirely unrelated to what you want?
Here’s an example. Let’s say I want to be able to summarize any web page. I want to feed it something like this article and get back a short summary that’s well-considered and appropriate. As my input, I’ll specify a web page URL. As my output, it’s a block of text with a summary.
Continuing with the example above, a very old school way of extracting web page data was to just find the text between HTML paragraph tags.
But with the rise of AI tools, it makes more sense to use an AI library to do an intelligent extract and summary. One of the places ChatGPT excels (and it’s also an area you can easily verify to avoid its authoritative-but-wrong behavior pattern) is finding libraries and resources.
Also: How to make ChatGPT provide sources and citations
OpenAI (the maker of ChatGPT) sells API access to the GPT-3 and GPT-4 engines that will do exactly what we want. But in the case of this example, let’s assume we don’t want to pay transaction fees.
So let’s look at interacting with ChatGPT to figure out how to use such a tool, for free, with a project that runs in PHP.
I started with a prompt that was designed to elicit information about what libraries would provide the functionality I wanted. A library (for those of you reading along who aren’t programmers) is a body of code a programmer can access that does a lot of the heavy lifting for a specific purpose. A big part of modern programming is finding and choosing the right libraries. So this is a good starting point.
Also: I used ChatGPT to write the same routine in 12 top programming languages. Here’s how it did
In this case, I’m looking at blocks of code written by other people that will summarize text. Here’s my first prompt:
Describe ten different open source AI libraries (and the languages they work with) that I can use to generate a summary of the main core contents of any web page, ignoring any ads or embedded materials.
This gave me exactly what I wanted, including a mention of OpenAI’s offerings. I think OpenAI would do great here, but for this hypothetical project, don’t want to budget for API fees. So I’ll narrow down the question.
Are any of these free?
ChatGPT hedged its bets with its answer. Here’s what it said: “Yes, all ten of these AI libraries are open source and free to use. However, some of them may have usage limits or require payment for access to additional features or resources.” So, based on that, I clarified my query.
Which of these libraries have no usage limits and don’t require any additional payment or licensing
Notice how this is very much a conversation. I don’t have to re-ask the originating question. I’m just drilling down in the same way I might if I had an expert at hand and was seeking clarification. In this case, ChatGPT gave me eight library choices, but none of them mentioned the PHP language that I was planning to code in. So, here’s the next prompt:
Of those 8 libraries, can I use any with PHP?
It returned three libraries, but I wasn’t sure about what each did. So another question:
What’s the difference between Sumy, Gensim, and NLTK?
I still wasn’t sure, so I clarified my use plan and then asked:
If I want to create summaries of web page news articles, which library would work better?
The answer I got was clear and promising: “Sumy is specifically designed for text summarization, which is the task of creating a summary that captures the most important information from a piece of text.” So, now it was time to see what was involved in using it with PHP. I asked my last question for this part of the project:
Can you explain how to use Sumy from PHP?
Feel free to play along on your computer and paste these prompts into your instance of ChatGPT. Notice that, in step 1, I decided what program module I was going to get help on. Then, in this step, I had a conversation with ChatGPT to decide what library to use and how to integrate it into my project.
Also: The best AI chatbots: ChatGPT and other interesting alternatives to try
That may not seem like programming, but I assure you it is. Programming isn’t just blasting lines of code onto a page. Programming is figuring out how to integrate all the various resources and systems together, and how to talk to all the various components of your solution. Here, ChatGPT helped me do that integration analysis.
By the way, I was curious whether Google’s Bard could help in the same way. Bard can’t actually write code, but it did give some extra insights into the planning aspect of programming over ChatGPT’s responses. So don’t hesitate to use multiple tools to triangulate on answers you want. Here’s that story: Bard vs. ChatGPT: Can Bard help you code? Since that article, Google added some coding capabilities to Bard. But they’re not all that great. Here, read about it: I tested Google Bard’s new coding skills. It didn’t go well.
Coding is next.
Okay, let’s take a full-stop here for a moment. This article is entitled “How to use ChatGPT to write code.” And it will but we’re really asking ChatGPT to write example code. Wait. What?
Also: What is GPT-4? Here’s everything you need to know
Let’s be clear. Unless you’re writing a very small function (like the line sorter/randomizer ChatGPT wrote for my wife), ChatGPT isn’t going to be able to write your final code. First, you’re going to have to maintain it. ChatGPT is terrible at modifying already-written code. Terrible, as in, it doesn’t do it. So to get new code, you have to ask ChatGPT to generate something new. As I found previously, even if your prompt is virtually identical, ChatGPT may change what it gives you in very unexpected way.
So, bottom line: ChatGPT can’t maintain your code, or even tweak it.
That means you have to do it yourself. As we know, the first draft of a piece of code is rarely the final code. So even if you were to expect ChatGPT to generate final code, it would really be a starting point, one where you need to take it to completion, integrate it into your bigger project, test it, refine it, debug it, and so on.
Also: I asked ChatGPT to write a short Star Trek episode. It actually succeeded
But that doesn’t mean the example code is worthless. Far from it. Let’s take a look at a prompt I wrote based on the project I described earlier. Here’s the first part:
Wite a PHP function called summarize_article.
As input, summarize_article will be passed a URL to an article on a news-related site like ZDNET.com or Reuters.com.
I’m telling ChatGPT the programming language it should use. I’m also telling it the input but, while doing so, providing two sites as samples to help ChatGPT understand the style of article. Honestly, I’m not sure ChatGPT didn’t ignore that bit of guidance. Next, I’ll tell it how to do the bulk of the work:
Inside summarize_article, retrieve the contents of the web page at the URL provided. Using the library Sumy from within PHP and any other libraries necessary, extract the main body of the article, ignoring any ads or embedded materials, and summarize it to approximately 50 words. Make sure the summary consists of complete sentences. You can go above the 50 words to finish the last sentence, if necessary.
This is very similar to how I’d instruct an employee. I’d want that person to know that they weren’t only restricted to Sumy. If they needed another tool, I wanted them to use it.
Also: Want to learn more about prompt engineering? This free course from OpenAI can help
I also specified an approximate number of words to create bounds for what I wanted as a summary. A later version of the routine might take that number as a parameter. I then ended by saying what I wanted as a result:
Once processing is complete, code summarize_article so it returns the summary in plain text.
The resulting code is pretty simple. ChatGPT did call on another library (Goose) to retrieve the article contents. It then passed that to Summy with a 50-word limit, and then returned the result. That’s it. But once the basics are written, it’s a mere matter of programming to go back in and add tweaks, customize what’s passed to the two libraries, and deliver the results.
One interesting point of note. ChatGPT created a sample call to the routine it wrote, using a URL from after 2021 (when ChatGPT’s dataset ends).
https://www.reuters.com/business/retail-consumer/teslas-musk-says-fremont-california-factory-may-be-sold-chip-shortage-bites-2022-03-18/
I checked that URL against both Reuters’ site and the Wayback Machine, and it doesn’t exist. ChatGPT just made it up.
FAQ
Does ChatGPT replace programmers?
Not now, or at least not yet. ChatGPT programs at the level of a talented first-year programming student, but it’s lazy (like that first-year student). It might reduce the need for very entry-level programmers, but at its current level, I think it will just make life easier for entry-level programmers (and even programmers with more experience) to write code and look up information. It’s definitely a time-saver, but there are few programming projects it can do on its own — at least now. In 2030? Who knows.
How do I get coding answers in ChatGPT?
Just ask it. You saw above how I used an interactive discussion dialog to narrow down the answers I wanted. When you’re working with ChatGPT, don’t expect one question to magically do all your work for you. But use ChatGPT as a helper and resource, and it will give you a lot of very helpful information. Of course, test that information — because, as John Schulman, a cofounder of OpenAI, says, “Our biggest concern was around factuality, because the model likes to fabricate things.”
What programming languages does ChatGPT know?
Most of them. I got very side-tracked trying this. I tested common modern languages, like PHP, Python, Java, Kotlin, Swift, C#, and more. But then I had it write code in obscure dark-ages languages like COBOL, Fortran, Forth, LISP, ALGOL, RPG (the report program generator, not the role-playing game), and even IBM/360 assembly language.
As the icing on the cake, I gave it this prompt:
Write a sequence that displays ‘Hello, world’ in ascii blinking lights on the front panel of a PDP 8/e
The PDP 8/e was my very first computer, and ChatGPT actually gave me instructions for toggling in a program using front panel switches. I was impressed, gleeful, and ever so slightly afraid.
Also: How to use ChatGPT to summarize a book, article, or research paper
So what’s the bottom line? Honestly, it’s that ChatGPT can be a very helpful tool. Just don’t ascribe superpowers to it. Yet.
You can follow my day-to-day project updates on social media. Be sure to follow me on Twitter at @DavidGewirtz, on Facebook at Facebook.com/DavidGewirtz, on Instagram at Instagram.com/DavidGewirtz, and on YouTube at YouTube.com/DavidGewirtzTV.