Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import custom func demo from Next 24 #463

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions ai/custom-func-ai-studio/Code.js
Original file line number Diff line number Diff line change
@@ -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);
}
32 changes: 32 additions & 0 deletions ai/custom-func-ai-studio/README.md
Original file line number Diff line number Diff line change
@@ -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")
```
7 changes: 7 additions & 0 deletions ai/custom-func-ai-studio/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"timeZone": "America/Los_Angeles",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
72 changes: 72 additions & 0 deletions ai/custom-func-ai-studio/gemini.js
Original file line number Diff line number Diff line change
@@ -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;

}