-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai.gs
39 lines (34 loc) · 1.05 KB
/
openai.gs
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
var API_KEY = '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';
function callOpenAI(text, prompt) {
var url = 'https://api.openai.com/v1/chat/completions';
var headers = {
'Authorization': 'Bearer ' + API_KEY,
'Content-Type': 'application/json'
};
var payload = {
'model': 'gpt-3.5-turbo',
'messages': [{
'role': 'system',
'content': prompt
}, {
'role': 'user',
'content': text
}],
'max_tokens': 256
};
var options = {
'method': 'post',
'headers': headers,
'payload': JSON.stringify(payload),
'muteHttpExceptions': true // Optional: handle exceptions silently
};
Logger.log("Sending openai request");
var response = UrlFetchApp.fetch(url, options);
Logger.log("All done with request! :>");
var responseText = response.getContentText();
var responseObject = JSON.parse(responseText);
Logger.log("responseText:\n"+responseText)
// Logger.log("responseObject:\n"+responseObject)
return responseObject.choices[0].message.content;
// need error handling
}