Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //сюда вводим наш API-ключ, получать здесь https://beta.openai.com/account/api-keys
- const t = '';
- function gpt_image() {
- //адрес API, на который будем обращаться
- const url = 'https://api.openai.com/v1/images/generations';
- //обращаемся к нашей таблице и к листу
- const ss = SpreadsheetApp.getActive();
- const sh = ss.getSheetByName('IMAGE');
- //забираем значение для запроса
- const [prompt, n, size] = sh.getRange("A2:C2").getValues()[0];
- // https://platform.openai.com/docs/api-reference/images/create
- const request = r(url, 'post', t, {
- "prompt": prompt,
- "n": n,
- "size": size
- });
- //извлекаем либо ответ, либо, если ответа нет (как правило, это какая-то ошибка) - возвращаем целиком запрос
- var response = request?.data?.map(f => f?.url) || request;
- response = [[new Date(), [prompt, n, size].join("|")].concat(response)];
- Logger.log(response)
- //вставляем на наш лист строку перед 3й строкой
- sh.insertRowBefore(3);
- //и вставляем в эту строчку дату и время, аргументы запроса и сам ответ
- sh.getRange(3, 7, response.length, response[0].length).setValues(response);
- }
- function r(url, method, token, data) {
- let params = {
- method: method,
- muteHttpExceptions: true,
- contentType: 'application/json;',
- payload: JSON.stringify(data),
- 'headers': {
- Authorization: 'Bearer ' + token
- }
- };
- var r = UrlFetchApp.fetch(url, params);
- r = JSON.parse(r)
- return r;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement