Skip to main content

ChatGPT: 3 productivity tips for IT professionals

ChatGPT can help you write better code, faster and expedite problem solving, but you need to be aware of its limitations.
Image
Robots overwhelmed by parts (graffiti art)

Image by MMT from Pixabay

IT professionals continually face the challenge of writing efficient, reliable, and secure code. It can be daunting, especially when working on a complex project or dealing with legacy code. Many people are experimenting with ChatGPT and other generative artificial intelligence (AI) to help them write better code.

[ Read Can ChatGPT write Ansible playbooks that work? ]

ChatGPT is a large language model (LLM) developed by OpenAI that can generate natural language responses to user queries. It has been trained on vast amounts of text data, including books, articles, and websites, and provide relevant information on a wide range of topics, but it's far from perfect. If you're interested in using ChatGPT to improve your code, you must be aware of its strengths and weaknesses and independently verify everything it says.

That said, ChatGPT has a lot of potential value for IT architects and other professionals. Here are a few ways you can use it, as well as some of its limitations.

Do X in Y

One of my favorite ChatGPT uses is asking it how to perform certain tasks in a given context. This has proven extremely powerful within complex contexts. For example, suppose someone asks you to implement role-based access control (RBAC) for a GraphQL API, which is implemented in Golang using gqlgen.

If you do not know these technologies, you would have to go from website to website, video to video, learning about concepts such as authentication, GraphQL resolvers, and how gqlgen works in Golang. With ChatGPT, you can provide it the relevant information and then ask it how to accomplish what you're looking to do.

[ Check out Red Hat Portfolio Architecture Center for a wide variety of reference architectures you can use. ]

In this example, I gave ChatGPT the GraphQL schema so that it knows what entities exist, told it what technology I'm using–Golang with gqlgen for the GraphQL server–and what I want to achieve. Here's the prompt:

Suppose I have the following GraphQL schema:

```graphql
<contents of my schema.graphql file>
```

Using Golang and gqlgen, how can I implement RBAC so that there is a `login` mutation and only authenticated users are able to access the `reviews` query?

Given this information, ChatGPT tells me how to implement my request, including sample code I can use.

[ Read 9 ways ChatGPT will help CIOs  and 6 precautions to consider. ]

The following images chronicle the communication process with ChatGPT.

The first four steps (shown in the following image) are:

  1. Define a User struct to represent authenticated users.
  2. Define a ContextKey to store the user information in the context.
  3. Create a middleware function to check the user's authentication status and add that to the context.
  4. Modify the createReview mutation to require the authentication by adding @auth(requires: AUTHENTICATED).
Image
ChatGPT responses
(Oleg Silkin, CC BY-SA 4.0)

The next two steps (shown in the following image) are:

  1. Define a custom AuthDirective struct that implements the Auth directive to check for the requestor's authentication status.
  2. Apply the @auth(requires: AUTHORIZED) directive to the reviews query.
Image
ChatGPT responses
(Oleg Silkin, CC BY-SA 4.0)

The following two steps are:

  1. Add a login mutation to the GraphQL schema, which returns a JWT token.
  2. Implement the login mutation resolver to authenticate the user and provide them with a JWT. ChatGPT doesn't finish step 8 fully. It stops at func after writing the Login result.
Image
ChatGPT responses
(Oleg Silkin, CC BY-SA 4.0)
  1. ChatGPT says (in the next image) to create an AuthMiddleware function, which adds the JWT token to the authorization header of requests that require authentication.
Image
ChatGPT responses
(Oleg Silkin, CC BY-SA 4.0)
  1. Step 10 modifies the authenticateUser function from earlier to check for the JWT in the authorization header and validate the token.
Image
ChatGPT responses
(Oleg Silkin, CC BY-SA 4.0)
  1. Finally, in step 11, the generateToken function is modified to create a JWT containing the user's ID and email.
Image
ChatGPT responses
(Oleg Silkin, CC BY-SA 4.0)

With the right set of prompts (and a bit of patience), ChatGPT can help you write code to perform tasks, even in programming languages that are new to you.

Expedite troubleshooting and problem-solving

When you're working on a code problem and get stuck, ChatGPT can help you troubleshoot and solve the issue. You can describe the problem to ChatGPT, and it will provide you with possible solutions or point you in the right direction. It can save you valuable time and help you avoid common mistakes.

I recently had an error with some tooling I was using within a monorepo. Debugging it meant reading through a lengthy log file, which I didn't have time for. Instead, I simply described what I was doing and provided the error log to ChatGPT.

