|
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 asyncio |
16 | | - |
17 | | -from google.adk.agents import Agent |
18 | | -from google.adk.runners import Runner |
19 | | -from google.adk.sessions import InMemorySessionService |
20 | | -from google.adk.tools.bigquery import BigQueryCredentialsConfig |
21 | 1 | from google.adk.tools.bigquery import BigQueryToolset |
22 | | -from google.adk.tools.bigquery.config import BigQueryToolConfig |
23 | | -from google.adk.tools.bigquery.config import WriteMode |
24 | | -from google.genai import types |
25 | | -import google.auth |
26 | | - |
27 | | -# Define constants for this example agent |
28 | | -AGENT_NAME = "bigquery_agent" |
29 | | -APP_NAME = "bigquery_app" |
30 | | -USER_ID = "user1234" |
31 | | -SESSION_ID = "1234" |
32 | | -GEMINI_MODEL = "gemini-2.0-flash" |
33 | | - |
34 | | -# Define a tool configuration to block any write operations |
35 | | -tool_config = BigQueryToolConfig(write_mode=WriteMode.BLOCKED) |
36 | | - |
37 | | -# Define a credentials config - in this example we are using application default |
38 | | -# credentials |
39 | | -# https://cloud.google.com/docs/authentication/provide-credentials-adc |
40 | | -application_default_credentials, _ = google.auth.default() |
41 | | -credentials_config = BigQueryCredentialsConfig( |
42 | | - credentials=application_default_credentials |
43 | | -) |
44 | | - |
45 | | -# Instantiate a BigQuery toolset |
46 | | -bigquery_toolset = BigQueryToolset( |
47 | | - credentials_config=credentials_config, bigquery_tool_config=tool_config |
48 | | -) |
49 | | - |
50 | | -# Agent Definition |
51 | | -bigquery_agent = Agent( |
52 | | - model=GEMINI_MODEL, |
53 | | - name=AGENT_NAME, |
54 | | - description=( |
55 | | - "Agent to answer questions about BigQuery data and models and execute" |
56 | | - " SQL queries." |
57 | | - ), |
58 | | - instruction="""\ |
59 | | - You are a data science agent with access to several BigQuery tools. |
60 | | - Make use of those tools to answer the user's questions. |
61 | | - """, |
62 | | - tools=[bigquery_toolset], |
63 | | -) |
64 | | - |
65 | | -# Session and Runner |
66 | | -session_service = InMemorySessionService() |
67 | | -session = asyncio.run( |
68 | | - session_service.create_session( |
69 | | - app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID |
70 | | - ) |
71 | | -) |
72 | | -runner = Runner( |
73 | | - agent=bigquery_agent, app_name=APP_NAME, session_service=session_service |
74 | | -) |
75 | | - |
76 | | - |
77 | | -# Agent Interaction |
78 | | -def call_agent(query): |
79 | | - """ |
80 | | - Helper function to call the agent with a query. |
81 | | - """ |
82 | | - content = types.Content(role="user", parts=[types.Part(text=query)]) |
83 | | - events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content) |
84 | | - |
85 | | - print("USER:", query) |
86 | | - for event in events: |
87 | | - if event.is_final_response(): |
88 | | - final_response = event.content.parts[0].text |
89 | | - print("AGENT:", final_response) |
90 | 2 |
|
| 3 | +# Initialize the BigQuery toolset with a specific location |
| 4 | +bigquery_toolset = BigQueryToolset(project_id="my-project", location="US") |
91 | 5 |
|
92 | | -call_agent("Are there any ml datasets in bigquery-public-data project?") |
93 | | -call_agent("Tell me more about ml_datasets.") |
94 | | -call_agent("Which all tables does it have?") |
95 | | -call_agent("Tell me more about the census_adult_income table.") |
96 | | -call_agent("How many rows are there per income bracket?") |
97 | | -call_agent( |
98 | | - "What is the statistical correlation between education_num, age, and the income_bracket?" |
99 | | -) |
| 6 | +# The agent can now use the BigQuery tools to query datasets in the specified location. |
0 commit comments