louisbrulenaudet commited on
Commit
d5392c2
1 Parent(s): 9968058

Update code.gs

Browse files

Fixed payloadString bug: Corrected JSON stringification of payload.

Files changed (1) hide show
  1. code.gs +12 -17
code.gs CHANGED
@@ -1,7 +1,4 @@
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
  /**
@@ -35,7 +32,7 @@ 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=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.
@@ -46,23 +43,21 @@ function queryHuggingFace(prompt, model = DEFAULT_MODEL, systemPrompt = DEFAULT_
46
  throw new Error('Please enter your Hugging Face API key using the menu.');
47
  }
48
 
49
- const url = `https://api-inference.huggingface.co/models/${model}`;
50
- const headers = {
51
- "Authorization": `Bearer ${apiKey}`,
52
- "Content-Type": "application/json"
53
- };
54
-
55
-
56
  const formattedPrompt = `<s> [INST] ${systemPrompt} [/INST] ${prompt} </s>`;
57
  const payload = {
58
  "inputs": formattedPrompt
59
  };
60
 
 
61
 
 
62
  const options = {
63
- "method": "post",
64
- "headers": headers,
65
- "payload": JSON.stringify(payload)
 
 
 
66
  };
67
 
68
  try {
@@ -76,11 +71,11 @@ function queryHuggingFace(prompt, model = DEFAULT_MODEL, systemPrompt = DEFAULT_
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) {
 
 
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
  /**
 
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.
 
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 {
 
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) {