-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Add Google API toolset for agents to access 1000+ APIs #70144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gopidesupavan
wants to merge
8
commits into
apache:main
Choose a base branch
from
gopidesupavan:gcp-toolset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ec433a
Add Google API toolset for agents to access 1000+ APIs
gopidesupavan c3b43fc
Update method args param conversion
gopidesupavan 654067d
Clarify when to use provider API toolsets
gopidesupavan ce00730
Fixup docs
gopidesupavan 4501241
Add troubleshoot example
gopidesupavan 7b7056e
fixup tests
gopidesupavan 50f8411
resolve comments
gopidesupavan 119b9a2
resolve comments
gopidesupavan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
providers/common/ai/src/airflow/providers/common/ai/example_dags/example_gcp_toolset.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Example Dag: agent with allow-listed Google API access via GoogleCloudToolset.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from airflow.providers.common.ai.operators.agent import AgentOperator | ||
| from airflow.providers.common.ai.toolsets.google import GoogleCloudToolset | ||
| from airflow.providers.common.compat.sdk import dag | ||
|
|
||
|
|
||
| # [START howto_operator_agent_gcp] | ||
| @dag(tags=["example"]) | ||
| def example_agent_gcp_toolset(): | ||
| AgentOperator( | ||
| task_id="gcs_auditor", | ||
| prompt="Which buckets exist, and roughly how many objects are in 'data-lake-raw'?", | ||
| llm_conn_id="pydanticai_default", | ||
| system_prompt=( | ||
| "You are a Google Cloud operations assistant. Discover what you " | ||
| "are allowed to call, check parameter shapes before calling, and " | ||
| "answer with concrete numbers." | ||
| ), | ||
| toolsets=[ | ||
| GoogleCloudToolset( | ||
| gcp_conn_id="google_cloud_default", | ||
| allowed_methods=[ | ||
| "storage/v1:buckets.list", | ||
| "storage/v1:buckets.get", | ||
| "storage/v1:objects.list", | ||
| ], | ||
| ) | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| # [END howto_operator_agent_gcp] | ||
|
|
||
| example_agent_gcp_toolset() |
87 changes: 87 additions & 0 deletions
87
...mmon/ai/src/airflow/providers/common/ai/example_dags/example_gcp_troubleshooting_agent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ | ||
| Example Dag: troubleshoot a Google Cloud data pipeline with GoogleCloudToolset. | ||
|
|
||
| Required Airflow Variables: | ||
| - ``common_ai_gcp_project_id`` | ||
| - ``common_ai_gcp_bucket`` | ||
|
|
||
| Optional Airflow Variables: | ||
| - ``common_ai_gcp_dataset``; default: ``pipeline_observability`` | ||
| - ``common_ai_gcp_pipeline_table``; default: ``pipeline_runs`` | ||
| - ``common_ai_gcp_raw_prefix``; default: ``raw/`` | ||
|
|
||
| For a useful demo, point the variables at a project with: | ||
| - GCS objects under ``gs://<bucket>/<raw_prefix>`` | ||
| - a BigQuery table with recent pipeline status rows | ||
| - Cloud Monitoring metrics from recent BigQuery and GCS activity | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from airflow.providers.common.ai.operators.agent import AgentOperator | ||
| from airflow.providers.common.ai.toolsets.google import GoogleCloudToolset | ||
| from airflow.providers.common.compat.sdk import Variable, dag, task | ||
|
|
||
|
|
||
| # [START howto_operator_agent_gcp_troubleshooting] | ||
| @dag(tags=["example"]) | ||
| def example_gcp_troubleshooting_agent(): | ||
| @task | ||
| def build_triage_prompt() -> str: | ||
| project_id = Variable.get("common_ai_gcp_project_id") | ||
| bucket_name = Variable.get("common_ai_gcp_bucket") | ||
| dataset_id = Variable.get("common_ai_gcp_dataset", default="pipeline_observability") | ||
| pipeline_table = Variable.get("common_ai_gcp_pipeline_table", default="pipeline_runs") | ||
| raw_prefix = Variable.get("common_ai_gcp_raw_prefix", default="raw/") | ||
|
|
||
| return ( | ||
| f"Pipeline is slow today in project {project_id}. " | ||
| f"Check whether raw files arrived in gs://{bucket_name}/{raw_prefix}, " | ||
| "inspect recent BigQuery jobs and rows from " | ||
| f"{dataset_id}.{pipeline_table}, check active Cloud Monitoring alert policies, " | ||
| "and compare BigQuery query count and GCS request count metrics for today versus yesterday. " | ||
| "Summarize the likely cause and the next action." | ||
| ) | ||
|
|
||
| AgentOperator( | ||
| task_id="triage_pipeline", | ||
| prompt=build_triage_prompt(), | ||
| llm_conn_id="pydanticai_default", | ||
| system_prompt="Use the available Google tools to investigate the pipeline. Do not invent missing values.", | ||
| toolsets=[ | ||
| GoogleCloudToolset( | ||
| gcp_conn_id="google_cloud_default", | ||
| allowed_methods=[ | ||
| "storage/v1:objects.list", | ||
| "bigquery/v2:jobs.list", | ||
| "bigquery/v2:jobs.get", | ||
| "bigquery/v2:jobs.query", | ||
| "monitoring/v3:projects.alertPolicies.list", | ||
| "monitoring/v3:projects.timeSeries.list", | ||
| ], | ||
| max_pages=2, | ||
| max_output_bytes=20000, | ||
| ) | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| # [END howto_operator_agent_gcp_troubleshooting] | ||
|
|
||
| example_gcp_troubleshooting_agent() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just have it use
HookToolsetwith GCPHook?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HookToolsetis still useful when the workflow maps to existing hook methods. The gap here is that it only exposes Python methods that a hook already wraps, while this toolset exposes allowed Google API client methods directly.For example, an incident triage agent might need to check recent BigQuery jobs, Dataflow jobs, and GCS object arrival. With
GoogleCloudToolset, the Dag author can allow-list the exact API methods, such asbigquery/v2:jobs.list,bigquery/v2:jobs.get,dataflow/v1b3:projects.locations.jobs.list, andstorage/v1:objects.list.Doing the same with
HookToolsetdepends on each service hook having the right method and returning data in a useful shape for the agent. Some APIs also have no dedicated hook coverage. For example, Cloud Monitoring time series reads can be exposed asmonitoring/v3:projects.timeSeries.list, while the existing Stackdriver hook mainly wraps alert policy and notification channel operations.So this is not replacing
HookToolset; it is for cases where the agent should call selected Google REST API methods directly, instead of being limited to the Python methods currently wrapped by Airflow hooksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example I tested was this prompt:
With
HookToolset, the agent handled the parts backed by existing hook methods: alert policies, BigQuery rows, and GCS object listing. But it could not answer the Monitoring time-series part because that metric-read operation is not exposed by the hook methods available to it.With
GoogleCloudToolset, the agent completed the full request by calling the relevant Google REST API methods directly, includingmonitoring/v3:projects.timeSeries.list.This is just one example; in other multi-service troubleshooting questions the missing piece may be a different Google API method. ideally here it is not to replace
HookToolset, but to support cases where we want to expose selected Google API methods without needing a dedicated Airflow hook wrapper for each one.