Skip to content

Instantly share code, notes, and snippets.

@ferminhg
Created February 27, 2023 21:57
Show Gist options
  • Save ferminhg/b2a5f7495d91aa83183a341f35cf20bc to your computer and use it in GitHub Desktop.
Save ferminhg/b2a5f7495d91aa83183a341f35cf20bc to your computer and use it in GitHub Desktop.
Apps script to connect chatGPT with gdocs
// DROP DOWN MENU
function onOpen() {
DocumentApp.getUi().createMenu("ChatGPT")
.addItem("Generate Ideas", "generateIdeas")
.addItem("Summary", "summary")
.addItem("Action Items", "actionItems")
.addToUi();
}
// ****END MENU****
// https://platform.openai.com/overview
const apiKey = "xxxxxxx";
const model = "text-davinci-003"
const temperature= 0.7
const maxTokens = 2060
const doc = DocumentApp.getActiveDocument()
function buildOptions(doc, prompt) {
const selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
const requestBody = {
"model": model,
"prompt": prompt + selectedText,
"temperature": temperature,
"max_tokens": maxTokens,
}
const requestOptions = {
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer "+apiKey
},
"payload": JSON.stringify(requestBody)
}
return requestOptions
}
function askAI(prompt) {
const requestOptions = buildOptions(doc, prompt)
const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions)
const responseText = response.getContentText()
const json = JSON.parse(responseText)
return json['choices'][0]['text']
}
function generateIdeas() {
const answer = askAI("Que ideas puedes extraer de este texto: ")
write(answer)
}
function summary() {
const answer = askAI("Puedes hacerme un resumen con los puntos mas imporanteas sobre esta conversacion: ")
write(answer)
}
function actionItems() {
const answer = askAI("Puedes extraer los action items de esta conversacion: ")
write(answer)
}
function write(answer) {
const body = doc.getBody()
para = body.appendParagraph("\n --- 🤖 ChatpGPT ---")
para = body.appendParagraph(answer)
para = body.appendParagraph("--- 🤖 EOB ---")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment