Utilize Free AI within Spreadsheet
Are you tired of managing lengthy cell processing tasks in your spreadsheet? Today, AI is impressively powerful, capable of liberating us from tedious, repetitive tasks. In this video, I'll be showing you how to leverage a free AI service within your Google spreadsheets.
To utilize Cloudflare's complimentary AI features, you'll need to have an existing Cloudflare account or create one if you haven't already. Once you're logged into your Cloudflare account, navigate to the Works AI feature. From here, generate an API token specifically for your Workers AI. Make sure to note down your Cloudflare Account ID and AI API Token, as we'll be needing them later.
Let's dive into Google Spreadsheets! We have the option to start from scratch with a brand new blank spreadsheet or open an existing one. To do this, head to the menu above and click on 'Extensions', followed by 'Apps Script'.
The Code.gs
file is now open, and an empty function named myFunction
is waiting to be filled with code.
Let's create a new function and name it LLM. then we copy the code into the LLM function.
Google Apps Script code:
/**
* Use meta LLM to process string.
*
* @param {string} prompt The text to LLM.
* @param {string} user user extra prompt.
* @param {string} system system prompt.
* @return LLM response string.
* @customfunction
*/
function LLM(prompt = "",
user="correct above text",
system="you are a great grammar writer.") {
prompt = prompt.trim();
user = user.trim();
system = prompt.trim();
if(!prompt)return;
const CLOUDFLARE_ACCOUNT_ID = 'YOUR_CF_USER_ID';
const CLOUDFLARE_API_TOKEN = 'YOUR_CF_API_TOKEN';//beware of the token leaking by share the spreadsheet
const url = 'https://api.cloudflare.com/client/v4/accounts/' + CLOUDFLARE_ACCOUNT_ID + '/ai/run/@cf/meta/llama-3-8b-instruct';
const payload = {
"temperature":0.95,
"messages": [
{ "role": "system", "content": system },
{ "role": "user", "content": prompt + "\n\n"+user}
]
};
const options = {
'method': 'post',
'contentType': 'application/json',
'headers': {
'Authorization': 'Bearer ' + CLOUDFLARE_API_TOKEN
},
'payload': JSON.stringify(payload)
};
try {
const response = UrlFetchApp.fetch(url, options);
const json = JSON.parse(response.getContentText());
Logger.log(json);
return json.result.response;
} catch (e) {
Logger.log(e)
return e.message;
}
}
Let's explain how the code works. The function takes one required parameter and two optional parameters. The required parameter should be a string containing the cell reference you want to convert. The other two parameters are optional with default prompts to simplify the function's use.
We use the Cloudflare user id and cloudflare api token to authenticate the request. You can choose other suitable AI models after you find available models from the Cloudflare Workers AI API documentation.
After the HTTP request is made, we parse the JSON response and return the result from Cloudflare Workers AI.
The code explanation is complete. Now, let's save the code and name the project, be sure to click the Save
button.
Now, switch to your Google Sheets window. You can use the LLM function just as you would any other Google Sheets built-in function.
In the fx
=LLM(C4)
I've got a screenshot that shows how I typically use it.
When sharing your spreadsheet or Cloudflare account, be cautious not to compromise your AI API token or inadvertently impact your Cloudflare expenses.
So, share your spreadsheet cautiously.
Was this article helpful to you?
Provide feedback
Last edited on December 07, 2024.
Edit this page