10000 GitHub - scriptit-fr/GenAIApp: About The Google Apps Script binding for Gemini and OpenAI generative AI API
[go: up one dir, main page]

Skip to content

About The Google Apps Script binding for Gemini and OpenAI generative AI API

License

Notifications You must be signed in to change notification settings

scriptit-fr/GenAIApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 

Repository files navigation

GenAIApp

The GenAIApp library is a Google Apps Script library designed for creating, managing, and interacting with LLMs using Gemini and OpenAI's API. The library provides features like text-based conversation, browsing the web, image analysis, and more, allowing you to build versatile AI chat applications that can integrate with various functionalities and external data sources.

Table of Contents

Features

  • Chat Creation: Create interactive chats that can send and receive messages using Gemini or OpenAI's API.
  • Web Search Integration: Perform web searches using the Google Custom Search API to enhance chatbot responses.
  • Image Analysis: Retrieve image descriptions using Gemini and OpenAI's vision models.
  • Function Calling: Enable the chat to call predefined functions and utilize their results in conversations.
  • Assistant Knowledge Retrieval: Retrieve knowledge from OpenAI vector search assistants for a better contextual response.
  • Document Analysis: Analyze documents from Google Drive with support for various formats.

Prerequisites

The setup for GenAIApp varies depending on which models you plan to use:

  1. If you want to use OpenAI models: You'll need an OpenAI API key
  2. If you want to use Google Gemini models: you’ll need a Google Cloud Platform (GCP) project with Vertex AI enabled for access to Gemini models. Ensure to link your Google Apps Script project to a GCP project with Vertex AI enabled, and to include the following scopes in your manifest file:
"oauthScopes": [
    "https://www.googleapis.com/auth/cloud-platform",
    "https://www.googleapis.com/auth/script.external_request"
  ]
  1. An OpenAI API key for accessing OpenAI models.
  2. A Gemini API key OR a Google Cloud Platform (GCP) project for using Gemini models.
  3. (Optionnal) A Google Custom Search API key for utilizing the Google Custom Search API (if web browsing is enabled).

Installation

To start using the library, include the GenAIApp code in your Google Apps Script project environment.

Usage

Setting API Keys

You need to set your API keys before starting any chat:

// Set Gemini API Key
GenAIApp.setGeminiAPIKey('your-gemini-api-key');

// Set Gemini Auth if using Google Cloud
GenAIApp.setGeminiAuth('your-gcp-project-id','your-region');

// Set OpenAI API Key if using OpenAI
GenAIApp.setOpenAIAPIKey('your-openai-api-key');

// Set Google Search API Key (optional, for web browsing)
GenAIApp.setGoogleSearchAPIKey('your-google-search-api-key');

Creating a New Chat

To start a new chat, call the newChat() method. This creates a new Chat instance.

let chat = GenAIApp.newChat();

Adding Messages

You can add messages to your chat using the addMessage() method. Messages can be from the user or the system.

// Add a user message
chat.addMessage("Hello, how are you?");

// Add a system message (optional)
chat.addMessage("Answer to the user in a professional way.", true);

Adding callable Functions to the Chat

You can create and add functions to the chat that the AI can call during the conversation: The newFunction() method allows you to create a new Function instance. You can then add this function to your chat using the addFunction() method.

// Create a new function
let myFunction = GenAIApp.newFunction()
  .setName("getWeather")
  .setDescription("Retrieve the current weather for a given city.")
  .addParameter("city", "string", "The name of the city.");

// Add the function to the chat
chat.addFunction(myFunction);

From the moment that you add a function to chat, we will use function calling features. For more information :

Enable web browsing (optional)

If you want to allow the chat to perform web searches and fetch web pages, you can either:

  • run the chat with a Gemini model
  • if you're using OpenAI's model, you'll need to enable browsing (and set your Google Custom Search API key, as desbribed above)
chat.enableBrowsing(true);

If want to restrict your browsing to a specific web page, you can add as a second argument the url of this web page as bellow.

  chat.enableBrowsing(true, "https://support.google.com");

Give a web page as a knowledge base (optional)

