Update code.gs (#1)
Browse files- Update code.gs (380137628d02333e448431298cf11c9fe61744fc)
- Update code.gs (9968058f9be0caada9ef6bb204cbd15463f73754)
- Update code.gs (d5392c23e319b05b1828ecf2700581d06454c119)
Co-authored-by: Louis Brulé Naudet <louisbrulenaudet@users.noreply.huggingface.co>
code.gs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
-
const
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
function onOpen() {
|
| 5 |
const ui = SpreadsheetApp.getUi();
|
| 6 |
ui.createMenu('Hugging Sheets')
|
|
@@ -8,63 +11,94 @@ function onOpen() {
|
|
| 8 |
.addToUi();
|
| 9 |
}
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
function showApiKeyPrompt() {
|
| 13 |
const ui = SpreadsheetApp.getUi();
|
| 14 |
const response = ui.prompt('Enter your Hugging Face API Key:');
|
| 15 |
if (response.getSelectedButton() == ui.Button.OK) {
|
| 16 |
-
const apiKey = response.getResponseText();
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
const apiKey = PropertiesService.getScriptProperties().getProperty('HF_API_KEY');
|
| 25 |
if (!apiKey) {
|
| 26 |
throw new Error('Please enter your Hugging Face API key using the menu.');
|
| 27 |
}
|
| 28 |
|
| 29 |
-
const
|
| 30 |
-
const headers = {
|
| 31 |
-
"Authorization": `Bearer ${apiKey}`,
|
| 32 |
-
"Content-Type": "application/json"
|
| 33 |
-
};
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
const formattedPrompt = `<s> [INST] You are a helpful and honest assistant. Please, respond concisely and truthfully. [/INST] ${prompt} </s>`;
|
| 37 |
const payload = {
|
| 38 |
"inputs": formattedPrompt
|
| 39 |
};
|
| 40 |
|
|
|
|
| 41 |
|
|
|
|
| 42 |
const options = {
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
};
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
try {
|
| 57 |
-
const response = queryHuggingFace(prompt, model);
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
} catch (error) {
|
| 63 |
return `Error: ${error.message}`;
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
function onInstall(e) {
|
| 69 |
onOpen(e);
|
| 70 |
const formula = SpreadsheetApp.newUserDefinedFunctionBuilder()
|
|
@@ -72,4 +106,4 @@ function onInstall(e) {
|
|
| 72 |
.setFunction('HF')
|
| 73 |
.build();
|
| 74 |
SpreadsheetApp.installUserDefinedFunction(formula);
|
| 75 |
-
}
|
|
|
|
| 1 |
+
const DEFAULT_SYSTEM_PROMPT = 'You are a helpful and honest assistant. Please, respond concisely and truthfully.';
|
| 2 |
+
const DEFAULT_MODEL = "mistralai/Mistral-7B-Instruct-v0.3";
|
| 3 |
|
| 4 |
+
/**
|
| 5 |
+
* Adds a custom menu to Google Sheets to allow users to input their Hugging Face API key.
|
| 6 |
+
*/
|
| 7 |
function onOpen() {
|
| 8 |
const ui = SpreadsheetApp.getUi();
|
| 9 |
ui.createMenu('Hugging Sheets')
|
|
|
|
| 11 |
.addToUi();
|
| 12 |
}
|
| 13 |
|
| 14 |
+
/**
|
| 15 |
+
* Prompts the user to enter their Hugging Face API key and saves it in the script properties.
|
| 16 |
+
*/
|
| 17 |
function showApiKeyPrompt() {
|
| 18 |
const ui = SpreadsheetApp.getUi();
|
| 19 |
const response = ui.prompt('Enter your Hugging Face API Key:');
|
| 20 |
if (response.getSelectedButton() == ui.Button.OK) {
|
| 21 |
+
const apiKey = response.getResponseText().trim();
|
| 22 |
+
if (apiKey) {
|
| 23 |
+
PropertiesService.getScriptProperties().setProperty('HF_API_KEY', apiKey);
|
| 24 |
+
ui.alert('API Key saved successfully!');
|
| 25 |
+
} else {
|
| 26 |
+
ui.alert('API Key cannot be empty.');
|
| 27 |
+
}
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
+
/**
|
| 32 |
+
* Sends a request to the Hugging Face API with the specified prompt and model.
|
| 33 |
+
*
|
| 34 |
+
* @param {string} prompt - The input prompt to be sent to the model.
|
| 35 |
+
* @param {string} [model] - The model ID to query (e.g., 'mistralai/Mistral-7B-Instruct-v0.3').
|
| 36 |
+
* @param {string} [systemPrompt=DEFAULT_SYSTEM_PROMPT] - The system prompt to customize the assistant's behavior.
|
| 37 |
+
* @returns {object} JSON response from the Hugging Face API.
|
| 38 |
+
* @throws {Error} If the API key is not set or if the API request fails.
|
| 39 |
+
*/
|
| 40 |
+
function queryHuggingFace(prompt, model = DEFAULT_MODEL, systemPrompt = DEFAULT_SYSTEM_PROMPT) {
|
| 41 |
const apiKey = PropertiesService.getScriptProperties().getProperty('HF_API_KEY');
|
| 42 |
if (!apiKey) {
|
| 43 |
throw new Error('Please enter your Hugging Face API key using the menu.');
|
| 44 |
}
|
| 45 |
|
| 46 |
+
const formattedPrompt = `<s> [INST] ${systemPrompt} [/INST] ${prompt} </s>`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
const payload = {
|
| 48 |
"inputs": formattedPrompt
|
| 49 |
};
|
| 50 |
|
| 51 |
+
const payloadString = JSON.stringify(payload);
|
| 52 |
|
| 53 |
+
const url = `https://api-inference.huggingface.co/models/${model}`;
|
| 54 |
const options = {
|
| 55 |
+
'method': 'post',
|
| 56 |
+
'contentType': 'application/json',
|
| 57 |
+
'headers': {
|
| 58 |
+
'Authorization': `Bearer ${apiKey}`
|
| 59 |
+
},
|
| 60 |
+
'payload': payloadString
|
| 61 |
};
|
| 62 |
|
| 63 |
+
try {
|
| 64 |
+
const response = UrlFetchApp.fetch(url, options);
|
| 65 |
+
const json = JSON.parse(response.getContentText());
|
| 66 |
+
return json;
|
| 67 |
+
} catch (error) {
|
| 68 |
+
throw new Error(`Failed to fetch data from Hugging Face API: ${error.message}`);
|
| 69 |
+
}
|
| 70 |
}
|
| 71 |
|
| 72 |
+
/**
|
| 73 |
+
* Custom Google Sheets formula to query the Hugging Face API and return the generated text.
|
| 74 |
+
* Function to create the custom formula =HF(prompt, model, [systemPrompt])
|
| 75 |
+
*
|
| 76 |
+
* @param {string} prompt - The input prompt to be sent to the model.
|
| 77 |
+
* @param {string} [model] - The model ID to query (e.g., 'mistralai/Mistral-7B-Instruct-v0.3').
|
| 78 |
+
* @param {string} [systemPrompt] - The system prompt to customize the assistant's behavior. Defaults to DEFAULT_SYSTEM_PROMPT.
|
| 79 |
+
* @returns {string} The generated output text from the Hugging Face API, or an error message if the request fails.
|
| 80 |
+
*/
|
| 81 |
+
function HF(prompt, model = DEFAULT_MODEL, systemPrompt = DEFAULT_SYSTEM_PROMPT) {
|
| 82 |
try {
|
| 83 |
+
const response = queryHuggingFace(prompt, model, systemPrompt);
|
| 84 |
+
if (response && response.length > 0 && response[0].generated_text) {
|
| 85 |
+
const fullResponse = response[0].generated_text;
|
| 86 |
+
// Extract the part of the response after the prompt
|
| 87 |
+
const generatedOutput = fullResponse.split(`</s>`).pop().trim();
|
| 88 |
+
return generatedOutput;
|
| 89 |
+
} else {
|
| 90 |
+
return 'Error: Invalid response structure from Hugging Face API.';
|
| 91 |
+
}
|
| 92 |
} catch (error) {
|
| 93 |
return `Error: ${error.message}`;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
+
/**
|
| 98 |
+
* Add the formula to Google Sheets.
|
| 99 |
+
*
|
| 100 |
+
* @param {object} e - The event object.
|
| 101 |
+
*/
|
| 102 |
function onInstall(e) {
|
| 103 |
onOpen(e);
|
| 104 |
const formula = SpreadsheetApp.newUserDefinedFunctionBuilder()
|
|
|
|
| 106 |
.setFunction('HF')
|
| 107 |
.build();
|
| 108 |
SpreadsheetApp.installUserDefinedFunction(formula);
|
| 109 |
+
}
|