-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt3.js
33 lines (31 loc) · 1.04 KB
/
gpt3.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
/**
Sends a request to the OpenAI API to generate text using the GPT-3 language model.
@param {string} prompt - The text prompt to generate text from.
@param {number} maxToken - The maximum number of tokens to generate.
@returns {string} - The generated text from the OpenAI API.
*/
function GPT3(prompt, maxToken) {
const apiKey = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Config").getRange("A2").getValue()
const data = {
"model": "text-davinci-003",
"prompt": prompt,
"max_tokens": maxToken,
"temperature": 0.8,
"top_p": 1,
"n": 1,
"stream": false,
"logprobs": null,
}
const options = {
'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : JSON.stringify(data),
'headers': {
'Authorization': "Bearer " + apiKey
}
};
const x = UrlFetchApp.fetch('https://api.openai.com/v1/completions', options);
const res = JSON.parse(x.getContentText());
return res.choices[0].text
}