louisbrulenaudet
commited on
Commit
•
9968058
1
Parent(s):
3801376
Update code.gs
Browse filesHi Florent,
Thanks a lot for the comment, it's been corrected normally, I can't wait to think about the next steps and new functions, it's really a great project 🤗
https://cdn-uploads.huggingface.co/production/uploads/6459fa0f5b3111fbe83286e1/e9q5ueTV3gOTiF9J47AU5.qt
code.gs
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
// Default system prompt used if none is provided
|
2 |
const DEFAULT_SYSTEM_PROMPT = 'You are a helpful and honest assistant. Please, respond concisely and truthfully.';
|
3 |
|
4 |
-
// Base
|
5 |
-
const
|
6 |
|
7 |
/**
|
8 |
* Adds a custom menu to Google Sheets to allow users to input their Hugging Face API key.
|
@@ -35,12 +35,12 @@ function showApiKeyPrompt() {
|
|
35 |
* Sends a request to the Hugging Face API with the specified prompt and model.
|
36 |
*
|
37 |
* @param {string} prompt - The input prompt to be sent to the model.
|
38 |
-
* @param {string} model - The model ID to query (e.g., 'mistralai/Mistral-7B-Instruct-v0.3').
|
39 |
* @param {string} [systemPrompt=DEFAULT_SYSTEM_PROMPT] - The system prompt to customize the assistant's behavior.
|
40 |
* @returns {object} JSON response from the Hugging Face API.
|
41 |
* @throws {Error} If the API key is not set or if the API request fails.
|
42 |
*/
|
43 |
-
function queryHuggingFace(prompt, model, systemPrompt = DEFAULT_SYSTEM_PROMPT) {
|
44 |
const apiKey = PropertiesService.getScriptProperties().getProperty('HF_API_KEY');
|
45 |
if (!apiKey) {
|
46 |
throw new Error('Please enter your Hugging Face API key using the menu.');
|
@@ -53,7 +53,7 @@ function queryHuggingFace(prompt, model, systemPrompt = DEFAULT_SYSTEM_PROMPT) {
|
|
53 |
};
|
54 |
|
55 |
|
56 |
-
const formattedPrompt =
|
57 |
const payload = {
|
58 |
"inputs": formattedPrompt
|
59 |
};
|
@@ -76,14 +76,14 @@ function queryHuggingFace(prompt, model, systemPrompt = DEFAULT_SYSTEM_PROMPT) {
|
|
76 |
|
77 |
/**
|
78 |
* Custom Google Sheets formula to query the Hugging Face API and return the generated text.
|
79 |
-
* Function to create the custom formula =HF(prompt
|
80 |
*
|
81 |
* @param {string} prompt - The input prompt to be sent to the model.
|
82 |
-
* @param {string} model - The model ID to query (e.g., 'mistralai/Mistral-7B-Instruct-v0.3').
|
83 |
-
* @param {string} [systemPrompt] - The system prompt to customize the assistant's behavior. Defaults to DEFAULT_SYSTEM_PROMPT.
|
84 |
* @returns {string} The generated output text from the Hugging Face API, or an error message if the request fails.
|
85 |
*/
|
86 |
-
function HF(prompt, model, systemPrompt) {
|
87 |
try {
|
88 |
const response = queryHuggingFace(prompt, model, systemPrompt);
|
89 |
if (response && response.length > 0 && response[0].generated_text) {
|
@@ -111,4 +111,4 @@ function onInstall(e) {
|
|
111 |
.setFunction('HF')
|
112 |
.build();
|
113 |
SpreadsheetApp.installUserDefinedFunction(formula);
|
114 |
-
}
|
|
|
1 |
// Default system prompt used if none is provided
|
2 |
const DEFAULT_SYSTEM_PROMPT = 'You are a helpful and honest assistant. Please, respond concisely and truthfully.';
|
3 |
|
4 |
+
// Base model for Hugging Face API
|
5 |
+
const DEFAULT_MODEL = "mistralai/Mistral-7B-Instruct-v0.3";
|
6 |
|
7 |
/**
|
8 |
* Adds a custom menu to Google Sheets to allow users to input their Hugging Face API key.
|
|
|
35 |
* Sends a request to the Hugging Face API with the specified prompt and model.
|
36 |
*
|
37 |
* @param {string} prompt - The input prompt to be sent to the model.
|
38 |
+
* @param {string} [model=DEFAULT_MODEL] - The model ID to query (e.g., 'mistralai/Mistral-7B-Instruct-v0.3').
|
39 |
* @param {string} [systemPrompt=DEFAULT_SYSTEM_PROMPT] - The system prompt to customize the assistant's behavior.
|
40 |
* @returns {object} JSON response from the Hugging Face API.
|
41 |
* @throws {Error} If the API key is not set or if the API request fails.
|
42 |
*/
|
43 |
+
function queryHuggingFace(prompt, model = DEFAULT_MODEL, systemPrompt = DEFAULT_SYSTEM_PROMPT) {
|
44 |
const apiKey = PropertiesService.getScriptProperties().getProperty('HF_API_KEY');
|
45 |
if (!apiKey) {
|
46 |
throw new Error('Please enter your Hugging Face API key using the menu.');
|
|
|
53 |
};
|
54 |
|
55 |
|
56 |
+
const formattedPrompt = `<s> [INST] ${systemPrompt} [/INST] ${prompt} </s>`;
|
57 |
const payload = {
|
58 |
"inputs": formattedPrompt
|
59 |
};
|
|
|
76 |
|
77 |
/**
|
78 |
* Custom Google Sheets formula to query the Hugging Face API and return the generated text.
|
79 |
+
* Function to create the custom formula =HF(prompt; [model]; [systemPrompt])
|
80 |
*
|
81 |
* @param {string} prompt - The input prompt to be sent to the model.
|
82 |
+
* @param {string} [model=DEFAULT_MODEL] - The model ID to query (e.g., 'mistralai/Mistral-7B-Instruct-v0.3').
|
83 |
+
* @param {string} [systemPrompt=DEFAULT_SYSTEM_PROMPT] - The system prompt to customize the assistant's behavior. Defaults to DEFAULT_SYSTEM_PROMPT.
|
84 |
* @returns {string} The generated output text from the Hugging Face API, or an error message if the request fails.
|
85 |
*/
|
86 |
+
function HF(prompt, model = DEFAULT_MODEL, systemPrompt = DEFAULT_SYSTEM_PROMPT) {
|
87 |
try {
|
88 |
const response = queryHuggingFace(prompt, model, systemPrompt);
|
89 |
if (response && response.length > 0 && response[0].generated_text) {
|
|
|
111 |
.setFunction('HF')
|
112 |
.build();
|
113 |
SpreadsheetApp.installUserDefinedFunction(formula);
|
114 |
+
}
|