ChatGPT is a large language model developed by OpenAI.

As a developer, ChatGPT makes you more productive as it is capable of code generation, debugging, and answering questions. With ChatGPT, you can write code faster, quickly generate tests, find and fix bugs and even explain what code written can do.

Integrating ChatGPT with Google Colab will help you reap all the above benefits in the Google Colab environment. This post describes how to get started with ChatGPT with Google Colab.

Benefits of Integrating ChatGPT With Google Colab

✅ You can get use both ChatGPT and Google Colab simultaneously without needing to switch between different tabs.

✅ It is free to use, at least at the time of writing.

✅ You debug, refactor, and generate code faster.

✅ It can explain code written in a notebook if there’s something you do not understand.

✅ It can add comments to your code to make it more descriptive.

How to Integrate ChatGPT With Google Colab

To integrate ChatGPT with Google Colab, you use an extension called ChatGPT for Google Colab. It is available for most popular browsers. I use Google Chrome, so that I will install it from the Chrome web store.

You can also install it from the Mozilla Web Store. The extension is free and open-source. The code is available on GitHub.

How to Use ChatGPT in Google Colab

After you have installed ChatGPT with Google Colab from your relevant web store or extension marketplace, open a Google Colab Notebook.

Screenshot of Google Colab notebook

To use the extension, hover your cursor over the cell, and a ChatGPT button will appear in the top center of the cell.

Screenshot showing ChatGPT button

When you press the button, this interface will open up at the bottom of the cell.

Screenshoot showing the ChatGPT prompt interface inside Google Colab

Using this interface, you can select a task you want ChatGPT to perform. Tasks you can select include Refactoring Code, Explaining Code, and Summarising Code. Selecting a task will generate a prompt to tell ChatGPT what to do. For example, if I select the option to refactor code, you can see the text in the prompt text box changed.

Screenshot showing the Refactor task select and the associated prompt that is generated

You can edit the prompt if you want to or type a new one altogether. When your prompt is ready, press the Submit button to send it to ChatGPT.

Next to the Submit button, there is a checkbox for including the code. When this checkbox is checked, the code in the cell will be sent along with the prompt.

Screenshow showing the output of sending the request to chatgpt

Great! ChatGPT now works natively within Google Colab without moving between different tabs.

Gotchas/Caveats

From this, you can see ChatGPT encapsulated the code into a function and called the function. It even explained what it did. This is great, but if you look closely at the description of changes made, you will notice two issues:

  1. It renamed the variables to more descriptive names (bullet item 2). It did not. It still uses a and b as variable names.
  2. ChatGPT also says it added comments to the code (bullet item 4), which it did not.

These are some of the present drawbacks of ChatGPT. That is, it isn’t always right. It makes mistakes, hallucinates, or produces erroneous code using functions that don’t exist.

It is important to double-check and verify the code generated is correct instead of just blindly copying and pasting code from ChatGPT.

Potential Use Cases

Despite these caveats, if you make sure to review the code generated by ChatGPT and take its explanations with a pinch of salt, you will find that it will make you a more productive developer. For me, this is how I find ChatGPT most useful:

❇️ Refactoring code – Rewrite code you have already written in a more concise, readable, and understandable manner.

❇️ Generating test cases – Writing tests is boring, yet they are so essential. With ChatGPT, you can provide a function, and it will generate test cases for the function based on the code you wrote.

❇️ Getting explanations for code – At times, another developer will write cryptic code that you can’t understand. ChatGPT will help explain what the code does. You can even ask questions about frameworks and libraries, saving you a trip to Google and Stack Overflow.

Final Words

ChatGPT is a huge productivity booster for developers, and using it effectively with Google Colab is helpful to coders. You may also want to read about how to use ChatGPT with Siri.