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…
INTERESTED IN READING THE FULL ARTICLE?
CLICK HERE AND LEARN MORE ABOUT THE BENEFITS OF CHATGPT FOR YOUR WORK AS SOFTWARE ENGINEER!
Note: This article is written by Mayya Markova. Mayya is a skilled frontend consultant at Accedia with experience in developing web applications for various industries. Additionally, she is an avid team player and sports enthusiast.
Originally published at https://accedia.com on September 7, 2023.