Skip to content

Commit

Permalink
Create Tool to get list of events from all Google calendars
Browse files Browse the repository at this point in the history
  • Loading branch information
DEENUU1 committed Jan 28, 2024
1 parent a902a60 commit 0dba7e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
46 changes: 42 additions & 4 deletions app/ai/tools/google_calendar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Type, Optional
from typing import Type, Optional, List

import requests
from langchain_core.tools import BaseTool
Expand All @@ -8,7 +8,6 @@


class GoogleCalendarCreateEventInput(BaseModel):
"""Inputs for creating Notion Note page """
calendar_type: str = Field(
description=f"Type of calendar in which you can create event, choose based on context or user input. "
f"You can use: {settings.GOOGLE_CALENDAR_NAMES}"
Expand All @@ -22,6 +21,10 @@ class GoogleCalendarCreateEventInput(BaseModel):
)


class GoogleCalendarListEventInput(BaseModel):
start_date: str = Field(description="Start date of the event in format 'YYYY-MM-DD'")


class GoogleCalendarCreateEventTool(BaseTool):
name = "google_calendar_create_event_tool"
description = "Useful when you need to create event in Google Calendar, use current_time_tool to get current date"
Expand All @@ -34,22 +37,57 @@ def _run(
all_day: bool,
start_date: str,
end_date: Optional[str] = None,
duration: Optional[str] = None
duration: Optional[str] = None,
):
try:
return requests.post(
settings.MAKE_GOOGLE_CALENDAR_CREATE_EVENT,
settings.MAKE_GOOGLE_CALENDAR_CREATE_LIST_EVENT,
data={
"calendar_type": calendar_type,
"event_name": event_name,
"all_day": all_day,
"start_date": start_date,
"end_date": end_date,
"duration": duration,
"operation": "create" # hard coded value based on routing in make.com
}
)
except Exception as e:
print(e)

def _arun(self, url: str):
raise NotImplementedError("error here")


class GoogleCalendarListEventTool(BaseTool):
name = "google_calendar_list_event_tool"
description = "Useful when you need to answer about events in Google Calendar"
args_schema: Type[BaseModel] = GoogleCalendarListEventInput

def _run(
self,
start_date: str,
):
return self.get_events_from_all_calendars(start_date=start_date)

def _arun(self, url: str):
raise NotImplementedError("error here")

@staticmethod
def get_events_from_all_calendars(start_date: str) -> List:
result = []

calendars = settings.GOOGLE_CALENDAR_NAMES.split(",")
for calendar in calendars:
response = requests.post(
settings.MAKE_GOOGLE_CALENDAR_CREATE_LIST_EVENT,
data={
"start_date": start_date,
"calendar_type": calendar,
"operation": "list" # Hard coded value based on routing in make.com,
}
)
result.append(response.json())

return result

2 changes: 1 addition & 1 deletion app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Settings(BaseSettings):
GOOGLE_CALENDAR_DEBUG: bool = os.getenv("GOOGLE_CALENDAR_DEBUG") == "True"

MAKE_NOTION_CREATE_NOTE: Optional[str] = os.getenv("MAKE_NOTION_CREATE_NOTE")
MAKE_GOOGLE_CALENDAR_CREATE_EVENT: Optional[str] = os.getenv("MAKE_GOOGLE_CALENDAR_CREATE_EVENT")
MAKE_GOOGLE_CALENDAR_CREATE_LIST_EVENT: Optional[str] = os.getenv("MAKE_GOOGLE_CALENDAR_CREATE_LIST_EVENT")
GOOGLE_CALENDAR_NAMES: Optional[str] = os.getenv("GOOGLE_CALENDAR_NAMES")


Expand Down

0 comments on commit 0dba7e6

Please sign in to comment.