How to Integrate ChatGPT API with Google Docs – AI Content Writer

Hey, what’s up coders? Welcome to One Little Coder! I’m genuinely excited about this video where we are going to integrate GPT API with Google Docs. I would like to first show you the demo, how it works. I was so excited when this worked, and I’m going to show you step by step, starting from creating OpenAI token until having this on your Google doc, how to do it, what prompts to change, everything is going to be covered in this video, so I strongly suggest you to stick to the end of the video. If you have not subscribed to our Channel, please subscribe that I make a lot of do-it-yourself coding, programming, AI, ML related tutorials, it would mean a lot.

First of all, I would like to quickly show you the demo. Like, I’ve got a Google doc, I am going to delete everything, and I’m going to just simply say a topic. In my case, the topic could be, let’s say, artificial intelligence. Okay, I said artificial. Oh, let’s just type artificial intelligence. I’m going to go to this button called “Chargpt,” and I’m going to click “Generate Prompt.” It’s a very bad name, I just came up with that. When I click “Generate Prompt,” you can see that it is going to take a couple of seconds. After it has taken a couple of seconds, you can see that now I’ve got a fully written essay on artificial intelligence using GPT API. This is quite exciting, and I’m going to show you how to do that.

The first step is, you need OpenAI token. So, how do you create OpenAI token? All you have to do is go to your OpenAI account and then go to your account settings, and inside that, you would see a tab called API keys. And click API keys and click “Create New Secret Token.” Once you click “Create New Secret Token,” you are going to be greeted with this window where you need to copy the token. Just make sure that you copy it somewhere and keep it, otherwise, you might lose it. And again, like, if you are using the token, do not share it with anybody. If you are going to share it with anybody, make sure that you delete the tokens or revoke the token after you have used it or shown it, so that nobody can misuse your OpenAI account. Having said that, at this point, we have learned how to use, how to create OpenAI API key or OpenAI token.

The next thing that we need to do is, we need to learn how to create this tab or the button inside Google Docs. So, this is going to be done using something called an App Editor. So, I’m going to go to “Extensions,” and I’m going to click “Apps Script.” Once I click “Apps Script,” I’m going to get this particular window. So, this particular window where I’m going to paste the entire code. You don’t have to take notes of the code, I’m going to explain this code, but also, I’m going to share this code as a gist file so it’s easier for you to copy and paste. And I’m also going to tell you where all the places you need to make any change.

First of all, this is a JavaScript code, so if you know JavaScript, it’s quite easy. I’m not a guy who knows JavaScript, so it was a little hard for me to understand. But if you know JavaScript, you are in a much, much better position. Okay, first of all, what is this code doing? This code is helping you do two things. One, it is going to help you create this button here. And it is going to help you create this button here. Second, it is going to help you capture what you are highlighting. And third, it is going to make a call to the OpenAI API endpoint, which, in this case, is GPT endpoint. And finally, it’s going to paste the text as a new paragraph. This is what this code is doing.

Let’s go one by one. First, we are going to define our API key. I told you to copy the API key, if you remember that. API key is supposed to be pasted here. So, first paste the API key here. Once you have the API key here, next, you need to specify the model that you want to use. We’re going to use the GPT model. So, in this case, it’s a GPT model. You can use any model from OpenAI, but because GPT, of course, is the cheapest one at this point, with the best cheapest model, is GPT 3.5 Turbo. GPT 3.5 Turbo is what we are using here. After that, you need to now create a custom menu in Google Docs. How do we create a custom menu? This function helps us create a custom menu. This is the main menu level button, and this is the item. Now, any item that you add corresponds to a function. So, you can see the item that I’ve added in this menu, “Chat GPT,” is called “Generate Prompt.” And that corresponds to a function called “Generate Prompt.” Now, I can have multiple things. For example, right now, I have one button to generate an essay. So, what if I want product ideas? Okay, I’m going to add another one here. So, I’m going to add another item here, and I would say, “Generate Ideas,” and I’m going to call this “Generate IDs.” This is for you to understand how also you can extend this. Now, I have added a new item, and I have added “Generate Ideas.” Now, let me save this. And after I save this, I have to run this. When I run this, most likely, it does not show you an error. But when you go here and click here and click “Generate Ideas,” it doesn’t do anything. Why? Because the script function for “Generate Ideas” does not exist. So, now, our next duty is to create a new function, just like this, that can generate ideas. So, I’m going to paste it on top of this. Now, I’m going to call this “Generate Ideas.” The function name should be “Generate IDs.” Now, inside this function, what are we trying to do? We are first trying to get the selected text. You are going to get this selected text. So, just the selected text. I think instead of “Generate Ideas,” I should call it “Generate LinkedIn Post.” “LinkedIn Post,” but I’ll keep the function name “Generate Ideas.” But button, I’m calling it “Generate LinkedIn Post.” So, first, we are collecting the text. Okay, the text that we are highlighting. Next, we know what is the body. Now, this is where you are going to give the prompt. Once you have the text that you have already selected, next, you need to give the prompt. So, what is the prompt that you want? Like, in my case, I want, to help me write 5 LinkedIn posts on… So, this will say, for example, if I say “Artificial Intelligence” as title, it will, the prompt will look like this, “Help me write five LinkedIn posts on…” Or, I can say it, “Tweets,” five tweets, whatever you want to say, you can say that. This is the place where you are updating the prompt. First, you need to update your API key here. If you want to add a new item, you can add a new item here. And then you need to add a new function. And inside the function, this is where you need to update the prompt. Then you can play with the parameters like “Max Token.” “Max Token,” I think 4000 something, but I’ll just stick to 2016. Uh, the “Temperature,” which is, again, it will help you make Chat GPT from no creativity to extensive creativity, where you it might hallucinate a lot. So, you can play with the “Temperature.” I’m not focusing on the parameters here. If you are interested in knowing Chat GPT parameters, let me know. I can make a separate video about it. But this video is supposed to be focusing primarily on integrating Chat GPT API with Google Docs.

