Merci @bill.french !
Here is âmyâ code (in fact the one of our gptFriend, since I have 0 knowledge in codingâŚyet).
It basically is an addon for Google Sheet to integrate with chatGpt.
My objective is to analyse 3000 rows of toolsâ description (letâs say 100 words per description) and have gtp return 7 keywords per row.
But I need to have ĂĄ macro view of the cost (20$ vs 200$ )
<function onOpen() {
// Create a custom menu in Google Sheets
var ui = SpreadsheetApp.getUi();
ui.createMenu(âChatGPTâ)
.addItem(âSelect promptâ, âshowPromptPickerâ)
.addItem(âEnter custom promptâ, âshowCustomPromptDialogâ)
.addToUi();
}
function CHAT(val) {
// main function to generate the custom formula (=CHAT)
var ss = SpreadsheetApp.getActiveSheet();
// Set API key
var apiKey = ââ â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â â YZd78Gniâ;
// Configure the API request to OpenAI
var data = {
âmessagesâ: [
{âroleâ: âsystemâ, âcontentâ: val},
{"role": "user", "content": "Here is your prompt:\n"}],
"model": "gpt-3.5-turbo",
};
var options = {
âmethodâ: âpostâ,
âcontentTypeâ: âapplication/jsonâ,
âpayloadâ: JSON.stringify(data),
âheadersâ: {
Authorization: Bearer ${apiKey}
,
},
};
var response = UrlFetchApp.fetch(
âhttps://api.openai.com/v1/chat/completionsâ,
options
);
Logger.log(response.getContentText());
// Send the API request
var result = JSON.parse(response.getContentText())[âchoicesâ][0][âmessageâ][âcontentâ];
Logger.log(result);
return result;
}
function showCustomPromptDialog() {
var ui = SpreadsheetApp.getUi();
var promptResponse = ui.prompt(âEnter your custom prompt:â);
if (promptResponse.getSelectedButton() == ui.Button.OK) {
var customPrompt = promptResponse.getResponseText();
var rangeResponse = ui.prompt(âEnter the origin cell, row, column, or range to analyze (e.g., A1, A2; A:B, 1:2)â);
if (rangeResponse.getSelectedButton() == ui.Button.OK) {
var selectedRange = rangeResponse.getResponseText();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var rangeData = sheet.getRange(selectedRange).getDisplayValues();
var input = rangeData.map(function (row) {
return row.join(â â);
}).join(â\nâ);
var destinationResponse = ui.prompt('Enter the Destination cell, row, column, or range (e.g., A1, A2; A:B; 1:2)');
if (destinationResponse.getSelectedButton() == ui.Button.OK) {
var destinationRange = destinationResponse.getResponseText();
var result = CHAT(customPrompt + '\n' + input);
sheet.getRange(destinationRange).setValue(result);
}
}
}
}
function showPromptPicker() {
var htmlOutput = HtmlService.createHtmlOutputFromFile(âGptApiDialogâ)
.setWidth(500)
.setHeight(400);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, âSelect a Promptâ);
}
function processPrompt(promptText, inputRange, outputRange) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var rangeData = sheet.getRange(inputRange).getDisplayValues();
var input = rangeData.map(function (row) {
return row.join(â â);
}).join(â\nâ);
var standardPrompts = [
{
prompt: âKeywordsâ,
systemMessage: âYou are experienced in reflecting a content with keywords. Please provide 10 keywords describing the following content.â
},
{
prompt: âSummaryâ,
systemMessage: âYou are an expert at summarizing content. You are able to reflect in a few words the key points of any type of text. Please provide a summary of the following contentâ
},
{
prompt: âSentimentâ,
systemMessage: âYou are an experienced analyst in social network moderation. Please analyse whether the following content is positive, neutral or negativeâ
},
{
prompt: âEmojiâ,
systemMessage: âPlease provide an emoji reflecting each of the following contentâ
}
];
var selectedPrompt = standardPrompts.find(function (promptObj) {
return promptObj.prompt == promptText;
});
if (selectedPrompt) {
var result = CHAT(selectedPrompt.systemMessage + â\nâ + input);
sheet.getRange(outputRange).setValue(result);
} else {
throw new Error(âInvalid prompt selected. Please try again.â);
}
}>