|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from dotenv import load_dotenv |
| 18 | +from google.adk import Agent |
| 19 | +from google.adk.auth import AuthCredential |
| 20 | +from google.adk.auth import AuthCredentialTypes |
| 21 | +from google.adk.auth import OAuth2Auth |
| 22 | +from google.adk.tools.application_integration_tool.application_integration_toolset import ApplicationIntegrationToolset |
| 23 | +from google.adk.tools.openapi_tool.auth.auth_helpers import dict_to_auth_scheme |
| 24 | +from google.genai import types |
| 25 | + |
| 26 | +# Load environment variables from .env file |
| 27 | +load_dotenv() |
| 28 | + |
| 29 | +connection_name = os.getenv("CONNECTION_NAME") |
| 30 | +connection_project = os.getenv("CONNECTION_PROJECT") |
| 31 | +connection_location = os.getenv("CONNECTION_LOCATION") |
| 32 | +client_secret = os.getenv("CLIENT_SECRET") |
| 33 | +client_id = os.getenv("CLIENT_ID") |
| 34 | + |
| 35 | + |
| 36 | +oauth2_data_google_cloud = { |
| 37 | + "type": "oauth2", |
| 38 | + "flows": { |
| 39 | + "authorizationCode": { |
| 40 | + "authorizationUrl": "https://accounts.google.com/o/oauth2/auth", |
| 41 | + "tokenUrl": "https://oauth2.googleapis.com/token", |
| 42 | + "scopes": { |
| 43 | + "https://www.googleapis.com/auth/cloud-platform": ( |
| 44 | + "View and manage your data across Google Cloud Platform" |
| 45 | + " services" |
| 46 | + ), |
| 47 | + "https://www.googleapis.com/auth/calendar.readonly": ( |
| 48 | + "View your calendars" |
| 49 | + ), |
| 50 | + }, |
| 51 | + } |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +oauth2_scheme = dict_to_auth_scheme(oauth2_data_google_cloud) |
| 56 | + |
| 57 | +auth_credential = AuthCredential( |
| 58 | + auth_type=AuthCredentialTypes.OAUTH2, |
| 59 | + oauth2=OAuth2Auth( |
| 60 | + client_id=client_id, |
| 61 | + client_secret=client_secret, |
| 62 | + ), |
| 63 | +) |
| 64 | + |
| 65 | +calendar_tool = ApplicationIntegrationToolset( |
| 66 | + project=connection_project, |
| 67 | + location=connection_location, |
| 68 | + tool_name_prefix="calendar_tool", |
| 69 | + connection=connection_name, |
| 70 | + actions=["GET_calendars/%7BcalendarId%7D/events"], |
| 71 | + tool_instructions=""" |
| 72 | + Use this tool to list events in a calendar. Get calendarId from the user and use it in tool as following example: |
| 73 | + connectorInputPayload: { "Path parameters": { "calendarId": "primary" } }. Follow the schema correctly. Note its "Path parameters" and not "Path_parameters". |
| 74 | + """, |
| 75 | + auth_scheme=oauth2_scheme, |
| 76 | + auth_credential=auth_credential, |
| 77 | +) |
| 78 | + |
| 79 | +root_agent = Agent( |
| 80 | + model="gemini-2.0-flash", |
| 81 | + name="data_processing_agent", |
| 82 | + description="Agent that can list events in a calendar.", |
| 83 | + instruction=""" |
| 84 | + Helps you with calendar related tasks. |
| 85 | + """, |
| 86 | + tools=calendar_tool.get_tools(), |
| 87 | + generate_content_config=types.GenerateContentConfig( |
| 88 | + safety_settings=[ |
| 89 | + types.SafetySetting( |
| 90 | + category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 91 | + threshold=types.HarmBlockThreshold.OFF, |
| 92 | + ), |
| 93 | + ] |
| 94 | + ), |
| 95 | +) |
0 commit comments