How to Use Bard to Write and Debug Code
In this video, I will show you how to use Bard, Google’s large language model, to write and debug code. Bard is a powerful tool that can be used to generate code in a variety of programming languages. It can also be used to debug code, find errors, and suggest improvements.
In this video, I will show you how to use Bard to write a simple Python program to generate cellular automata. I will also show you how to use Bard to debug the code and fix any errors.
By the end of this video, you will be able to use Bard to write and debug code in a variety of programming languages.
Video Transcript
New artificial intelligence large language models like ChatGPT and Google’s Bard are really great at taking a text prompt and then creating text from it. They are also really good at creating code. They do this by statistically predicting what the next set of characters is going to be based on the previous set. It’s actually quite remarkable that these neural networks are able to make such human like text although they are not perfect. Bard and ChatGPT make mistakes like most generative AI tools that are out today but one nice thing about Bard is that it can write code and then that code can be instantly run on Google Colab.
Let’s go ahead and try it out. If you haven’t used Bard before you need to go to bard.google.com and then you can enter a prompt. So let’s have it write some code. So we’ll write, we’re going to, “write a python code to generate cellular autonoma visually” Let’s see what Bard has to say about that.
So here Bard has generated some code and then right here we have a little pop-up that says, “When Bard generates Python code you can export it and test it in Google Colab.” So let’s click this button and then we’ll export to Colab. Then we can click this button and open it in Colab. Here we have our Python code automatically loaded in Colab and then we can run it. But you can see here’s an error. It has some error but it did reach what we wanted by making cellular autonomata visually. So what you can do is you can go back to Bard and you can say, you can ask Bard If the previous code was correct.
So it claims the code’s correct, but another nice thing about Bard is we can have three different versions. So unfortunately that’s only available for the last prompt. So here we can look at the other drafts of this prompt and on all of them it says the code is correct. Here it’s claiming that there is a YouTube of the animation it creates and that is fascinating.
So what we’re going to do is go ahead and grab this prompt from the top here, copy and then we can put the prompt back at the bottom. So now we have very similar code as before but remember we have different drafts so let’s try draft number two.
And then we’ll go ahead and Export this to Colab, and then open it in Colab, and then let’s see what happens. So here we definitely have errors. So we’ll try to, we’re going to try to fix the index out of range error in the code above.
So it claims that this is fixed code. So we’ll go ahead and find out. We’ll export the code from Bard to Google Colab and then we’ll open the Python code in Collab. And then we’ll run it. So now we don’t have any errors. So quite amazingly, Bard was able to fix its own code and now we don’t get any errors when we run the code and now we have our cellular autonomata depicted visually.
Hopefully this simple demonstration of how you can use Bard to write simple code, sometimes even correct the code with Bard, and then run that code in Google Collab will get you coding with Bard as well.