-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: Add BigQuery Agent Analytics Dashboard #4281
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
caohy1988
wants to merge
1
commit into
google:main
Choose a base branch
from
caohy1988:feature/bigquery-agent-analytics-dashboard
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.
+3,175
−0
Draft
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions
17
contributing/samples/bigquery_agent_analytics_demo/__init__.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,17 @@ | ||
| # 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. | ||
|
|
||
| """BigQuery agent analytics demo agent package.""" | ||
|
|
||
| from . import agent |
131 changes: 131 additions & 0 deletions
131
contributing/samples/bigquery_agent_analytics_demo/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,131 @@ | ||
| # 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| """Minimal agent wired to the BigQuery Agent Analytics plugin.""" | ||
|
|
||
| import os | ||
| from typing import Dict | ||
| from typing import List | ||
|
|
||
| from google.adk import Agent | ||
| from google.adk.apps import App | ||
| from google.adk.models.google_llm import Gemini | ||
| from google.adk.plugins.bigquery_agent_analytics_plugin import ( | ||
| BigQueryAgentAnalyticsPlugin, | ||
| BigQueryLoggerConfig, | ||
| ) | ||
| from google.adk.tools.function_tool import FunctionTool | ||
| from google.adk.tools.google_search_agent_tool import ( | ||
| GoogleSearchAgentTool, | ||
| create_google_search_agent, | ||
| ) | ||
|
|
||
| PROJECT_ID = os.getenv("BQ_AGENT_ANALYTICS_PROJECT") | ||
| DATASET_ID = os.getenv("BQ_AGENT_ANALYTICS_DATASET") | ||
| TABLE_ID = os.getenv("BQ_AGENT_ANALYTICS_TABLE", "agent_events") | ||
|
|
||
| # Default Vertex AI settings if env vars are not provided. | ||
| os.environ.setdefault("VERTEXAI_PROJECT", "test-project-0728-467323") | ||
| os.environ.setdefault("VERTEXAI_LOCATION", "us-central1") | ||
| # google.genai expects these env vars to auto-switch to Vertex AI with ADC. | ||
| os.environ.setdefault("GOOGLE_GENAI_USE_VERTEXAI", "true") | ||
| os.environ.setdefault("GOOGLE_CLOUD_PROJECT", "test-project-0728-467323") | ||
| os.environ.setdefault("GOOGLE_CLOUD_LOCATION", "us-central1") | ||
|
|
||
| if not PROJECT_ID or not DATASET_ID: | ||
| raise ValueError( | ||
| "Set BQ_AGENT_ANALYTICS_PROJECT and BQ_AGENT_ANALYTICS_DATASET before " | ||
| "running this agent to enable BigQuery analytics logging." | ||
| ) | ||
|
|
||
| analytics_plugin = BigQueryAgentAnalyticsPlugin( | ||
| project_id=PROJECT_ID, | ||
| dataset_id=DATASET_ID, | ||
| table_id=TABLE_ID, | ||
| config=BigQueryLoggerConfig( | ||
| event_allowlist=[ | ||
| "USER_MESSAGE_RECEIVED", | ||
| "LLM_REQUEST", | ||
| "LLM_RESPONSE", | ||
| "TOOL_STARTING", | ||
| "TOOL_COMPLETED", | ||
| "MODEL_RESPONSE", | ||
| "TOOL_CALL", | ||
| "TOOL_RESULT", | ||
| "ERROR", | ||
| ], | ||
| max_content_length=400, | ||
| ), | ||
| ) | ||
|
|
||
| def pick_city(top_n: int = 3) -> List[str]: | ||
| """Return a short list of recommended cities.""" | ||
| cities = ["Tokyo", "Paris", "New York", "Sydney", "Singapore", "Toronto"] | ||
| return cities[:top_n] | ||
|
|
||
| def city_highlights(city: str) -> Dict[str, str]: | ||
| """Return quick highlights for a city.""" | ||
| highlights = { | ||
| "Tokyo": "Sushi, Akihabara tech, efficient transit.", | ||
| "Paris": "Museums, pastries, walkable boulevards.", | ||
| "New York": "Broadway, diverse food, skyline views.", | ||
| "Sydney": "Harbour, beaches, outdoor cafes.", | ||
| "Singapore": "Hawker food, gardens, clean and safe.", | ||
| "Toronto": "CN Tower, neighbourhood food, waterfront.", | ||
| } | ||
| return {"city": city, "highlights": highlights.get(city, "Explore freely.")} | ||
|
|
||
| def estimate_trip_budget(city: str, days: int, budget_per_day: float) -> Dict[str, str]: | ||
| """Rough budget calculator.""" | ||
| total = days * budget_per_day | ||
| return { | ||
| "city": city, | ||
| "days": str(days), | ||
| "budget_per_day": f"${budget_per_day:,.0f}", | ||
| "estimated_total": f"${total:,.0f}", | ||
| "note": "Assumes lodging+food+local transit; adjust for flights.", | ||
| } | ||
|
|
||
| city_tool = FunctionTool(pick_city) | ||
| city_highlights_tool = FunctionTool(city_highlights) | ||
| budget_tool = FunctionTool(estimate_trip_budget) | ||
|
|
||
| gemini = Gemini(model="gemini-2.5-flash") | ||
| search_agent = create_google_search_agent(model=gemini) | ||
| google_search_tool = GoogleSearchAgentTool(agent=search_agent) | ||
|
|
||
| root_agent = Agent( | ||
| name="bq_agent_analytics_demo", | ||
| model=gemini, | ||
| instruction=( | ||
| "You are a concise assistant. Prefer to use tools when asked for trip " | ||
| "ideas, highlights, or cost estimates. Keep answers short and " | ||
| "actionable." | ||
| ), | ||
| description="A minimal agent that logs events to BigQuery.", | ||
| tools=[ | ||
| city_tool, | ||
| city_highlights_tool, | ||
| budget_tool, | ||
| google_search_tool, | ||
| ], | ||
| ) | ||
|
|
||
| app = App( | ||
| name="bq_agent_analytics_demo", | ||
| root_agent=root_agent, | ||
| plugins=[analytics_plugin], | ||
| ) | ||
45 changes: 45 additions & 0 deletions
45
contributing/samples/bigquery_agent_analytics_demo/dashboard/Dockerfile
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,45 @@ | ||
| # Agent Analytics Dashboard Docker Image | ||
| # | ||
| # Build: | ||
| # docker build -t agent-analytics-dashboard . | ||
| # | ||
| # Run: | ||
| # docker run -p 8080:8080 \ | ||
| # -e BQ_AGENT_ANALYTICS_PROJECT=your-project \ | ||
| # -e BQ_AGENT_ANALYTICS_DATASET=your-dataset \ | ||
| # -v ~/.config/gcloud:/root/.config/gcloud \ | ||
| # agent-analytics-dashboard | ||
|
|
||
| FROM python:3.11-slim | ||
|
|
||
| # Set working directory | ||
| WORKDIR /app | ||
|
|
||
| # Install system dependencies | ||
| RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
| curl \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| # Copy requirements first for better caching | ||
| COPY requirements.txt . | ||
|
|
||
| # Install Python dependencies | ||
| RUN pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| # Copy application code | ||
| COPY app.py . | ||
| COPY __init__.py . | ||
|
|
||
| # Create non-root user for security | ||
| RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app | ||
| USER appuser | ||
|
|
||
| # Expose port | ||
| EXPOSE 8080 | ||
|
|
||
| # Health check | ||
| HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ | ||
| CMD curl -f http://localhost:8080/api/health || exit 1 | ||
|
|
||
| # Run the application | ||
| CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"] |
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.
The default table ID is set to "agent_events", which is inconsistent with the dashboard and simulation code where the default is "agent_events_v2". To ensure data is logged to the correct table by default when the
BQ_AGENT_ANALYTICS_TABLEenvironment variable is not set, this should be updated.