From 9412b84bb8a411c07ad90daa98cbf44612d48429 Mon Sep 17 00:00:00 2001 From: Justin Xiao Date: Wed, 27 Sep 2023 01:46:24 +0800 Subject: [PATCH] feat: add google calendar tool --- requirements.txt | 2 +- src/app/flask_postgresql/configs.py | 2 +- src/infrastructure/tools/__init__.py | 2 + src/infrastructure/tools/google_calendar.py | 70 +++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/infrastructure/tools/google_calendar.py diff --git a/requirements.txt b/requirements.txt index 6f056cb..ab0638f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,4 +21,4 @@ psycopg2==2.9.7 openai==0.27.8 tiktoken==0.4.0 google-search-results==2.4.2 -flake8==6.1.0 \ No newline at end of file +flake8==6.1.0 diff --git a/src/app/flask_postgresql/configs.py b/src/app/flask_postgresql/configs.py index 26ec6f5..33dfb52 100644 --- a/src/app/flask_postgresql/configs.py +++ b/src/app/flask_postgresql/configs.py @@ -24,7 +24,7 @@ class Config(object): SERVICE_PREFIX = os.environ.get("SERVICE_PREFIX", "") CHANNEL_SECRET = os.getenv("CHANNEL_SECRET", "") CHANNEL_ACCESS_TOKEN = os.getenv("CHANNEL_ACCESS_TOKEN", "") - PORT = os.getenv("PORT", 5000) + PORT = int(os.getenv("PORT", 5000)) SERPAPI_API_KEY = os.getenv("SERPAPI_API_KEY", "") CHATBOT_DESCRIPTION = os.getenv("CHATBOT_DESCRIPTION", "") CHATBOT_LANGUAGE = os.getenv("CHATBOT_LANGUAGE", "") diff --git a/src/infrastructure/tools/__init__.py b/src/infrastructure/tools/__init__.py index f58f44b..87f3d5f 100644 --- a/src/infrastructure/tools/__init__.py +++ b/src/infrastructure/tools/__init__.py @@ -3,6 +3,7 @@ from src.app.flask_postgresql.configs import Config +from .google_calendar import GoogleCalendarTool from .stock import CurrentStockPriceTool, StockPerformanceTool # initialize LangChain services @@ -16,4 +17,5 @@ ), CurrentStockPriceTool(), StockPerformanceTool(), + GoogleCalendarTool(), ] diff --git a/src/infrastructure/tools/google_calendar.py b/src/infrastructure/tools/google_calendar.py new file mode 100644 index 0000000..d2dac2d --- /dev/null +++ b/src/infrastructure/tools/google_calendar.py @@ -0,0 +1,70 @@ +import datetime +import urllib +from typing import Optional, Type + +from langchain.tools import BaseTool +from pydantic import BaseModel, Field + + +def create_gcal_url( + title="What?", date="20230524T180000/20230524T220000", location="Where?", description="" +): + """ + Generate a Google Calendar URL for creating a new event. + + Args: + title (str): The title of the event. Defaults to "What?". + date (str): The date and time of the event in the format "yyyyMMddTHHmmss/yyyyMMddTHHmmss". + Defaults to "20230524T180000/20230524T220000". + location (str): The location of the event. Defaults to "Where?". + description (str): The description of the event. Defaults to an empty string. + + Returns: + str: The URL for creating a new event in Google Calendar. + + """ + + base_url = "https://www.google.com/calendar/render?action=TEMPLATE" + event_url = f"{base_url}&text={urllib.parse.quote(title)}&dates={date}\ + &location={urllib.parse.quote(location)}&details={urllib.parse.quote(description)}" + + return event_url + "&openExternalBrowser=1" + + +class GoogleCalendarGeneratorInput(BaseModel): + """Input for Google Calendar Generator.""" + + dates: str = Field( + ..., + description=f"Datetime symbol if text contained. format should be \ + 'YYYYMMDDTHHMMSS/YYYYMMDDTHHMMSS'. Current time is {datetime.date.today()}", + ) + title: str = Field(..., description="Calendar Title symbol for reserve schedule.") + description: str = Field( + ..., description="Calendar Summary text symbol for schedule description." + ) + location: str = Field(..., description="Calendar location symbol for reservation.") + + +class GoogleCalendarTool(BaseTool): + name = "google_calendar_reservation" + description = "Generate Google Calendar url from user text first when containing time, date." + args_schema: Optional[Type[BaseModel]] = GoogleCalendarGeneratorInput + + def _run(self, dates: str, title: str, description: str, location: str): + """ + Generates a Google Calendar URL based on the given parameters. + + Args: + dates (str): The dates of the event. + title (str): The title of the event. + description (str): The description of the event. + location (str): The location of the event. + + Returns: + str: The generated Google Calendar URL. + """ + + result = create_gcal_url(title, dates, location, description) + + return result