ChatGPT for coding: Empowering developers with intelligent assistance
ChatGPT (abbr. Chat Generative Pre-Trained Transformer) is an artificial intelligence (AI) chatbot. In general, this means that we can ask it any question, and it will generate an answer for us. Super straightforward, isn’t it? Type your text and receive the information you need!
Today everyone talks about ChatGPT. As we at Accedia are constantly on the wave of new technology, we were among the first to try out its capabilities when assisting in the development process.
ChatGPT was created by OpenAI, an American AI research laboratory, and released at the end of 2022. Within only five days, over one million users had tried it! But what was the motivation of the team to create such a thing? OpenAI states that its mission is to “ensure that artificial general intelligence benefits all of humanity”.
In this article, our main goal is to answer a few questions: What are the advantages and disadvantages of the most popular chatbot and how can we, as developers, use ChatGPT for coding in our professional lives? Still, it's important to note that to safeguard the confidentiality and security of your client's information, it is essential to exercise caution and avoid sharing any sensitive data during your interactions with ChatGPT.
Why is ChatGPT so popular?
Generating answers itself sounds great! But let’s have a look at some of the other advantages of ChatGPT:
Helpful and easy to use
As we previously mentioned, the chatbot is designed to be incredibly convenient, which makes it easy to use. Its user interface, which looks like a messaging application, ensures a seamless experience, making it accessible and enjoyable for everyone.
Fast responses that save time when we need a quick answer
The chatbot is a real-time-saver. It provides swift answers to users’ requests, ensuring they can access the information they need. What about the delay? There is almost none of it.
Contributes with creativity and generates inspiration for ideas
You have a task to think of an inspirational quote and add it as a slogan to the website, you are developing. But you have no idea what to write. A very, very basic example, but all of us can think of a similar scenario in his professional life. ChatGPT to the rescue!
Has a huge knowledge base
The assistant tool covers a wide range of topics. It provides solutions to any kind of problem and the responses usually are very detailed. This makes it the perfect place to find an extensive summary of the information you need, without browsing the internet. ChatGPT also remembers previous interactions. This is super helpful if you want to find something you have searched for earlier.
Allows follow-up corrections
You didn’t like the response? There are two options here. You can regenerate the answer, or you can just type what you think is wrong and the chatbot will correct itself with the next answer.
Declines inappropriate requests
If someone asks for information that involves topics violating usage policies, such as illegal activities, the AI will refuse to answer the prompt. It follows strict ethical guidelines and ensures a positive and safe experience for all users.
It sounds too good to be true?
Sounds perfect, right? AI changes the world and even the impossible now looks possible. Unfortunately, everything comes with some limitations. Here are some of the ChatGPT ones:
Offers potential security risks
When you interact with ChatGPT, your input is sent to external servers for processing. This could potentially expose sensitive information if the input contains personal, confidential, or proprietary data. ChatGPT can be misused for malicious purposes as well - generating misleading information, phishing attempts, spam, or even content that manipulates the users, promotes violence, or illegal activities
Cannot manage multiple tasks together
ChatGPT performs best when it’s given a single task or objective to focus on. However, if presented with multiple tasks simultaneously, it may encounter difficulty in prioritizing them, resulting in diminished effectiveness and accuracy.
Cannot problem-solve
These kinds of AI systems primarily rely on pre-programmed responses or pattern recognition to handle user queries, limiting their capacity to creatively address complex issues. For problem-solving capabilities, human intervention and advanced AI algorithms are still necessary to overcome this limitation.
Cannot be precise in the specialized topics
ChatGPT faces challenges with in-depth topics, especially when dealing with intricate or specialized data that demand high accuracy and precision.
The answers sometimes include grammar errors and typos
This issue is obvious especially when writing in languages different from English. However, the chatbot continuously learns from user interactions to improve its performance over time.
How can developers use ChatGPT for coding?
Having the advantages and disadvantages in our minds let’s think how we, developers, can use ChatGPT in our projects. With careful consideration and customization, we can find useful information in a few seconds, build content generators, add creativity to our applications, and much more. Let’s talk about more specific usages, that potentially can be beneficial for developers.
Integrate with the ChatGPT API in your project
The ChatGPT API is a powerful tool that enables you to integrate ChatGPT programmatically into your applications, products, or services. There is a wide range of possible use cases. Let’s explore some examples.
Usage of ChatGPT for coding
- Chatbots and virtual assistants
You need to create an interactive chatbot or a virtual assistant? The API might be the easiest solution for you. A perfect example of this is if you have some sort of customer support in your application. You can enhance the systems by integrating ChatGPT to provide instant, natural, and meaningful responses to user queries or offer personalized assistance.
- Content generation
You can use ChatGPT to generate creative and informative content for articles, product descriptions, stories, and more. Here is an example – imagine you have a website for generating personal CVs. Writing a small summary might be difficult for some users. It would be great for them to have a button that can generate that summary just by filling in their experience and skills.
- Writing and editing tools
ChatGPT can be used in writing and editing tools to receive suggestions, improvements, or even complete drafts for various types of writing, like emails.
- Language translations
By using ChatGPT’s language understanding capabilities, you can create language translation tools, that can convert text between different languages.
- Educational applications
You can develop interactive learning platforms that offer personalized tutoring, explanations, and responses. One of my personal projects is an application for taking online quizzes. My plan is to extend it by integrating it with the ChatGPT API. How? Wouldn’t it be great if when reviewing the results, there is a button next to the wrong answers, that when clicked, provides detailed information about the correct answer?
- Gaming
You are implementing a game? Integrate ChatGPT into it to provide engaging and dynamic in-game conversations and interactions.
- Text-based RPGs
ChatGPT can be used to design a text-based role-playing game that offers storytelling experiences with interactive characters. I was a mentor at a hackathon a few months ago. One of the teams was using the API exactly for this and I was impressed, their application was great and it was one of the favorites for winning the big prize.
Ways for interaction
Lots of usages! Let’s see how we can start using the API in our projects. Of course, like any other API, first you need to generate your personal API key. Then, you need to choose one of the possible ways for interaction.
- Interaction through HTTP requests
This is the most generic option. It can be used in any language. Do you remember my idea of using the API in one of my personal projects? Probably, I will choose this approach. In my opinion, it's the best one.
Here is an example of an HTTP request, fired directly from the Terminal:
And here is an example of the JSON response:
- Interaction through the official Node.js package
The OpenAI team has created a package for Node.js applications. It’s called openai. The installation is super straightforward. You just need to install it via npm or yarn:
or
Then, add the configurations (your API key):
And simply to make requests via the Node.js OpenAiApi instance:
An important note here! The package is recommended for server-side usage only, as using it in client-side browser code will expose your secret API key!
The official repository can be found here.
- Interaction through the official Python bindings
Again, the installation is super. You can install the official Python bindings by running the following command:
Like the Node.js package, this library needs to be configured with the API key you have. You can set it as an environment variable:
The other option is to define it in the code:
Then, the usage is similar:
You can check out the official repository of the package.
- Interaction through community-maintained libraries
If your project is not written in Node.js or Python, or for some reason, you don’t want to send direct HTTP requests, this option is for you. These libraries are built and maintained by other developers. The main idea is the same – create an instance of a client by providing your API key and use that client for sending requests and receiving responses. Currently, there are available libraries for C#/.NET, C++, Java, Golang, PHP, Ruby, Rust, etc. The full list of options can be found here. You can also access the full documentation of the ChatGPT API.
Find answers to technical questions
A common usage of ChatGPT is learning. As we previously described, the knowledge base of the chatbot is massive. We can find a quick answer to a question that had appeared, without browsing for hours and reading tons and tons of articles.
Here is an example - we asked ChatGPT “What is ECMAScript 2015?” and here is what it generated for us:
A great answer! It includes a description of what exactly ECMAScript 2015 is and a list of its features. Enough information to learn something you didn’t know earlier.
Do you remember one of the cons of the chatbot? Its knowledge is still limited to 2021. So, if you want to learn something that happened or is released after that, unfortunately, it can't help. When we asked, “What are the features of ECMAScript 2023?” the answer was:
If we read the answer again, we can’t deny that it isn’t that bad at all! We are informed that the answer might not be accurate, where can we get up-to-date information and a suggestion on where we can subscribe to get the latest updates?
Provide an idea of a small independent function
We have tested if the chatbot is good enough for generating code. If you remember, ChatGPT cannot problem solve, so it cannot find a solution for a project-specific issue. But it could be useful for small, independent functions. We decided to ask for a small function that finds the average sum from an array of numbers. The exact question was “Could you generate a function in JavaScript that can find the average sum from an array of numbers?” here is the output:
Impressive! Another good answer! The naming conventions are followed, error handling is included, which most developers might miss at the beginning, and also, explanations are provided.
Personally, I’d prefer to use the reduce method for the iterations and calculations, however, the answer is correct.
In my professional experience, I had several situations where I had to create technical lectures. Sometimes it’s difficult to come up with a good example for a given topic, and after I saw the output here, probably next time I would ask ChatGPT for an idea.
Generating documentation
Generating documentation for code snippets is a task that requires a deep understanding of programming languages, code structure, and context. ChatGPT is a powerful language model capable of generating human-like text, but it is not specifically designed for writing documentation. However, it might be able to provide some helpful general explanations or descriptions for code snippets.
It is continuously evolving, and newer versions may have improved capabilities, for generating code documentation. Of course, specialized tools, like Doxygen e.g., remain the preferred approach, but if you want to use the chatbot for this, just review the output to ensure correct results.
We asked ChatGPT to write the documentation for our generated JavaScript function from earlier. The exact question was “Could you generate documentation for the findAverageSum function? We use JSDoc.” Here is the output:
Nothing to complain about. The documentation was correctly generated, and the answer follows the JSDoc conventions. Again, additional explanations are included. Of course, the example is simple enough. For more complex methods the answer might not be as accurate as wanted. However, a good answer might be provided, that could help you or give you an idea of how to write the documentation.
Writing unit tests
Developers typically write unit tests manually, specifying different test cases and comparing actual results against expected outcomes. The one who wrote a specific function is the one who can figure out the best scenarios to test. But writing unit tests sometimes is tedious. Or even worse – you have no idea how to write them because you are new to the project and have never used the technology, which the team uses.
ChatGPT might help in some way. Let’s try to generate a test for our super simple findAverageSum function. What we asked the chatbot was “Could you write unit tests for the findAverageSum function by using Jest?” and here is the output:
The generated answer once again is very good. First of all, there is a lot of useful information. We specified that a requirement was to use Jest and ChatGPT told us what we needed to do – how to install it, how we should name the file for the tests, and how to run the tests.
Yes, it didn’t include information for additional Jest configurations, but the core things were explained very well. Also, the answer provides a few examples of how the unit test should look and what is the specific syntax for that particular technology. And last, but not least, it provided a few test scenarios, which almost everyone would admit sometimes is difficult to do.
Propose creative styles
Yes, that is right – when using ChatGPT for coding you can generate HTML with styles. As a language model, ChatGPT can understand and generate HTML markup and CSS styles based on the input it receives. That’s not all – by providing the necessary instructions and details, you can prompt the chatbot to produce HTML code along with CSS styles.
For example, we provided the following prompt: “How can I create a button, using JavaScript and HTML? When clicking on it, it must open an alert with the text "Hi". The button should be pretty”. Here is the generated answer:
Here is a link to CodeSandbox, where you can interact with the generated code.
Excellent! We have everything we want – a clickable, styled button! Keep in mind that while ChatGPT can assist in generating HTML and CSS, the quality and accuracy of the code may vary. Especially when the instructions grow, and the output should be more complex. It's always good practice, and I would say it’s required to review and fine-tune the code produced by the language model to ensure it meets your specific requirements and follows best practices for web development.
And again, would I try this? Well, as a web developer, who often has to implement small, educational demos, I think that this ability of ChatGPT is great. I’d try it next time in order to save some time from styling and to spend the earned time on the logic.
Conclusion
ChatGPT is a powerful assistant tool, which will continue to evolve in the future for sure. There are many aspects, which could be improved and the major one is the ability to access the internet.
How can ChatGPT for coding benefit developers? Well, if we need a fast answer to a question or an example of a given topic, or just to learn something new, then definitely yes, we can use ChatGPT for that. Should we use ChatGPT for coding? Well... It can generate an example or a possible solution, but it cannot understand your teammates' code conventions, the business logic of your project, or the possible issues that the generated code can introduce. Use the chatbot wisely, check the answers it generates, when you need up-to-date information, and when it comes to programming, make sure it will not introduce more trouble than positives.
And if you have any questions regarding the usge of ChatGPT for coding, don't hesitate to reach out!