Skip to content

Instantly share code, notes, and snippets.

@ferminhg
Created February 27, 2023 21:57
Show Gist options
  • Select an option

  • Save ferminhg/b2a5f7495d91aa83183a341f35cf20bc to your computer and use it in GitHub Desktop.

Select an option

Save ferminhg/b2a5f7495d91aa83183a341f35cf20bc to your computer and use it in GitHub Desktop.

Revisions

  1. ferminhg created this gist Feb 27, 2023.
    72 changes: 72 additions & 0 deletions code_chatgpt.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    // 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 ---")
    }