0% found this document useful (0 votes)
96 views1 page

opemai api key

The document is a Google Apps Script function that retrieves user input from the last row of a Google Sheets document and sends it to the OpenAI API for processing. It checks for empty rows and retrieves the response from the API, which is then stored in the adjacent column. The function utilizes the GPT-4 model to generate responses based on the user's input.

Uploaded by

ASTE CON
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views1 page

opemai api key

The document is a Google Apps Script function that retrieves user input from the last row of a Google Sheets document and sends it to the OpenAI API for processing. It checks for empty rows and retrieves the response from the API, which is then stored in the adjacent column. The function utilizes the GPT-4 model to generate responses based on the user's input.

Uploaded by

ASTE CON
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

function getChatGPTResponse() {

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();


var lastRow = sheet.getLastRow();

// Check if the sheet is empty


if (lastRow === 0) {
Logger.log("No data found in the sheet.");
return;
}

var userInput = sheet.getRange(lastRow, 1).getValue(); // Get input from column


A

if (!userInput) {
Logger.log("No input found in the last row.");
return;
}

var apiKey = "sk-proj-


xQqGwsJborv8pSywRrOBaau8uYd2tC5cpQ__oDz__QJtvJkbTciAYcy0frZ0p8h0izd4hZz6bWT3BlbkFJv
OaW1pocrgkyDH-4WSJu5F2TyT-kiRcMnTEWkVrhg77FTlqmDIxAXvqD0aqIMlrnvkH_zvmiAA
"; // Replace with your OpenAI API key
var url = "https://api.openai.com/v1/chat/completions";

var options = {
"method": "post",
"headers": {
"Authorization": "Bearer " + apiKey,
"Content-Type": "application/json"
},
"payload": JSON.stringify({
"model": "gpt-4",
"messages": [{"role": "user", "content": userInput}]
})
};

var response = UrlFetchApp.fetch(url, options);


var json = JSON.parse(response.getContentText());
var gptResponse = json.choices[0].message.content;

sheet.getRange(lastRow, 2).setValue(gptResponse); // Store response in column B


}

You might also like