• 2 days ago
Bring ChatGPT into your Jupyter Notebook workflow and get help with Python code without constantly switching between your notebook and the ChatGPT website. This walkthrough compares two approaches: sending prompts through the ChatGPT API from notebook cells, and using a Jupyter AI Assistant extension with tools to explain, debug, format, complete, and review code.

The API method requires your own API key and may incur usage charges; the extension’s voice-question feature also uses the paid Whisper API, while its other demonstrated tools are free. Explore practical examples, including debugging an error and generating code completions, in this hands-on guide to ChatGPT for Python programming. Useful for data analysts, students, and developers searching for a Jupyter Notebook AI coding assistant, Python debugging help, code explanations, or ways to use ChatGPT while coding.

Category

📚
Learning
Transcript
00:00Now, full disclosure, there's not a tutorial on how to do this. I'm not starting from scratch
00:03and building it all out. I'm just going to kind of show you what I've done and how to do
00:07it.
00:08But I do it in two different ways. The first way is using ChatGPT's API. So I just use my
00:13API key,
00:14use it within the cells in Jupyter Notebooks. And if I'm being honest, this is not the best way to
00:18do that. I'll show that to you in just a little bit. The second way you do it and the
00:22way that
00:22I recommend it is this extension that I found that integrates into your Jupyter Notebooks,
00:27and it is phenomenal. I just started using it about two weeks ago and I was like, whoa,
00:31this is really, really helpful. It's basically like having ChatGPT with you there when you're
00:36coding in Jupyter Notebooks. It's phenomenal. And I think it's really, really helpful. I just wanted
00:40to show it to you. So let's jump onto my screen. We'll take a look and we'll see how we
00:44can put
00:44ChatGPT into your Jupyter Notebooks. All right. So the first way that I want to show you how you can
00:49use ChatGPT in Jupyter Notebooks is by using the API key. Now you are not going to use this API
00:55key
00:56because I will go and delete it. You can try it if you want to. I'll have that on my
01:00GitHub where
01:01I post this so you can just copy this and try it yourself. But to actually install this, you need
01:05your own API key. So you just have to go to the OpenAI Account API Keys, create your own API.
01:12Of
01:12course, this does cost money, but I've used it quite a few times and it's cost me like six cents.
01:18So
01:18it's crazy cheap. But you go and you get your API key. You copy that. And then you go over
01:24here and
01:25we input it as our API key. We have this function that we created and then we prompt it. So
01:31we say
01:31prompt. What are the benefits of using renewable energy? And it gives us this output. Now, here's
01:37what I found about the API really quickly is that when you do it like this and you're using the
01:42API key
01:43like this in your Jupyter Notebook, it's extremely limited. It's actually not super even helpful.
01:49I would just go into Bing or chat GPT and use it there. So if I'm being honest, this isn't
01:54the best
01:54way to do it. I would probably, if I was trying to really make the most out of this, I
01:59would try to
02:00create some type of user interface or button or something that I can use on the side over here
02:07that would integrate maybe like a widget or something. This just isn't great. And this is what most
02:12people are going to do. This is what I tried to do when I was first doing it. And if
02:17you're having
02:17trouble, you may need to do pip install open AI in your Anaconda prompt. If you're using it through
02:23Anaconda, that's where I'm using it through. So just pip install, it installs it. And then of course
02:27you can use this exact code right here. Again, don't really recommend it. It's honestly not that great,
02:34but this is how you do it. Now, when I was going through all the research to find this,
02:39I found this right here. This is the ChapGPT Jupyter AI assistant. It is super great. And I
02:47found this and I was like, Hey, I need to make a video on this. Not for anything more than
02:50to just
02:51show it to you in case you use Jupyter notebooks in your work. This is a fantastic integration.
02:56And they even have all of the code for it in their GitHub. So you can go and you can
03:01literally clone
03:02this and create your own if you want to with any extra code you would like to do. But, you
03:09know,
03:09he walks through how he created it, what it does, everything in here. Now, really quickly,
03:13I'm just going to give you a quick demonstration of how it works. But when you install it, you come
03:18right here. You, I've already installed it, but you just select, you know, install or download.
03:23Then you refresh your Jupyter notebook. And then you get all these buttons up here, buttons like
03:29format, explain, debug, complete, review, question. And then there is a voice question. Now,
03:39all of these other functions are completely free. But if you go to the very bottom right here,
03:44it says that the voice command, you can ask it questions. This part does cost money. Actually,
03:50he talks about how much it costs right down, right up here, actually. It's the voice command. So it uses
03:55the whisper API and charges 0.006 per minute. So if you do use that, that costs money. But everything
04:04else is completely free, which is just, it's fantastic. So if we go back here, let's go down
04:10and let's say I have this error right here. For example, I can come in here and I can click
04:14debug. And if I click debug, right at the bottom, it starts prompting or creating this output for us.
04:21It's only at the very bottom, but it even says, look, there's an issue with your code. It looks
04:26like you needed a semicolon or a colon. So it corrects the code and you can just copy that,
04:31which is fantastic. Or you can come right over here and click copy. And you can then paste it into
04:37here and it'll work better. And actually that's an issue because I need to get rid of this. There we
04:42go. So that's how they get around that. Now it's free because they're using the chat GPT 3.5.
04:48They're not using the chat GPT 4, which would cost you money. And I believe you can actually
04:53upgrade that as well. When you're actually adding this, you do need an API key in order to use this.
04:59You just have to input it in case you do use the whisper API, then it charges you. But I've
05:05been
05:05using this quite a bit, just testing it out, running a bunch of, you know, tests and different stuff.
05:10And it's been working perfectly. Let's go back up really quickly. And let me get rid of this.
05:15I'm zoomed in. Let me get rid of this. And let's say I want to do something like format or
05:21let me do
05:23the complete. So I'll just do this right here. And let's say I wanted to do num times. I'm just
05:33going to get rid of this and then we'll do complete. So now it's going to say num2. And it's
05:38even going
05:38to give us different stuff. So it's going to do a list comprehension. So we didn't say do num to
05:44the
05:44power of two, but you know, I think it saw our last query right here and it's going to work
05:49perfect. So we're just going to copy this and we're going to paste it down here. And if we run
05:54this,
05:54you know, it gives us an output. So it changed this, which basically is nothing. This is hot
05:58garbage. And it changed it into a nice little, you know, query for whatever you're using it for.
06:04As you get more complex, of course, it's going to be more tough and it's going to have, you know,
06:08different options and it may get things wrong. But this is exactly working off what you would have
06:13gotten. If you just plug this into the chat GPT, you know, window or their website. So I thought
06:18this was just really, really cool. And if I click on this, I can even go over here. I can
06:21have it
06:22explain it to me. And now it's going to explain this code to me all within my browser, which again,
06:28I just really like, and it keeps it super simple. So, you know, they're using it using list
06:32comprehension, squared list, an element, blah, blah, blah, blah, blah. So it just explains the code to you.
06:37Now this isn't a full tutorial on how to use this. So I'm not going to show you every single
06:41thing.
06:41I just wanted to show you how you can use the API key, how it's not super useful. And this
06:47extension
06:47that I found that I've been using for a little while, and it's been working awesome. So wanted
06:52to show this to you, thought it would be helpful, thought it would be useful. If you were interested
06:55in this, I'm going to leave links to basically all of these in the description to where you can access
06:59your API key to the GitHub with this code that I don't really recommend to this extension and this
07:06GitHub repo as well. So I hope that this was helpful. If you use Jupyter Notebooks, I highly recommend
07:10downloading this extension. It's just super cool. If you like this video, be sure to like
07:14and subscribe below, and I will see you in the next video.
Reviews, news & tutorialsDiscover the Tech Corner

Recommended