├── .gitignore ├── Code.js ├── README.md ├── img ├── gpt3-spreadsheet.gif ├── nations.png ├── scientists.png └── sports.png └── push.sh /.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 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | .apikey.env # Holds the openai api key. 132 | appsscript.json 133 | .clasp.json 134 | .* 135 | -------------------------------------------------------------------------------- /Code.js: -------------------------------------------------------------------------------- 1 | /** 2 | * This sheet adds an option to the top-bar of google sheets to fill an NxM range of the 3 | * sheet using OpenAI's GPT-3 Language model. 4 | */ 5 | 6 | var API_KEY = "[API_KEY_GOES_HERE]"; 7 | 8 | var preface = "I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with 'Unknown'.\ 9 | \ 10 | Q: What is human life expectancy in the United States?\ 11 | A: 78 years.\ 12 | \ 13 | Q: Who was president of the United States in 1955?\ 14 | A: Dwight D. Eisenhower.\ 15 | \ 16 | Q: Which party did he belong to?\ 17 | A: He belonged to the Republican Party.\ 18 | \ 19 | Q: What is the square root of banana?\ 20 | A: Unknown\ 21 | \ 22 | Q: How does a telescope work?\ 23 | A: Telescopes use lenses or mirrors to focus light and make objects appear closer.\ 24 | \ 25 | Q: Where were the 1992 Olympics held?\ 26 | A: Barcelona, Spain.\ 27 | \ 28 | Q: How many squigs are in a bonk?\ 29 | A: Unknown\ 30 | \ 31 | Q: What is the revenue of General Motors?\ 32 | A: $150.8 billion.\ 33 | \ 34 | Q: What is the market cap of 3M?\ 35 | A: $93.8 billion.\ 36 | \ 37 | " 38 | 39 | var NUM_TOKENS = 15; 40 | 41 | function onOpen() { 42 | var spreadsheet = SpreadsheetApp.getActive(); 43 | 44 | var menuItems = [ 45 | {name: 'Fill with GPT-3', functionName: 'gpt3fill'}, 46 | ]; 47 | 48 | spreadsheet.addMenu('GPT3', menuItems); 49 | } 50 | 51 | function _callAPI(prompt) { 52 | var data = { 53 | 'prompt': prompt, 54 | 'max_tokens': NUM_TOKENS, 55 | 'temperature': 0, 56 | }; 57 | 58 | var options = { 59 | 'method' : 'post', 60 | 'contentType': 'application/json', 61 | 'payload' : JSON.stringify(data), 62 | 'headers': { 63 | Authorization: 'Bearer ' + API_KEY, 64 | }, 65 | }; 66 | 67 | response = UrlFetchApp.fetch( 68 | 'https://api.openai.com/v1/engines/davinci/completions', 69 | options, 70 | ); 71 | 72 | return JSON.parse(response.getContentText())['choices'][0]['text'] 73 | } 74 | 75 | function _parse_response(response) { 76 | var parsed_fill = response.slice(3, response.indexOf('Q: ')); 77 | 78 | if (parsed_fill.charAt(parsed_fill.length - 1) == '.') { 79 | parsed_fill = parsed_fill.slice(0, -1); 80 | } 81 | 82 | return parsed_fill; 83 | } 84 | 85 | function get_x_of_y(x, y) { 86 | var prompt = preface + "Q: What is the " + x + " of " + y + "?" 87 | 88 | var response = _callAPI(prompt); 89 | 90 | var parsed_response = _parse_response(response); 91 | 92 | return parsed_response; 93 | } 94 | 95 | 96 | function gpt3fill() { 97 | /* 98 | Highlight a nxm range where the leftmost column contains names of public companies. 99 | The header row of a column identifies properties of those companies. 100 | Fill in the values. 101 | */ 102 | var spreadsheet = SpreadsheetApp.getActive(); 103 | var range = spreadsheet.getActiveRange(); 104 | var num_rows = range.getNumRows(); 105 | var num_cols = range.getNumColumns(); 106 | 107 | for (var x=2; x Tools -> Script Editor 16 | 3. Copy the contents of Code.js into the script editor, and put your OpenAI API key in the relevant location. 17 | 4. Click save. You may have to consent to a few authentication dialogs. 18 | 5. Refresh your google sheet. You should see a new menu bar item "GPT3". 19 | 20 | 21 | ## Usage 22 | 23 | Simply fill out row and column titles for an empty table, highlight the table and headings, and select "GPT3 Fill" from the menu bar. Wait a few moments and you should see entries begin to appear in the table. 24 | 25 | The way this script infers what data you want in your table by looking at the row and column headings, so care should be taken in selecting them. We assume that properties are described by column headings and entities are described by row headings. The query that is put to GPT3 is roughly of the form: "What is the [column_heading] of [row_heading]?" 26 | 27 | ## Usage examples 28 | 29 | Here are some examples of tables that have been fully filled out by this tool. Answers that are clearly or questionably incorrect are highlighted in red. 30 | 31 | Numeric and subjective facts about different nations. 32 | 33 | ![alttext](img/nations.png) 34 | 35 | Information about famous scientists. 36 | 37 | ![alttext](img/scientists.png) 38 | 39 | Facts about different sports. 40 | 41 | ![alttext](img/sports.png) 42 | 43 | ## Locally editable installation 44 | 45 | It is possible to develop, push, and deploy this script locally. Please see the ![clasp](https://developers.google.com/apps-script/guides/clasp) documentation for instructions on how to set this up. When developing locally it is best-practice to keep your API key in the file `.apikey.env` instead of in `Code.js`. The shell script `push.sh` can be used to deploy your script with the key included while keeping the key out of your github/source control. 46 | 47 | ## Further development 48 | 49 | There are a lot of things which could be improved in this tool. Here are some ideas or directions I may work in in the future. 50 | 51 | - Performance: the script currently makes a single API call for every cell. It is very likely this could be reduced by using a more clever prompt to GPT3. 52 | - Parsing: the parsing logic for answers which come out of the AI is very basic. Improvements here would greatly improve the accuracy of the tool. 53 | - Prompt improvements: It is likely that small modifications to the prompt could increase the accuracy of the table-filling substantially. 54 | 55 | 56 | -------------------------------------------------------------------------------- /img/gpt3-spreadsheet.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garethdmm/SpreadsheetMagic/c6a2db051b13e10e8daaf6a7d0e132b83e84f0a9/img/gpt3-spreadsheet.gif -------------------------------------------------------------------------------- /img/nations.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garethdmm/SpreadsheetMagic/c6a2db051b13e10e8daaf6a7d0e132b83e84f0a9/img/nations.png -------------------------------------------------------------------------------- /img/scientists.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garethdmm/SpreadsheetMagic/c6a2db051b13e10e8daaf6a7d0e132b83e84f0a9/img/scientists.png -------------------------------------------------------------------------------- /img/sports.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garethdmm/SpreadsheetMagic/c6a2db051b13e10e8daaf6a7d0e132b83e84f0a9/img/sports.png -------------------------------------------------------------------------------- /push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | placeholder="\[API_KEY_GOES_HERE\]" 3 | value=`cat .apikey.env` 4 | sed -i '' -e "s/$placeholder/$value/" Code.js 5 | clasp push 6 | sed -i '' -e "s/$value/$placeholder/" Code.js 7 | --------------------------------------------------------------------------------