Coder Social home page Coder Social logo

scriptit-fr / chatgptapp Goto Github PK

View Code? Open in Web Editor NEW
38.0 7.0 7.0 253 KB

The Google Apps Script binding for the OpenAI chat completion API

Home Page: https://romain-vialard.medium.com/introducing-chatgptapp-a-new-library-for-google-apps-script-aee672b8d22a

License: Apache License 2.0

JavaScript 100.00%
chatgpt googleappsscript openai

chatgptapp's Introduction

ChatGPTApp Google Apps Script Library Documentation

The ChatGPTApp is a library that facilitates the integration of OpenAI's GPT into your Google Apps Script projects. It allows for structured conversation, function calling and web browsing capabilities.

Table of Contents

How to use :
Examples :
Reference :

How to use

Setup

To use the ChatGPTApp library, you first need to include the library code in your project. You then need to provide your OpenAI API key via setOpenAIAPIKey().

If you wish to enable browsing capabilities, you will also need to provide your Google API key via setGoogleAPIKey().

ChatGPTApp.setOpenAIAPIKey("Your-OpenAI-API-Key");
ChatGPTApp.setGoogleSearchAPIKey("Your-Google-API-Key"); // if you want to enable browsing

To get an OpenAI API key, visit the OpenAI website, create an account, and navigate to the API section to generate your API key.

To get a Google Custom Search API key (free) you can visit this page.

Create a New Chat

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

let chat = ChatGPTApp.newChat();

Add Messages

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

chat.addMessage("Hello, how are you?");
chat.addMessage("Answer to the user in a professional way.", true);

Add callable Function

The newFunction() method allows you to create a new Function instance. You can then add this function to your chat using the addFunction() method.

let functionObject = ChatGPTApp.newFunction()
  .setName("myFunction")
  .setDescription("This is a test function.")
  .addParameter("arg1", "string", "This is the first argument.");

chat.addFunction(functionObject);

From the moment that you add a function to chat, we will use Open AI's function calling features.

For more information : https://platform.openai.com/docs/guides/gpt/function-calling

Enable web browsing (optional)

If you want to allow the model to perform web searches and fetch web pages, you can enable browsing.

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);

Run the Chat

Once you have set up your chat, you can start the conversation by calling the run() method.

let response = chat.run();

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 [email protected]";

  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 : 	"[email protected]"

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.

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 OpenAI API is 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.

chatgptapp's People

Contributors

guilleminea avatar louisrub avatar romainvialard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

chatgptapp's Issues

Make api key a parameter of runConversation()

I think it makes sense as it's a mandatory parameter and we can't include a default one in the lib (whereas the model for example is an optional parameter as one is set by default).

image

Add endWithResult() function

If enabled for a function and this function is called, return the function result and stop the conversation here.

The default model should be compatible with functions

Currently the default model is "gpt-3.5-turbo" but the models compatible with functions are model == "gpt-3.5-turbo-0613" || model == "gpt-4-0613".
Best to make sure that the default model is compatible with functions.

Improve Page Content when Fetching

For now the web page content is either polluted with html text, either a plain text (no tittle, links or anything).
To improve the quality of the results the goal is to manage to get a a web page content and format it in markdown. That way, OpenAI will have a better idea of what it's reading.

Simplify ChatGPTApp.newMessage()

Always asking for a role is not great and confusing.

In reality only 3 roles are supported, 'system', 'user' and 'assistant'
https://help.openai.com/en/articles/7042661-chatgpt-api-transition-guide

And in pretty much every case, we will only use 'system' or 'user', and the difference between the 2 is not super important.

Maybe we can simply have:
ChatGPTApp.newMessage("Send me a nice message");
The default role will be 'user'.
And if we specifically want to use a system role:
ChatGPTApp.newMessage("Send me a nice message").setSystemInstruction(true);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.