From 66b37d0d6ae27662ccbe8b75bfc913c6a7fdd4c8 Mon Sep 17 00:00:00 2001 From: Steve Bazyl Date: Tue, 7 May 2024 14:18:06 -0600 Subject: [PATCH] Import custom func demo from Next 24 --- ai/custom-func-ai-studio/Code.js | 29 ++++++++++ ai/custom-func-ai-studio/README.md | 32 +++++++++++ ai/custom-func-ai-studio/appsscript.json | 7 +++ ai/custom-func-ai-studio/gemini.js | 72 ++++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 ai/custom-func-ai-studio/Code.js create mode 100644 ai/custom-func-ai-studio/README.md create mode 100644 ai/custom-func-ai-studio/appsscript.json create mode 100644 ai/custom-func-ai-studio/gemini.js diff --git a/ai/custom-func-ai-studio/Code.js b/ai/custom-func-ai-studio/Code.js new file mode 100644 index 000000000..bb887a139 --- /dev/null +++ b/ai/custom-func-ai-studio/Code.js @@ -0,0 +1,29 @@ +/* +Copyright 2024 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + + +/** + * Passes a prompt and a data range to Gemini AI. + * + * @param {range} range The range of cells. + * @param {string} prompt The text prompt as a string or single cell reference. + * @return The Gemini response. + * @customfunction + */ +function gemini(range,prompt) { + prompt = `For the range of cells ${range}, ${prompt}` + return getAiSummary(prompt); +} \ No newline at end of file diff --git a/ai/custom-func-ai-studio/README.md b/ai/custom-func-ai-studio/README.md new file mode 100644 index 000000000..2bc51ec58 --- /dev/null +++ b/ai/custom-func-ai-studio/README.md @@ -0,0 +1,32 @@ +# Google Sheets Custom Function with AI Studio + +## Project Description + +Google Sheets Custom Function to be used as a bound Apps Script project with a Google Sheets Spreadsheet + +## Prerequisites + +* Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled + +## Set up your environment + +1. Create a Cloud Project + 1. Enable Generative Language API - (may skip as is automatically done in step 2) +1. Create a Google Gemini API Key + 1. Navigate to https://aistudio.google.com/app/apikey + 1. Create API key for existing project from step 1 + 1. Copy the generated key for use in the next step. +1. Open an Apps Script Project bound to a Google Sheets Spreadsheet + 1. From Project Settings, change project to GCP project number of Cloud Project from step 1 + 1. Add a Script Property. Enter `api_key` as the property name and use the Gemini API Key as the value +1. Add the project code to Apps Script + +## Usage + +Insert a custom function in Google Sheets, passing a range and a prompt as parameters + +Example: + +``` +=gemini(A1:A10,"Extract colors from the product description") +``` \ No newline at end of file diff --git a/ai/custom-func-ai-studio/appsscript.json b/ai/custom-func-ai-studio/appsscript.json new file mode 100644 index 000000000..f119e8cc1 --- /dev/null +++ b/ai/custom-func-ai-studio/appsscript.json @@ -0,0 +1,7 @@ +{ + "timeZone": "America/Los_Angeles", + "dependencies": { + }, + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8" +} \ No newline at end of file diff --git a/ai/custom-func-ai-studio/gemini.js b/ai/custom-func-ai-studio/gemini.js new file mode 100644 index 000000000..70f3a3067 --- /dev/null +++ b/ai/custom-func-ai-studio/gemini.js @@ -0,0 +1,72 @@ +/* +Copyright 2024 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/** + * Packages prompt and necessary settings, then sends a request to the + * Generative Language API. Returns the text string response, extracted from the + * Gemini AI response object. + * + * @param {string} prompt String representing the prompt for Gemini AI call. + * @return {string} Result of Gemini AI in string format. + */ +function getAiSummary(prompt) { + const data = { + "contents": [{ + "parts": [{ + "text": prompt + }] + }], + "generationConfig": { + "temperature": 0.2, + "topK": 1, + "topP": 1, + "maxOutputTokens": 2048, + "stopSequences": [] + }, + "safetySettings": [ + { + "category": "HARM_CATEGORY_HARASSMENT", + "threshold": "BLOCK_NONE" + }, + { + "category": "HARM_CATEGORY_HATE_SPEECH", + "threshold": "BLOCK_NONE" + }, + { + "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", + "threshold": "BLOCK_NONE" + }, + { + "category": "HARM_CATEGORY_DANGEROUS_CONTENT", + "threshold": "BLOCK_NONE" + } + ] + }; + const options = { + 'method': 'post', + 'contentType': 'application/json', + 'payload': JSON.stringify(data) // Convert the JavaScript object to a JSON string. + }; + + const apiKey = PropertiesService.getScriptProperties().getProperty('api_key'); + let response = UrlFetchApp.fetch('https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' + apiKey, options); + + const payload = JSON.parse(response.getContentText()); + const text = payload.candidates[0].content.parts[0].text; + + return text; + +} \ No newline at end of file