langchain
langchain copied to clipboard
Problems Using LLM and planner modules with Google Calendar API in Python
I'm trying to use the LLM and planner modules to interact with the Google Calendar API, but I'm facing issues in creating a compatible requests wrapper. I want to create a Google Calendar agent and schedule appointments using the agent.
Here's the code I have tried so far:
import config import os import sys import yaml import datetime from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError from langchain.agents.agent_toolkits.openapi.spec import reduce_openapi_spec from langchain.llms import PromptLayerOpenAI
os.environ["OPENAI_API_KEY"] = config.OPENAI_API_KEY os.environ["PROMPTLAYER_API_KEY"] = config.PROMPTLAYER_API_KEY llm = PromptLayerOpenAI(temperature=0)
from langchain.agents.agent_toolkits.openapi import planner
with open("google_calendar_openapi.yaml") as f: raw_google_calendar_api_spec = yaml.load(f, Loader=yaml.Loader)
google_calendar_api_spec = reduce_openapi_spec(raw_google_calendar_api_spec)
def authenticate_google_calendar(): creds = None if os.path.exists('token.json'): creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/calendar']) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file('credentials.json', ['https://www.googleapis.com/auth/calendar']) creds = flow.run_local_server(port=0) with open('token.json', 'w') as token: token.write(creds.to_json()) return creds
creds = authenticate_google_calendar() service = build('calendar', 'v3', credentials=creds)
class GoogleCalendarRequestsWrapper: def init(self, service): self.service = service
def request(self, method, url, headers=None, json=None):
if method == "POST" and "calendar/v3/calendars" in url:
calendar_id = url.split("/")[-1]
event = self.service.events().insert(calendarId=calendar_id, body=json).execute()
return {"status_code": 200, "json": lambda: event}
google_calendar_requests_wrapper = GoogleCalendarRequestsWrapper(service)
google_calendar_agent = planner.create_openapi_agent( google_calendar_api_spec, google_calendar_requests_wrapper, llm )
def schedule_appointment(agent, calendar_id, appointment_name, start_time, end_time): user_query = f"Create an event named '{appointment_name}' in calendar '{calendar_id}' from '{start_time}' to '{end_time}'" response = agent.run(user_query) return response
calendar_id = "[email protected]" appointment_name = "Haircut Appointment" start_time = "2023-04-25T15:00:00" end_time = "2023-04-25T16:00:00"
response = schedule_appointment( google_calendar_agent, calendar_id, appointment_name, start_time, end_time )
print(response) I'm getting the following error when running the code:
1 validation error for RequestsGetToolWithParsing requests_wrapper value is not a valid dict (type=type_error.dict)
I need assistance in creating a compatible requests wrapper for the Google Calendar API to work with the LLM and planner modules.
Please let me know if you have any suggestions or if there's a better way to create the requests wrapper and use the Google Calendar API with the LLM and planner modules.