Now, the main thing, where you are going to make a request body. So, if you have not seen my Chat GPT API in Python, I have explained this in detail. So, whenever you have an API, you are going to make send a request. While you are sending a request, you’re going to send a payload. And the request is going to give you a response back. Now, for that, you need to make the request body. Where you are going to call the model name that you have already defined. And unlike any previous API, Chat GPD API requires it to be sent in a particular format, and that is a format here. So, we are simply saying the role is the user, because we are sending the message. And what is the content? That is a prompt here. Unlike the Chat GPT API Python tutorial where I said that we have to set a context, like the system. Here, we are not doing that. We are just letting it to be by default. And next is temperature, maximum tokens. That is the request body. Now, what is the request option? Is a POST request. Your API key goes here, “Authorization,” “Content-Type,” and send the request as a payload. Now, you are going to fetch using this endpoint. This endpoint is the Chat GPT’s latest endpoint. And get the response, parse the response, and get the actual place where the message is. And this is again something that we discussed in the previous video. If you have got a JSON object, you know, inside JSON, it looks like this. Your response, inside JSON, inside “choices,” the first one, inside that “message,” inside that “content.” That’s exactly what we are doing inside JSON. “Choices,” the first one, “message,” “content.” Now, get it. If you want to log it, log it. But you’re going to convert into string just in case if it is a JSON object. And then, finally, up into the paragraph just below that, like whatever you do, the paragraph. So now, this function is called “Generate Ideas.” So it’s technically supposed to generate LinkedIn post ideas. So I’m going to save this, and I’m going to click “Run” this. First time, very first time when you are saving this, Google is going to ask you to… Google is going to ask you to approve something. So it’s going to say, you know, this application is not secure, you have to approve it. Fine, it’s happening only inside your Google environment. You’re not sharing it with anybody, so it’s… It’s a default warning for third-party applications that are not signed by Google, so it’s completely fine. You can save, give all the approvals, and click “Run.” Once you click “Run,” it’s going to run the code. So the code is ready. Now, go back here. You don’t have to refresh. You can see that it is already there. So, “Generate Prompt,” “Generate LinkedIn Post.” So I can go and then come down. What do I want to generate LinkedIn post about? Um, maybe, “Beautiful Morning.” Does it… I don’t know if people write about beautiful morning on LinkedIn, but let’s try. “Chat GPT,” click “Generate Post.” And wait for a few seconds because it’s going to make a call, get it back, add it here. So it takes… Oh, it’s fast. It’s quite fast. “Good morning LinkedIn family. Waking up to a beautiful morning is a reminder that every day is a new opportunity to chase our dreams. Quite amazing.”

So, you have got this button where you can click. Let me show it for one more thing. Just one more thing. So, what do you want? Um, I want to say, “One Little Coder.” So, write a LinkedIn post about why you should subscribe to “One Little Coder,” who makes AI videos. Okay, let’s see what happens. Select the text. You have to select the text because that’s where it takes the text for the prompt. Go to “Chat GPT,” “Generate LinkedIn Post,” and let’s see what it is going to say. So, it’s going to say, “Are you interested in learning about the latest advancements in AI technology? Look no further than One Little Coder.” Nice. “Want to take your AI skills to the next level?” Nice. “Whether you are a seasoned AI professional or just starting out, a little good?” Okay, I’m so happy. I’m so happy that it is managed to generate. And again, this is five, it’s primarily because we have mentioned five here. So, you… This are certain things that you can play with. So, to quickly summarize what we have done in this video, we learned how to create OpenAI token, API key. Then, we learned this code. This code will be linked in the YouTube description. Definitely check it out, just below the “like” button, if you want to like. And then add the API key here. And then we have the code. This is the main menu name. This is place where you add new items. You can change anything that you want. And then, for every item that you add, you have to link it with the function. And this is where we create the function. Inside the function, you don’t have to change anything here. You have to change the prompt. So, based on what you want to get, this is something that you can also practice with Chat GPT, the web version of Chat GPT, the free one, Write the right prompt, get what works fine for you, get it, and then finally, you have everything ready. So, at this point, you will have successfully created or integrated Chat GPT inside your… Again, we are not using it for chat, that’s something for you to keep in mind, that we are leveraging the low cost of the APIs because it’s a two-dollar for one million tokens, something like that, so we’re going to use that so that we can save cost instead of using DaVinci. And also, it is quite good, like, everybody loves Chat GPT. So, basically, we have learned to integrate Chat GPT API inside Google Docs for us to use artificial intelligence to help us write content inside Google Docs. I hope this video was helpful to you in learning to integrate Chat GPT API within Google Docs. If you have any questions, let me know in the comment section. Otherwise, see you in another video. Happy prompting!

Privacy Policy | Privacy Policy