If you don't need the perform a web search and want to directly give a link for a web page you want the chat to read before performing any action, you can use the addKnowledgeLink(url) function.

  chat.addKnowledgeLink("https://developers.google.com/apps-script/guides/libraries");

Enable Vision (optional)

To enable the chat model to describe images, use the enableVision() method

chat.enableVision(true);

Running the Chat

Once you've set up the chat and added the necessary components, you can start the conversation by calling the run() method.

let response = chat.run({
  model: "gemini-1.5-pro-002", // Optional: set the mod
8000
el to use
  temperature: 0.5 // Optional: set response creativity
  function_call: "getWeather" // Optional: force the first API response to call a function
});

console.log(response);

The library supports the following models:

  1. Gemini: "gemini-1.5-pro-002" | "gemini-1.5-pro" | "gemini-1.5-flash-002" | "gemini-1.5-flash"
  2. OpenAI: "gpt-3.5-turbo" | "gpt-4" | "gpt-4-turbo" | "gpt-4o" | "gpt-4o-mini"

⚠️ Warning: the "function_call" advanced parameter is only supported by OpenAI models, gemini-1.5-pro and gemini-1.5-flash

FunctionObject Class

Creating a Function

The FunctionObject class represents a callable function by the chatbot. It is highly customizable with various options:

let functionObject = GenAIApp.newFunction()
  .setName("searchMovies")
  .setDescription("Search for movies based on a genre.")
  .addParameter("genre", "string", "The genre of movies to search for.");

Configuring Parameters

The function parameters can be configured to be required or optional:

// Adding required parameter
functionObject.addParameter("year", "number", "The year of the movie release.");

// Adding optional parameter
functionObject.addParameter("rating", "number", "The minimum rating of movies to return.", true);

Advanced Options

Retrieving Knowledge from an OpenAI Assistant

Retrieve contextual information from a specific OpenAI vector search assistant:

chat.retrieveKnowledgeFromAssistant("assistant-id", "A description of available knowledge.");

To find out more : https://platform.openai.com/docs/assistants/overview

Analyzing Documents with an OpenAI Assistant

Analyze a document from Google Drive using an assistant:

chat.analyzeDocumentWithAssistant("assistant-id", "drive-file-id");

Examples

Example 1 : Send a prompt and get completion

 ChatGPTApp.setOpenAIAPIKey(OPEN_AI_API_KEY);

 const chat = ChatGPTApp.newChat();
 chat.addMessage("What are the steps to add an external library to my Google Apps Script project?");

 const chatAnswer = chat.run();
 Logger.log(chatAnswer);

Example 2 : Ask Open AI to create a draft reply for the last email in Gmail inbox

 ChatGPTApp.setOpenAIAPIKey(OPEN_AI_API_KEY);
 const chat = ChatGPTApp.newChat();

 var getLatestThreadFunction = ChatGPTApp.newFunction()
    .setName("getLatestThread")
    .setDescription("Retrieve information from the last message received.");

 var createDraftResponseFunction = ChatGPTApp.newFunction()
    .setName("createDraftResponse")
    .setDescription("Create a draft response.")
    .addParameter("threadId", "string", "the ID of the thread to retrieve")
    .addParameter("body", "string", "the body of the email in plain text");

  var resp = ChatGPTApp.newChat()
    .addMessage("You are an assistant managing my Gmail inbox.", true)
    .addMessage("Retrieve the latest message I received and draft a response.")
    .addFunction(getLatestThreadFunction)
    .addFunction(createDraftResponseFunction)
    .run();

  console.log(resp);

Example 3 : Retrieve structured data instead of raw text with onlyReturnArgument()

const ticket = "Hello, could you check the status of my subscription under customer@example.com";

  chat.addMessage("You just received this ticket : " + ticket);
  chat.addMessage("What's the customer email address ? You will give it to me using the function getEmailAddress.");

  const myFunction = ChatGPTApp.newFunction() // in this example, getEmailAddress is not actually a real function in your script
    .setName("getEmailAddress")
    .setDescription("To give the user an email address")
    .addParameter("emailAddress", "string", "the email address")
    .onlyReturnArguments(true) // you will get your parameters in a json object

  chat.addFunction(myFunction);

  const chatAnswer = chat.run();
  Logger.log(chatAnswer["emailAddress"]); // the name of the parameter of your "fake" function

  // output : 	"customer@example.com"

