├── 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 |
|
5 | Apps Scripts 10 | |
11 |
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 | |
17 |