-
Notifications
You must be signed in to change notification settings - Fork 6
/
appscript
59 lines (42 loc) · 1.71 KB
/
appscript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Setup Variables
* 1) Paste in Your Open AI Secret Key
* 2) Adjust the Maximum Tokens to Process
*/
const SECRET_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const MAX_TOKENS = 200;
// Inspired by Abi, follow him on Twitter: https://twitter.com/_abi_
// Modified by Rock, follow him on Twitter: https://twitter.com/rocktrembath
/**
* Completes your prompt with GPT-3
*
* @param {string} userprompt Prompt
* @param {string} userrole Set to User to instruct it for User input
* @param {number} temperature (Optional) Temperature. 2 is super creative while 0 is very exact and precise. Defaults to 0.5.
* @param {string} model (Optional) ChatGPT Model to use. Defaults to "gpt-4".
* @return Completion returned by ChatGPT
* @customfunction
*/
function AI(userprompt, userrole = "user", temperature = 0.5, model = "gpt-4") {
// Create the array for the Chat prompt
// More info: https://platform.openai.com/docs/guides/chat/introduction
const gptprompt = [
{role : userrole, content : userprompt},
];
// Create API Requests
const url = "https://api.openai.com/v1/chat/completions";
const payload = {
model: model,
messages: gptprompt,
temperature: temperature,
max_tokens: MAX_TOKENS,
};
const options = {
contentType: "application/json",
headers: { Authorization: "Bearer " + SECRET_KEY },
payload: JSON.stringify(payload),
};
const results = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
// Return results from call - for more info on the structure returned see the documentation:
// https://platform.openai.com/docs/api-reference/chat/create
return results.choices[0].message.content.trim();
}