Skip to content

Commit

Permalink
feat: Google Calendar (#280)
Browse files Browse the repository at this point in the history
* feat: Google Calendar tool working

* feat: Allow for enable disable calendar tool

* feat: better env checks and .env.example

* Update README.md

* Bump version to 2.4.0
  • Loading branch information
Luisotee authored Mar 5, 2024
1 parent 0fe77ae commit 040e6a3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
13 changes: 12 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ TRANSCRIPTION_LANGUAGE="auto" # Example: "pt" (portuguese), "en" (english), "es
# OpenRouter Features:
# ------------------------------

# Enable or disable Google Calendar. If enabled, the bot will be able to create and manage events in your Google Calendar.
# You will need to use OpenAI's GPT model to use this feature. Therefore, you will need to have an OpenAI API key.
# Accepted values are "true" or "false"
ENABLE_GOOGLE_CALENDAR="false"

# Google Calendar credentials.
# Refer here to learn how to get these: https://github.com/nearform/langchain-google-calendar/blob/master/docs/setup.md
GOOGLE_CALENDAR_CLIENT_EMAIL=""
GOOGLE_CALENDAR_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\\n-----END PRIVATE KEY-----\n"
GOOGLE_CALENDAR_CALENDAR_ID=""

# This is the API that OpenRouter will use to search in the web for information.
# You can get one at https://www.searchapi.io/
# Leave this empty if you're not going to use it
Expand All @@ -69,7 +80,7 @@ OPENROUTER_MEMORY_TYPE="summary"
# Some LLMs have problems with summary, so be careful when choosing one.
# GPT works well with summary, Claude has problems with it.
# The one here will usually be an okay model with free cost, but be careful because pricing may change.
SUMMARY_LLM_MODEL="gryphe/mythomist-7b"
SUMMARY_LLM_MODEL="gryphe/mythomist-7b:free"

# ==============================
# Optional Environment Variables
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Welcome to the WhatsApp AI Assistant repository, where you'll find a remarkable
| Feature | Sydney (BingAI Jailbreak) | OpenRouter Models* |
| :-------------------------- | :-----------------------: | :----------------: |
| Google/Bing Searching |||
| Google Calendar || |
| Google Calendar || |
| Google Places |||
| Gmail |||
| Communication Capability |||
Expand Down Expand Up @@ -105,7 +105,9 @@ cp .env.example .env

4. Read and fill in the remaining information in the `.env` file.

5. Run
5. Instructions on how to use langchain tools like Google Calendar and search will be in the `.env`

6. Run

```
pnpm build
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatsapp-ai-assistant",
"version": "2.3.0",
"version": "2.4.0",
"description": "WhatsApp chatbot",
"module": "src/index.ts",
"type": "module",
Expand Down
45 changes: 43 additions & 2 deletions src/clients/tools-openrouter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { SearchApi } from "@langchain/community/tools/searchapi";
import {
ENABLE_GOOGLE_CALENDAR,
GOOGLE_CALENDAR_CALENDAR_ID,
GOOGLE_CALENDAR_CLIENT_EMAIL,
GOOGLE_CALENDAR_PRIVATE_KEY,
OPENAI_API_KEY,
OPENROUTER_API_KEY,
SEARCH_API
} from "../constants";
import { WebBrowser } from "langchain/tools/webbrowser";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { ChatOpenAI, OpenAI, OpenAIEmbeddings } from "@langchain/openai";
import {
GoogleCalendarCreateTool,
GoogleCalendarViewTool,
} from "@langchain/community/tools/google_calendar";

const OPENROUTER_BASE_URL = "https://openrouter.ai";

Expand All @@ -18,6 +27,33 @@ const model = new ChatOpenAI(
basePath: `${OPENROUTER_BASE_URL}/api/v1`,
}
);
let googleCalendarCreateTool = null;
let googleCalendarViewTool = null;


if (ENABLE_GOOGLE_CALENDAR === "true") {
const googleCalendarModel = new OpenAI({
temperature: 0,
openAIApiKey: OPENAI_API_KEY,
});

const googleCalendarParams = {
credentials: {
clientEmail: GOOGLE_CALENDAR_CLIENT_EMAIL,
privateKey: GOOGLE_CALENDAR_PRIVATE_KEY,
calendarId: GOOGLE_CALENDAR_CALENDAR_ID,
},
scopes: [
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.events",
],
model: googleCalendarModel,
};

googleCalendarCreateTool = new GoogleCalendarCreateTool(googleCalendarParams);
googleCalendarViewTool = new GoogleCalendarViewTool(googleCalendarParams);
}


const embeddings = new OpenAIEmbeddings();

Expand All @@ -30,5 +66,10 @@ if (SEARCH_API !== '') {

const webBrowserTool = new WebBrowser({ model, embeddings });

export const tools = [...(searchTool ? [searchTool] : []), webBrowserTool];
export const tools = [
...(searchTool ? [searchTool] : []),
webBrowserTool,
...(googleCalendarCreateTool ? [googleCalendarCreateTool] : []),
...(googleCalendarViewTool ? [googleCalendarViewTool] : []),
];
export const toolNames = tools.map((tool) => tool.name);
5 changes: 4 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ export const DEBUG_SUMMARY = process.env.DEBUG_SUMMARY as string;
export const LOG_MESSAGES = process.env.LOG_MESSAGES as string;
export const SEARCH_API = process.env.SEARCH_API as string;
export const BING_COOKIES = process.env.BING_COOKIES as string;

export const ENABLE_GOOGLE_CALENDAR = process.env.ENABLE_GOOGLE_CALENDAR as string;
export const GOOGLE_CALENDAR_CLIENT_EMAIL = process.env.GOOGLE_CALENDAR_CLIENT_EMAIL as string;
export const GOOGLE_CALENDAR_PRIVATE_KEY = process.env.GOOGLE_CALENDAR_PRIVATE_KEY as string;
export const GOOGLE_CALENDAR_CALENDAR_ID = process.env.GOOGLE_CALENDAR_CALENDAR_ID as string;
12 changes: 12 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ export function checkEnv() {
"SEARCH_API not provided. You must set SEARCH_API. Please check your .env file."
);
}
if (process.env.ENABLE_GOOGLE_CALENDAR === "true") {
if (!process.env.GOOGLE_CALENDAR_CLIENT_EMAIL || !process.env.GOOGLE_CALENDAR_PRIVATE_KEY || !process.env.GOOGLE_CALENDAR_CALENDAR_ID) {
console.warn(
"Google Calendar is enabled but one or more environment variables (GOOGLE_CALENDAR_CLIENT_EMAIL, GOOGLE_CALENDAR_PRIVATE_KEY, GOOGLE_CALENDAR_CALENDAR_ID) are not set. Please check your .env file."
);
}
if (!process.env.OPENAI_API_KEY || process.env.OPENAI_API_KEY === "") {
throw new Error(
`Invalid OPENAI_API_KEY="${process.env.OPENAI_API_KEY}" provided. Please check the OPENAI_API_KEY variable in your .env file.`
);
}
}
}

if (!process.env.BING_COOKIES) {
Expand Down

0 comments on commit 040e6a3

Please sign in to comment.