forked from MenzelRobin/Google-Sheets-GPT3-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (38 loc) · 1.37 KB
/
script.js
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
// const CHATGPT_API_KEY = "paste your API key here" <- a number of online tutorials will recommend you to do this. If it is a private sheet, this is fine but if you plan to share, do not that your key will most definitely be exposed
// declare const or endpoint for completions
const BASE_URL = "https://api.openai.com/v1/chat/completions";
// create a function to fetch data from openAI
function fetchData(systemContent, userContent) {
try {
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${CHAT_GPT_API_KEY}`
};
const options = {
headers,
method: "GET",
muteHttpExceptions: true,
payload: JSON.stringify({
"model": "gpt-3.5-turbo",
"messages": [{
"role": "system",
"content": systemContent,
},
{
"role": "user",
"content": userContent
},
],
"temperature": 0.7
})
};
const response = JSON.parse(UrlFetchApp.fetch(BASE_URL, options));
//console.log(response);
//console.log(response.choices[0].message.content)
return response.choices[0].message.content;
} catch (e) {
console.log(e)
SpreadsheetApp.getActiveSpreadsheet().toast("Some Error Occured Please check your formula or try again later.");
return "Some Error Occured Please check your formula or try again later.";
}
}