Example 4 : Use web browsing

 const message = "You're a google support agent, a customer is asking you how to install a library he found on github in a google appscript project."

 const chat = ChatGPTApp.newChat();
 chat.addMessage(message);
 chat.addMessage("Browse this website to answer : https://developers.google.com/apps-script", true)
 chat.enableBrowsing(true);

 const chatAnswer = chat.run();
 Logger.log(chatAnswer);

Example 5 : Describe an Image

To have the chat model describe an image:

const chat = ChatGPTApp.newChat();
chat.enableVision(true);
chat.addMessage("Describe the following image.");
chat.addImage("https://example.com/image.jpg", "high");
const response = chat.run();
Logger.log(response);

This will enable the vision capability and use the OpenAI model to provide a description of the image at the specified URL. The fidelity parameter can be "low" or "high", affecting the detail level of the description.

Example 6 : Access Google Sheet Content

To retrieve data from a Google Sheet:

const chat = ChatGPTApp.newChat();
chat.enableGoogleSheetsAccess(true);
chat.addMessage("What data is stored in the following spreadsheet?");
const spreadsheetId = "your_spreadsheet_id_here";
chat.run({
  function_call: "getDataFromGoogleSheets",
  arguments: { spreadsheetId: spreadsheetId }
});
const response = chat.run();
Logger.log(response);

This example demonstrates how to enable access to Google Sheets and retrieve data from a specified spreadsheet.

Debugging and Logging

To debug the chat and view the search queries and pages opened:

let debugInfo = GenAIApp.debug(chat);

// Get web search queries
let webSearchQueries = debugInfo.getWebSearchQueries();

// Get web pages opened
let webPagesOpened = debugInfo.getWebPagesOpened();

console.log(webSearchQueries, webPagesOpened);

Contributing

Contributions are welcome! If you find any bugs, have feature requests, or want to contribute code, please submit an issue or pull request on this GitHub repository.

License

The GenAIApp library is licensed under the Apache License, Version 2.0. You may not use this file except in compliance with the License. For more details, please see the LICENSE.

Reference

Function Object

A FunctionObject represents a function that can be called by the chat.

Creating a function object and setting its name to the name of an actual function you have in your script will permit the library to call your real function.

setName(name)

Sets the name of the function.

setDescription(description)

Sets the description of the function.

addParameter(name, type, description, [isOptional])

Adds a parameter to the function. Parameters are required by default. Set 'isOptional' to true to make a parameter optional.

endWithResult(bool)

If enabled, the conversation with the chat will automatically end after this function is executed.

onlyReturnArguments(bool)

If enabled, the conversation will automatically end when this function is called and the chat will return the arguments in a stringified JSON object.

toJSON()

Returns a JSON representation of the function object.

Chat

A Chat represents a conversation with the chat.

addMessage(messageContent, [system])

Add a message to the chat. If system is true, the message is from the system, else it's from the user.

addFunction(functionObject)

Add a function to the chat.

enableBrowsing(bool)

Enable the chat to use a Google search engine to browse the web.

run([advancedParametersObject])

Start the chat conversation. It sends all your messages and any added function to the chat GPT. It will return the last chat answer.

Supported attributes for the advanced parameters :

advancedParametersObject = {
	temperature: temperature, 
	model: model,
	function_call: function_call
}

Temperature : Lower values for temperature result in more consistent outputs, while higher values generate more diverse and creative results. Select a temperature value based on the desired trade-off between coherence and creativity for your specific application.

Model : The Gemini and OpenAI API are powered by a diverse set of models with different capabilities and price points.

Function_call : If you want to force the model to call a specific function you can do so by setting function_call: "<insert-function-name>".

Note

If you wish to disable the library logs and keep only your own, call disableLogs():

ChatGPTApp.disableLogs();

This can be useful for keeping your logs clean and specific to your application.


Happy coding and enjoy building with the GenAIApp library!

About

About The Google Apps Script binding for Gemini and OpenAI generative AI API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0