Created
February 3, 2023 22:04
-
-
Save mustafabutt-dev/997afd9cc9f7285b055e053ad95ff298 to your computer and use it in GitHub Desktop.
Google Docs Integration with ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DROP DOWN MENU | |
function onOpen() { | |
DocumentApp.getUi().createMenu("ChatGPT") | |
.addItem("Generate Image", "generateImage") | |
.addItem("Generate synonym ", "generateSynonym") | |
.addItem("Write a tagline", "tagLine") | |
.addToUi(); | |
} | |
// ****END MENU**** | |
var apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
var model = "text-davinci-003" | |
// GENERATE PROMPT FOR CHATGPT | |
function generateSynonym() { | |
var document = DocumentApp.getActiveDocument() | |
var text = document.getSelection().getRangeElements()[0].getElement().asText().getText() | |
var body = document.getBody() | |
var prompt = "generate synonym for " + text; | |
temperature= 0 | |
maxTokens = 1060 | |
const requestBody = { | |
"model": model, | |
"prompt": prompt, | |
"temperature": temperature, | |
"max_tokens": maxTokens, | |
}; | |
const requestOptions = { | |
"method": "POST", | |
"headers": { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer "+apiKey | |
}, | |
"payload": JSON.stringify(requestBody) | |
} | |
const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions); | |
var responseText = response.getContentText(); | |
var json = JSON.parse(responseText); | |
Logger.log(json['choices'][0]['text']) | |
para = body.appendParagraph(json['choices'][0]['text']) | |
} | |
// ****END PROMPT**** | |
// GENERATE IMAGE | |
function generateImage() { | |
var document = DocumentApp.getActiveDocument() | |
var text = document.getSelection().getRangeElements()[0].getElement().asText().getText() | |
var body = document.getBody() | |
temperature= 0 | |
maxTokens = 2000 | |
var prompt2 = "Generate images for " + text; | |
const requestBody2 = { | |
"prompt": prompt2, | |
"n": 1, | |
"size": "1024x1024" | |
}; | |
const requestOptions2 = { | |
"method": "POST", | |
"headers": { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer "+apiKey | |
}, | |
"payload": JSON.stringify(requestBody2) | |
} | |
const response2 = UrlFetchApp.fetch("https://api.openai.com/v1/images/generations", requestOptions2); | |
var responseText = response2.getContentText(); | |
var json = JSON.parse(responseText); | |
var url=json['data'][0]['url'] | |
body.appendImage(UrlFetchApp.fetch(url).getBlob()); | |
} | |
// GENERATE PROMPT FOR CHATGPT | |
function tagLine() { | |
var doc = DocumentApp.getActiveDocument() | |
var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText() | |
var body = doc.getBody() | |
var prompt = "write a tagline for " + selectedText; | |
temperature= 0 | |
maxTokens = 2060 | |
const requestBody = { | |
"model": model, | |
"prompt": prompt, | |
"temperature": temperature, | |
"max_tokens": maxTokens, | |
}; | |
const requestOptions = { | |
"method": "POST", | |
"headers": { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer "+apiKey | |
}, | |
"payload": JSON.stringify(requestBody) | |
} | |
const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions); | |
var responseText = response.getContentText(); | |
var json = JSON.parse(responseText); | |
Logger.log(json['choices'][0]['text']) | |
para = body.appendParagraph(json['choices'][0]['text']) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment