├── README.md ├── add-on ├── ask.js └── sidebar.html ├── server ├── .env.example ├── .gitignore ├── Dockerfile ├── Procfile ├── requirements.txt └── server.py └── wordGPT └── ask.bas /README.md: -------------------------------------------------------------------------------- 1 |

2 | docGPT 📄 3 |

4 |

5 | ChatGPT directly integrated into Google Docs 📑 6 |

7 | 8 | 9 | ## Features 10 | - Faster than [chat.openai.com](https://chat.openai.com) 11 | - Free 12 | - Live Chat Within Google Docs 13 | - Text Completion 14 | 15 | 16 | ## Usage 17 | 18 | - **Google Docs**
19 | - **MS Word**
20 | 21 | ### Google Docs 22 | 23 | 1. Get the template: https://docs.google.com/document/d/1N7qvw5mZdVe2u2IQ5pnVDmUjHsLEfq9_Z0Tf8PHloZA/edit?usp=sharing 24 | 25 | 2. Make a copy of the document 26 | 27 | ![alt text](https://i.imgur.com/YlWvBEzl.png) 28 | 29 | 3. Type something in your Google Doc 30 | 31 | ![alt text](https://i.imgur.com/287n0U0l.png) 32 | 33 | 4. Select your question, or whatever text you want to send to ChatGPT 34 | 35 | ![alt text](https://i.imgur.com/62tfu0kl.png) 36 | 37 | 5. Use the extension! (click Start instead of Ask to access the chat pop-up) 38 | 39 | ![alt text](https://i.imgur.com/g7w6Qgfl.png) 40 | 41 | 6. Accept the Authorization request & sign into google 42 | 43 | ![alt text](https://i.imgur.com/LbmKDmpl.png) 44 | 45 | 7. Click Advanced, go to ChatGPT & allow the scopes required 46 | 47 | ![alt text](https://i.imgur.com/D7gzZpal.png) 48 | 49 | 50 | 8. Get your result! 51 | 52 | ![alt text](https://i.imgur.com/MEidlLYl.png) 53 | 54 | ### MS Word 55 | 56 | 1. Open a new word document 57 | 58 | 2. Enable the Developer Tab on Word 59 | 60 | 3. Click Macros 61 | ![alt text](https://i.imgur.com/946Lupxl.png) 62 | 63 | 4. Create a new macro with the name AddToShortcut 64 | ![alt text](https://i.imgur.com/1DSMx78l.png) 65 | 66 | 5. Copy the code in `wordGPT/ask.bas` of this repo, and paste it into the Word VBA Editor 67 | 68 | 6. Click `Tools > References` in the navbar
69 | ![alt text](https://i.imgur.com/eiWU4Ecl.png) 70 | 71 | 7. Search for Microsoft Scripting Runtime and enable it
72 | ![image](https://user-images.githubusercontent.com/67405604/205881130-c82f1ace-2c06-462e-a196-e7188077e9c5.png) 73 | 74 | 8. Click OK and Save the file containing the code you pasted. 75 | 76 | 9. Right click selected text in Word and click `Ask ChatGPT` 77 | 78 | ![image](https://user-images.githubusercontent.com/67405604/205882403-1fee052b-1a40-45e0-838b-f0c9268611ed.png) 79 | 80 | 1. Wait for your result! (Currently it is not recommended to ask ChatGPT again before current question is answered, otherwise all the answers can be mixed up. Keyboard input is disabled while waiting for the result, see issue #9.) 81 | 82 | -------------------------------------------------------------------------------- /add-on/ask.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // [START apps_script_docs_translate_quickstart] 17 | /** 18 | * @OnlyCurrentDoc 19 | * 20 | * The above comment directs Apps Script to limit the scope of file 21 | * access for this add-on. It specifies that this add-on will only 22 | * attempt to read or modify the files in which the add-on is used, 23 | * and not all of the user's files. The authorization request message 24 | * presented to users will reflect this limited scope. 25 | */ 26 | 27 | /** 28 | * Creates a menu entry in the Google Docs UI when the document is opened. 29 | * This method is only used by the regular add-on, and is never called by 30 | * the mobile add-on version. 31 | * 32 | * @param {object} e The event parameter for a simple onOpen trigger. To 33 | * determine which authorization mode (ScriptApp.AuthMode) the trigger is 34 | * running in, inspect e.authMode. 35 | */ 36 | function onOpen(e) { 37 | DocumentApp.getUi().createAddonMenu() 38 | .addItem('Start', 'showSidebar') 39 | .addItem('Ask', 'autoComplete') 40 | .addToUi(); 41 | showSidebar(); 42 | } 43 | 44 | /** 45 | * Runs when the add-on is installed. 46 | * This method is only used by the regular add-on, and is never called by 47 | * the mobile add-on version. 48 | * 49 | * @param {object} e The event parameter for a simple onInstall trigger. To 50 | * determine which authorization mode (ScriptApp.AuthMode) the trigger is 51 | * running in, inspect e.authMode. (In practice, onInstall triggers always 52 | * run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or 53 | * AuthMode.NONE.) 54 | */ 55 | function onInstall(e) { 56 | onOpen(e); 57 | } 58 | 59 | function autoComplete() { 60 | 61 | const selected = getSelectedText(); 62 | 63 | const response = getGPTResponse(selected[0]); 64 | 65 | insertTextWithoutReplacement(response); 66 | } 67 | 68 | /** 69 | * Opens a sidebar in the document containing the add-on's user interface. 70 | * This method is only used by the regular add-on, and is never called by 71 | * the mobile add-on version. 72 | */ 73 | function showSidebar() { 74 | const ui = HtmlService.createHtmlOutputFromFile('sidebar') 75 | .setTitle('docGPT'); 76 | DocumentApp.getUi().showSidebar(ui); 77 | } 78 | 79 | /** 80 | * Gets the text the user has selected. If there is no selection, 81 | * this function displays an error message. 82 | * 83 | * @return {Array.} The selected text. 84 | */ 85 | function getSelectedText() { 86 | const selection = DocumentApp.getActiveDocument().getSelection(); 87 | const text = []; 88 | if (selection) { 89 | const elements = selection.getSelectedElements(); 90 | for (let i = 0; i < elements.length; ++i) { 91 | if (elements[i].isPartial()) { 92 | const element = elements[i].getElement().asText(); 93 | const startIndex = elements[i].getStartOffset(); 94 | const endIndex = elements[i].getEndOffsetInclusive(); 95 | 96 | text.push(element.getText().substring(startIndex, endIndex + 1)); 97 | } else { 98 | const element = elements[i].getElement(); 99 | // Only translate elements that can be edited as text; skip images and 100 | // other non-text elements. 101 | if (element.editAsText) { 102 | const elementText = element.asText().getText(); 103 | // This check is necessary to exclude images, which return a blank 104 | // text element. 105 | if (elementText) { 106 | text.push(elementText); 107 | } 108 | } 109 | } 110 | } 111 | } 112 | if (!text.length) throw new Error('Please select some text.'); 113 | return text; 114 | } 115 | 116 | /** 117 | * Gets the stored user preferences for the origin and destination languages, 118 | * if they exist. 119 | * This method is only used by the regular add-on, and is never called by 120 | * the mobile add-on version. 121 | * 122 | * @return {Object} The user's origin and destination language preferences, if 123 | * they exist. 124 | */ 125 | function getPreferences() { 126 | const userProperties = PropertiesService.getUserProperties(); 127 | return { 128 | originLang: userProperties.getProperty('originLang'), 129 | destLang: userProperties.getProperty('destLang') 130 | }; 131 | } 132 | 133 | /** 134 | * Gets the user-selected text and translates it from the origin language to the 135 | * destination language. The languages are notated by their two-letter short 136 | * form. For example, English is 'en', and Spanish is 'es'. The origin language 137 | * may be specified as an empty string to indicate that Google Translate should 138 | * auto-detect the language. 139 | * 140 | * @param {string} origin The two-letter short form for the origin language. 141 | * @param {string} dest The two-letter short form for the destination language. 142 | * @param {boolean} savePrefs Whether to save the origin and destination 143 | * language preferences. 144 | * @return {Object} Object containing the original text and the result of the 145 | * translation. 146 | */ 147 | function getGPTResponse(question) { 148 | 149 | var options = { 150 | 'method' : 'post', 151 | 'contentType': 'application/json', 152 | // Convert the JavaScript object to a JSON string. 153 | 'payload' : JSON.stringify({ 154 | 'message': question 155 | }) 156 | }; 157 | 158 | const response = UrlFetchApp.fetch('https://chatgpt-api.kesarx.repl.co/chat', options); 159 | const json = JSON.parse(response.getContentText()); 160 | return json.choices[0].message.content; 161 | } 162 | 163 | /** 164 | * Replaces the text of the current selection with the provided text, or 165 | * inserts text at the current cursor location. (There will always be either 166 | * a selection or a cursor.) If multiple elements are selected, only inserts the 167 | * translated text in the first element that can contain text and removes the 168 | * other elements. 169 | * 170 | * @param {string} newText The text with which to replace the current selection. 171 | */ 172 | function insertText(newText) { 173 | const selection = DocumentApp.getActiveDocument().getSelection(); 174 | if (selection) { 175 | let replaced = false; 176 | const elements = selection.getSelectedElements(); 177 | if (elements.length === 1 && elements[0].getElement().getType() === 178 | DocumentApp.ElementType.INLINE_IMAGE) { 179 | throw new Error('Can\'t insert text into an image.'); 180 | } 181 | for (let i = 0; i < elements.length; ++i) { 182 | if (elements[i].isPartial()) { 183 | const element = elements[i].getElement().asText(); 184 | const startIndex = elements[i].getStartOffset(); 185 | const endIndex = elements[i].getEndOffsetInclusive(); 186 | element.deleteText(startIndex, endIndex); 187 | if (!replaced) { 188 | element.insertText(startIndex, newText); 189 | replaced = true; 190 | } else { 191 | // This block handles a selection that ends with a partial element. We 192 | // want to copy this partial text to the previous element so we don't 193 | // have a line-break before the last partial. 194 | const parent = element.getParent(); 195 | const remainingText = element.getText().substring(endIndex + 1); 196 | parent.getPreviousSibling().asText().appendText(remainingText); 197 | // We cannot remove the last paragraph of a doc. If this is the case, 198 | // just remove the text within the last paragraph instead. 199 | if (parent.getNextSibling()) { 200 | parent.removeFromParent(); 201 | } else { 202 | element.removeFromParent(); 203 | } 204 | } 205 | } else { 206 | const element = elements[i].getElement(); 207 | if (!replaced && element.editAsText) { 208 | // Only translate elements that can be edited as text, removing other 209 | // elements. 210 | element.clear(); 211 | element.asText().setText(newText); 212 | replaced = true; 213 | } else { 214 | // We cannot remove the last paragraph of a doc. If this is the case, 215 | // just clear the element. 216 | if (element.getNextSibling()) { 217 | element.removeFromParent(); 218 | } else { 219 | element.clear(); 220 | } 221 | } 222 | } 223 | } 224 | } else { 225 | const cursor = DocumentApp.getActiveDocument().getCursor(); 226 | const surroundingText = cursor.getSurroundingText().getText(); 227 | const surroundingTextOffset = cursor.getSurroundingTextOffset(); 228 | 229 | // If the cursor follows or preceds a non-space character, insert a space 230 | // between the character and the translation. Otherwise, just insert the 231 | // translation. 232 | if (surroundingTextOffset > 0) { 233 | if (surroundingText.charAt(surroundingTextOffset - 1) !== ' ') { 234 | newText = ' ' + newText; 235 | } 236 | } 237 | if (surroundingTextOffset < surroundingText.length) { 238 | if (surroundingText.charAt(surroundingTextOffset) !== ' ') { 239 | newText += ' '; 240 | } 241 | } 242 | cursor.insertText(newText); 243 | } 244 | } 245 | 246 | /** 247 | * Inserts text after the current selected text. 248 | * 249 | * @param {string} newText The text with which to replace the current selection. 250 | */ 251 | function insertTextWithoutReplacement(newText) { 252 | const selection = DocumentApp.getActiveDocument().getSelection(); 253 | if (selection) { 254 | const elements = selection.getSelectedElements(); 255 | if (elements.length === 1 && elements[0].getElement().getType() === 256 | DocumentApp.ElementType.INLINE_IMAGE) { 257 | throw new Error('Can\'t insert text into an image.'); 258 | } 259 | const element = elements[elements.length - 1].getElement().asText(); 260 | if (elements[elements.length - 1].isPartial()) { 261 | const endIndex = elements[elements.length - 1].getEndOffsetInclusive(); 262 | element.insertText(endIndex + 1, newText); 263 | } else { 264 | element.appendText(newText); 265 | } 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /add-on/sidebar.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 98 | 99 | 100 | 101 | 108 | 109 | 119 | 120 | 121 | 190 | 191 | 192 | 193 | -------------------------------------------------------------------------------- /server/.env.example: -------------------------------------------------------------------------------- 1 | api_key="" 2 | -------------------------------------------------------------------------------- /server/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 30 | __pypackages__/ 31 | 32 | # Environments 33 | .env 34 | .venv 35 | env/ 36 | venv/ 37 | pyenv/ 38 | ENV/ 39 | env.bak/ 40 | venv.bak/ 41 | 42 | # pytype static type analyzer 43 | .pytype/ 44 | 45 | # Cython debug symbols 46 | cython_debug/ 47 | -------------------------------------------------------------------------------- /server/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.7 2 | COPY ./* ./app/ 3 | WORKDIR /app/ 4 | RUN pip install -r requirements.txt 5 | EXPOSE 8080 6 | CMD ["python", "server.py"] 7 | -------------------------------------------------------------------------------- /server/Procfile: -------------------------------------------------------------------------------- 1 | web: python server.py -------------------------------------------------------------------------------- /server/requirements.txt: -------------------------------------------------------------------------------- 1 | flask 2 | waitress 3 | python-dotenv 4 | tls-client 5 | requests~=2.27.1 6 | colorama~=0.4.4 7 | svglib~=1.4.1 8 | bs4~=0.0.1 9 | beautifulsoup4~=4.10.0 10 | reportlab~=3.6.12 11 | openai~=0.27.0 12 | -------------------------------------------------------------------------------- /server/server.py: -------------------------------------------------------------------------------- 1 | """Make some requests to OpenAI's chatbot""" 2 | import os 3 | from dotenv import load_dotenv 4 | import openai 5 | import flask 6 | from flask import Flask 7 | 8 | api_key = os.environ['api_key'] 9 | 10 | # Fancy stuff 11 | import colorama 12 | 13 | colorama.init(autoreset=True) 14 | 15 | load_dotenv() 16 | 17 | app = Flask(__name__) 18 | 19 | openai.api_key = api_key 20 | 21 | 22 | @app.route("/", methods=["GET"]) 23 | def index(): 24 | return 'docGPT is up! Read the docs' 25 | 26 | 27 | @app.route("/chat", methods=["POST"]) 28 | def chat(): 29 | message = flask.request.json.get("message") 30 | 31 | completion = openai.ChatCompletion.create( 32 | model="gpt-3.5-turbo", 33 | messages=[{ 34 | "role": "user", 35 | "content": message 36 | }] 37 | ) 38 | 39 | return completion 40 | 41 | 42 | def application(): 43 | return app 44 | -------------------------------------------------------------------------------- /wordGPT/ask.bas: -------------------------------------------------------------------------------- 1 | Option Explicit 2 | 3 | Sub DeleteShortcut() 4 | Dim DeleteControl As CommandBarControl 5 | For Each DeleteControl In Application.CommandBars("Text").Controls 6 | If DeleteControl.Caption = "Ask ChatGPT" Then 7 | DeleteControl.Delete 8 | End If 9 | Next DeleteControl 10 | End Sub 11 | 12 | Sub AddToShortcut() 13 | Dim Bar As CommandBar 14 | Dim NewControl As CommandBarButton 15 | Set Bar = Application.CommandBars("Text") 16 | Set NewControl = Bar.Controls.Add(Type:=msoControlButton, ID:=1, Temporary:=False) 17 | With NewControl 18 | .Caption = "Ask ChatGPT" 19 | .OnAction = "Ask" 20 | .Style = msoButtonIconAndCaption 21 | End With 22 | End Sub 23 | 24 | 25 | Private Sub Ask() 26 | 27 | Dim selection As selection 28 | Set selection = Application.selection 29 | Dim selectedText As String 30 | Dim init_end, new_end As Integer 31 | 32 | selectedText = Replace(selection.text, ChrW$(13), "") 33 | init_end = CInt(selection.End) 34 | 35 | Dim req As Object 36 | Set req = CreateObject("WinHttp.WinHttpRequest.5.1") 37 | 38 | req.Open "POST", "https://chatgpt-api.kesarx.repl.co/chat", True 39 | req.SetRequestHeader "Content-Type", "application/json" 40 | req.Send "{""message"": """ & selectedText & """}" 41 | 42 | req.WaitForResponse 43 | 44 | Dim objHTML, objWin As Object 45 | Set objHTML = CreateObject("HTMLFile") 46 | Set objWin = objHTML.parentWindow 47 | objWin.execScript "var data = " & req.ResponseText & ";", "JScript" 48 | objWin.execScript "var response_msg = data.choices[0].message.content;", "JScript" 49 | 50 | Dim result As String 51 | result = objWin.response_msg 52 | 53 | new_end = CInt(selection.End) 54 | selection.Move Unit:=wdCharacter, Count:=init_end - new_end 55 | selection.Range.InsertAfter Chr(10) & result & Chr(10) 56 | 57 | End Sub 58 | 59 | 60 | Private Sub document_open() 61 | 'adds the right-click shortcut when the document opens 62 | Call AddToShortcut 63 | End Sub 64 | --------------------------------------------------------------------------------