The following image shows a prompt that says that I'm updating my package.json file in my local backstage repository and getting an error. Then the entire error output is provided and truncated.

Image
ChatGPT troubleshooting prompt 1
(Oleg Silkin, CC BY-SA 4.0)

As shown in the following image, ChatGPT responds suggesting that I see the error because the verify-lockfile-duplicates script is failing, and offers some potential fixes, including running yarn install or yarn why {package}.

Image
ChatGPT troubleshooting response 1
(Oleg Silkin, CC BY-SA 4.0)

ChatGPT doesn't magically have all the answers, and as with any other conversation, it should be a two-way street. So I followed its instructions and posted the results again, this time with more progress.

The following image shows my prompt (truncated). It says that I ran yarn install and the command failed. I provided the output with a log attached.

Image
ChatGPT troubleshooting prompt 2
(Oleg Silkin, CC BY-SA 4.0)

The following image shows ChatGPT's response. It tells me that the command fails because the cairo package isn't installed. Then it lists a few ways I could install the package, including using the Homebrew package manager with the brew install cairo command.

Image
ChatGPT troubleshooting response 2
(Oleg Silkin, CC BY-SA 4.0)

This solution fixed my problem. No grep or reading was needed!

Be aware of ChatGPT limitations

While ChatGPT can be extremely useful for engineering, it doesn't have the most recent information because of how long it takes to train. ChatGPT-3 is the version that's been available to most users since November 2022, and its information cutoff date is sometime around September 2021. This means that performing tasks that rely on the most recent software features requires some maneuvering in prompt design and what you ask for.

For example, if you're using TailwindCSS version 3.0, which was released in 2021 and introduced a massive list of new features, ChatGPT-3 cannot help you, as it only knows up to TailwindCSS version 2.2.4.

[ Read 6 precautions for CIOs to consider about ChatGPT. ]

The following image shows ChatGPT being asked the most up-to-date version of TailwindCSS. It responds with 2.2.4 and indicates that its knowledge cutoff date was September 2021.

Image
ChatGPT limitations
(Oleg Silkin, CC BY-SA 4.0)

Another limitation of ChatGPT is the amount of information it can process at a time. ChatGPT uses the text-davinci-003 InstructGPT model under the hood, which is limited to around 4,000 tokens per request, or about 12,000-16,000 characters. While this is more than enough for most queries, it also means you can't simply paste a file with hundreds or thousands of lines of code and be able to produce a meaningful answer from it. One workaround is to identify what you want ChatGPT to do for you and provide it with the relevant information.

Although ChatGPT lacks the most relevant information, it's often very confident in its incorrectness. Here's an example I saw in a recent video. If you ask ChatGPT or AI pair programmer GitHub Copilot to implement Quicksort, it will produce the result in the screenshot below.

This screenshot shows ChatGPT being asked to implement Quicksort in TypeScript. It returns a function that does everything as expected, except the return value combines arrays recursively by calling return [...quicksort(left), pivot, …quicksort(right)].

Image
ChatGPT limitations
(Oleg Silkin, CC BY-SA 4.0)

This implementation of Quicksort is logically sound; however, the return value recursively calls itself on the left and right array and then creates a new array from the results. This call causes Quicksort to have O(n ∙ log n) memory complexity, which is a huge issue since Quicksort is supposed to have O(1) memory complexity.

While this is something OpenAI is working to fix, it's an inevitable problem with LLMs. Therefore, it's extremely important to always verify what ChatGPT responds with and carefully review its code output before blindly executing it. Always read through your code, make sure you know exactly what it's doing, and be skeptical of its results.

Conclusion

ChatGPT is a powerful tool that can help you in your work. By leveraging its capabilities while accounting for its limitations, you can learn new concepts, troubleshoot problems, and improve your communication skills. So next time you're stuck on a coding problem or need to explain a complex concept, try ChatGPT.

Topics:   Artificial intelligence   Programming  
Author’s photo

Oleg Silkin

Oleg currently works as a Software Engineer within the Emerging Technologies Department in the Office of the CTO at Red Hat. His work currently spans several project areas, including AI tooling, Platform development, and experimental storage systems. He also loves full-stack development, and gets More about me

Navigate the shifting technology landscape. Read An architect's guide to multicloud infrastructure.

OUR BEST CONTENT, DELIVERED TO YOUR INBOX

Privacy Statement