Transcript for:
Automate Google Docs Summarization with OpenAI

hey folks welcome back to another video uh recently I started a new series called open Ai and Google workspace where we saw how we can use the openai API and Google app script to do something crazy we can use the power of child CPT instead of Google workspace tools such as Google Sheets and uh we even did some blog summaries for that in this video uh we're going to be doing something interesting recently um I was facing some issues of generating summaries for some paragraphs in my Google Docs I would have to manually copy the entire paragraph put it into chat GPT and tell it to give me a short summary of it but what if I told you we can use the open AI API and use Google apps script and we can automatically get subarues instead of the Google Doc so in this video I'm going to be showing you how we can do that so let's switch to the screen here um inside this we are in the platform.openapi.com I am getting the API Keys section in the previous videos I've shown you how we can grab the API keys and how to create accounts and that so I'll be leaving that link in the description you can check that out so I'm going to start out by creating a new secret key I'm going to name this as let's just say Google Docs summary key click on create new super key examine secret key and I'm just going to copy this once we are done I'm going to switch to my Google doc and this Google doc right now contains a paragraph here that I want to generate a summary for um if you want to work with the Google Doc and the Google apps code I'll be leaving that link in the description you can check that out so first off we'll go to our extensions and we'll open up the app script editor so we're going to start out by creating a new function called on open and this function is going to be will always run whenever I open the Google Doc so I want to create a custom menu for that so I can specify the paragraph that I want to generate a summary for and it can automatically run the code so what we'll be doing is I'll just do where UI inside this I'm going to be opening up the document app and I'm getting get the UI here and after this I'm going to be creating a menu here so I'm going to do UI dot create menu and inside of this I'm going to name this as custom menu and after this I'm going to be adding something called an add item and inside this I want to say uh let's just say summarize selected paragraph so what the add-on is supposed to do or this extension or something has to do is whenever I select this and I go to my menu and I clicks on generate summary it should generate a summary for the paragraph that I selected so we'll be doing something very cool with that and um after this I just want to specify the function that I want to run here so I'm going to name it as summary summary log or summary talk that's the function name and after this I'm just going to do an add to UI that is it's going to be adding this custom function I mean this menu to the Google Doc so if I run the code now and I just go ahead here once it says I just click on run here you can see the execution started and the execution is complete if I go back to my Google doc and see custom menu and summarize selected paragraph for now it won't do because there is no function yet so let's start coding for the function so first I'm going to start by declaring the API key here I'm going to paste in the API key that we just copied here after this I'm going to be creating a new function called summary Doc and inside this I'm going to passing the paragraph As a parameter here this up here after this first the first step is going to be getting the model ID model so I'm just going to put in the model name here that's going to be text DaVinci zero zero two to know more about models I'll be leaving the open EI API documentation in the description you can check that out after this tokens that I want for this and the max tokens I'm going to keep them as 200 for this one since the summary Can Depend according to what how big the paragraph is or how how do you want your summary to be do you want to be short summary or detailed summary and stuff and afterwards I'm going to be declaring the temperature so this is going to be checking the X the accuracy so temperature basically checks for the accuracy of the response that is given by child CPT after this I'm going to be declaring the payload that we're going to be passing in the API and first off we'll be doing the model here so inside this I'm passing the model ID after this is going to be the prompt so this is going to be something interesting that we're going to be designing so the prompt is going to be please generate a summary for or maybe please generate a summary four and after this I'm going to be adding a slash n here and after this we're going to be adding in the paragraph parameter that we accept from the Google Doc once we add the prompt that we have the next thing that we're going to be doing is we'll be adding the temperature it's going to be the accuracy that it gives so I'll be passing the temperature variable here after this we're going to be adding the max tokens here so inside the max tokens I'm going to be passing Max token as a parameter and after this I'll be adding some other details to this such as the presence penalty and stuff like that and the frequency penalty you need to worry about that if you want to read more I'll be leaving the open air documentation in the description after this we're going to be declaring something called as well options and inside this we're going to be specifying the method so the method is going to be a post method and after this post right after we are done with this one uh we will be moving forward to adding the headers for this so for the headers we have to specify the content type followed by the authorization and then we need to make the payload accept a Json one so we need to stringify that and here you can see I've entered the content type that's going to be application.json after that the authorization is going to be the bearer and the API key that we just declared about here and the payload and then the json.string file once we are done with this um the next step is going to be having the response I'm going to do a URL fetch app dot uh fetch inside this I'm going to be adding the API endpoint that is going to be open aiapi.com so I'm just going to pass that here and I'm going to be passing the options also as a parameter here and in return we're going to be getting this summary right once we fetch it so under Json dot pass and I want to do response dot get content text and then after this um I just want to return a summary to our choices dot zero dot print we've got X suppose it's going to dot X and then dot print so what we are doing here in the summary dot choices bracket 0 1 is uh opening the chart GPT might give maybe say three four different responses uh when you pass the paragraph to get a summary right so we wanted to select the first summary that it will generate out of it it gives you different different drafts and different different versions of it so we want the first version of it so now that we are done with this part of the code let's move on to accessing the paragraph from the Google Doc so here I'm going to be saying summarize so here um summary doc function is there and we just need to add this as access dock and this function is going to be accessing the dock here oops to copy this function here and we paste this function here and inside this we get the summary as a parameter cut so here inside this function when we open this up we move on to getting the selection that is going to be there so I'm going to be doing document app dot get active document and inside this I want to do Dot the get selection so after we do the get selection function the next thing is going to be getting the text so I'm going to be creating a new variable called text and selection dot get range elements and I want it to be the first one again and after this I want to get the element and after that once we get the specific element the next thing is going to be getting the text out okay get text here [Music] and after we get this I'm going to be doing variable uh summary here and inside this variable summary I'm going to be getting I'm going to be taking the function that is there up here there's going to be summary dock I'll be passing this here and I'm going to be adding the text as a parameter here after we move forward once we are done once we get the summary back from the open AI I mean we get the summary back from chartgpt we want to do a document app dot get active document again and we want to get the body and after this I want to append a paragraph oops here and inside this paragraph I'm going to be first name summary um of the or we can just keep it as summary here and after this I want to paste in the data again so it's going to document app dot get active document and after this I want to do again a get body and after this get body I want to append the paragraph append the paragraph and inside this I'm going to be passing the summary variable that we just scored go ahead and click on Save and our code is ready and good to go we have everything accessed and ready so I'm going to click on the Run open the on open function oops we click on run open and we have to give access to the um to the code so that it can access a third party application go to Advanced go to the project it's telling you it can see edit and clear all your docs and connect to an external service so let's go ahead and click on allow and once we do that then this execution started it's completed now I'll go back to my Google doc I'm going to select this particular text here I'm going to go to my custom menu and click on summarize select paragraph and in a few seconds you can see that the code has successfully worked and it has generated a summary for the paragraph that we just selected so here you can see this is how we use the open AI API and Google absolute instead of Google docs to generate summaries for specific paragraphs um you can extend this functionality if you are maybe you're a reader or your blogger or you just work with a team of people that write paragraphs in Google Docs and you just want a quick summary of everything you can use this code to do that I'll be leaving the code link and the Google Talk Link in the description if you want to work with it there is a Blog version of this video also coming out very soon so I'll be leaving that link in the description and if you like this video then don't forget to like share and for more content the Subscribe button I'll see you in the next one