mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
fb23822335
The BigQuery integration page still imports from google.adk.tools.bigquery, which now raises a DeprecationWarning pointing at google.adk.integrations.bigquery. Update all five auth snippets and the included sample to the new path. The sample also wrapped session_service.create_session() in asyncio.run(), which blows up in Colab and other notebooks where a loop is already running. Restructure it around an async main() that awaits create_session() and iterates runner.run_async(), matching the pattern the other built-in tool snippets already use. Bug: b/559149787 Co-authored-by: Joe Fernandez <931947+joefernandez@users.noreply.github.com>
111 lines
3.8 KiB
Python
111 lines
3.8 KiB
Python
# Copyright 2025 Google LLC
|
|
#
|
|
# Licensed 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.
|
|
|
|
import asyncio
|
|
|
|
from google.adk.agents import Agent
|
|
from google.adk.integrations.bigquery import BigQueryCredentialsConfig
|
|
from google.adk.integrations.bigquery import BigQueryToolset
|
|
from google.adk.integrations.bigquery.config import BigQueryToolConfig
|
|
from google.adk.integrations.bigquery.config import WriteMode
|
|
from google.adk.runners import Runner
|
|
from google.adk.sessions import InMemorySessionService
|
|
from google.genai import types
|
|
import google.auth
|
|
|
|
# Define constants for this example agent
|
|
AGENT_NAME = "bigquery_agent"
|
|
APP_NAME = "bigquery_app"
|
|
USER_ID = "user1234"
|
|
SESSION_ID = "1234"
|
|
GEMINI_MODEL = "gemini-2.0-flash"
|
|
|
|
# Define a tool configuration to block any write operations
|
|
tool_config = BigQueryToolConfig(write_mode=WriteMode.BLOCKED)
|
|
|
|
# Use Application Default Credentials (ADC) for BigQuery authentication
|
|
# https://cloud.google.com/docs/authentication/provide-credentials-adc
|
|
application_default_credentials, _ = google.auth.default()
|
|
credentials_config = BigQueryCredentialsConfig(
|
|
credentials=application_default_credentials
|
|
)
|
|
|
|
# Instantiate a BigQuery toolset
|
|
bigquery_toolset = BigQueryToolset(
|
|
credentials_config=credentials_config, bigquery_tool_config=tool_config
|
|
)
|
|
|
|
# Agent Definition
|
|
bigquery_agent = Agent(
|
|
model=GEMINI_MODEL,
|
|
name=AGENT_NAME,
|
|
description=(
|
|
"Agent to answer questions about BigQuery data and models and execute"
|
|
" SQL queries."
|
|
),
|
|
instruction="""\
|
|
You are a data science agent with access to several BigQuery tools.
|
|
Make use of those tools to answer the user's questions.
|
|
""",
|
|
tools=[bigquery_toolset],
|
|
)
|
|
|
|
# Session and Runner
|
|
async def setup_session_and_runner():
|
|
session_service = InMemorySessionService()
|
|
await session_service.create_session(
|
|
app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
|
|
)
|
|
return Runner(
|
|
agent=bigquery_agent, app_name=APP_NAME, session_service=session_service
|
|
)
|
|
|
|
|
|
# Agent Interaction
|
|
async def call_agent_async(runner, query):
|
|
"""
|
|
Helper function to call the agent with a query.
|
|
"""
|
|
content = types.Content(role="user", parts=[types.Part(text=query)])
|
|
events = runner.run_async(
|
|
user_id=USER_ID, session_id=SESSION_ID, new_message=content
|
|
)
|
|
|
|
print("USER:", query)
|
|
async for event in events:
|
|
if event.is_final_response():
|
|
final_response = event.content.parts[0].text
|
|
print("AGENT:", final_response)
|
|
|
|
|
|
async def main():
|
|
runner = await setup_session_and_runner()
|
|
await call_agent_async(
|
|
runner, "Are there any ml datasets in bigquery-public-data project?"
|
|
)
|
|
await call_agent_async(runner, "Tell me more about ml_datasets.")
|
|
await call_agent_async(runner, "Which all tables does it have?")
|
|
await call_agent_async(runner, "Tell me more about the census_adult_income table.")
|
|
await call_agent_async(runner, "How many rows are there per income bracket?")
|
|
await call_agent_async(
|
|
runner,
|
|
"What is the statistical correlation between education_num, age, and the"
|
|
" income_bracket?",
|
|
)
|
|
|
|
|
|
# Note: In Colab or another notebook, an event loop is already running, so call
|
|
# `await main()` directly instead of `asyncio.run(main())`.
|
|
asyncio.run(main())
|