├── Davinci3.js ├── GPT-4 ├── gpt-3.5-turbo.js └── README.md /Davinci3.js: -------------------------------------------------------------------------------- 1 | const SECRET_KEY = "API_key_here"; 2 | const MAX_DAVINCI_TOKENS = 500; 3 | 4 | function Davinci3(prompt, model="text-davinci-003", temperature=0.5) { 5 | let endpoint = "https://api.openai.com/v1/completions"; 6 | let apiKey = SECRET_KEY; 7 | let payload = { 8 | model: model, 9 | prompt: prompt, 10 | max_tokens: MAX_DAVINCI_TOKENS, 11 | temperature: temperature 12 | }; 13 | 14 | let options = { 15 | method: "post", 16 | contentType: "application/json", 17 | payload: JSON.stringify(payload), 18 | headers: {"Authorization": "Bearer " + apiKey} 19 | }; 20 | 21 | let response = UrlFetchApp.fetch(endpoint, options); 22 | let json = response.getContentText(); 23 | let data = JSON.parse(json); 24 | 25 | return data.choices[0].text.trim(); 26 | } 27 | -------------------------------------------------------------------------------- /GPT-4: -------------------------------------------------------------------------------- 1 | const SECRET_KEY = "API_key_here"; 2 | const MAX_TURBO_TOKENS = 500; 3 | 4 | function GPT-4(prompt, model="gpt-4", temperature=0.5) { 5 | let apiKey = SECRET_KEY; 6 | let endpoint = "https://api.openai.com/v1/chat/completions"; 7 | let payload = { 8 | messages:[ 9 | {role: 'system', content: 'This is where you describe what gpt does and how it answers.'}, 10 | {role: 'user', content: 'This is an example question'}, 11 | {role: 'assistant', content: "This is an example answer"}, 12 | {role: 'user', content: `${prompt}`}, 13 | ], 14 | model: model, 15 | temperature: temperature, 16 | max_tokens: MAX_TOKENS, 17 | }; 18 | let options = { 19 | method: "post", 20 | contentType: "application/json", 21 | payload: JSON.stringify(payload), 22 | headers: { 23 | "Authorization": "Bearer " + apiKey 24 | } 25 | }; 26 | 27 | let response = UrlFetchApp.fetch(endpoint, options); 28 | let json = response.getContentText(); 29 | let data = JSON.parse(json); 30 | return data.choices[0].message.content.trim(); 31 | } 32 | -------------------------------------------------------------------------------- /gpt-3.5-turbo.js: -------------------------------------------------------------------------------- 1 | const SECRET_KEY = "API_key_here"; 2 | const MAX_TURBO_TOKENS = 500; 3 | 4 | function TURBO(prompt, model="gpt-3.5-turbo", temperature=0.5) { 5 | let apiKey = SECRET_KEY; 6 | let endpoint = "https://api.openai.com/v1/chat/completions"; 7 | let payload = { 8 | messages:[ 9 | {role: 'system', content: 'This is where you describe what gpt does and how it answers.'}, 10 | {role: 'user', content: 'This is an example question'}, 11 | {role: 'assistant', content: "This is an example answer"}, 12 | {role: 'user', content: `${prompt}`}, 13 | ], 14 | model: model, 15 | temperature: temperature, 16 | max_tokens: MAX_TURBO_TOKENS, 17 | }; 18 | let options = { 19 | method: "post", 20 | contentType: "application/json", 21 | payload: JSON.stringify(payload), 22 | headers: { 23 | "Authorization": "Bearer " + apiKey 24 | } 25 | }; 26 | 27 | let response = UrlFetchApp.fetch(endpoint, options); 28 | let json = response.getContentText(); 29 | let data = JSON.parse(json); 30 | return data.choices[0].message.content.trim(); 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

ChatGPT-Google-Sheets-Apps-Script

2 | 3 | 4 | 11 | 17 | 18 |
5 | 9 |
Apps Scripts 10 |
12 |

Simple code snippets for integrating OpenAI's GPT language models into your Google Sheets documents using Apps Script. 13 |

!!!Davinci3 is now deprecated!!! 14 |

15 |

16 |
19 | 20 | 21 | ## ChatGPT-Google-Sheets-Apps-Scripts installation instructions 22 | 23 | ### Go to OpenAI and set up an account 24 | ```sh 25 | https://platform.openai.com/signup 26 | ``` 27 | ## IMPORTANT!! Sharing GPT-enabled documents could compromise API keys. Only share GPT enabled documents if you know what you are doing. 28 | ### Open a new document in Google Sheets, click Extensions / Apps Script 29 | ### Push the + button near the top left and choose Script to create a new script 30 | ### Clear the page, then copy and paste code from the desired model js file to Script 31 | ### Place your secret OpenAI API key between quotation marks at the top of the page where it says SECRET_API_KEY_HERE 32 | ### Each function calls a different AI model and can be called using the KEYWORD immediately following the word "function" in line 4 (GPT-4, TURBO, Davinci3) 33 | ### Use the new functionality by typing =KEYWORD("your prompt to GPT will go here") and pressing enter 34 | ### For GPT-3.5 models fill in the prompt at line 9,10,11 to specify desired functionality 35 | --------------------------------------------------------------------------------