Skip to content

Commit db06416

Browse files
Genesis929GWeale
authored andcommitted
feat: Add chart generation and artifact loading to data agent
Introduces a generate_chart tool to the Data Agent sample, leveraging Altair and vl-convert to render Vega-Lite specifications into charts. Co-authored-by: Han Cao <huanc@google.com> Change-Id: I5765487406d511e650091f5dc884102c43568fd4
1 parent ecb759c commit db06416

1 file changed

Lines changed: 56 additions & 3 deletions

File tree

  • contributing/samples/integrations/data_agent

contributing/samples/integrations/data_agent/agent.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
17+
import asyncio
1518
import os
19+
from typing import Any
1620

1721
from google.adk.agents import Agent
1822
from google.adk.auth.auth_credential import AuthCredentialTypes
23+
from google.adk.tools import load_artifacts
1924
from google.adk.tools.data_agent.config import DataAgentToolConfig
2025
from google.adk.tools.data_agent.credentials import DataAgentCredentialsConfig
2126
from google.adk.tools.data_agent.data_agent_toolset import DataAgentToolset
27+
from google.adk.tools.tool_context import ToolContext
2228
import google.auth
2329
import google.auth.transport.requests
30+
from google.genai import types
2431

2532
# Define the desired credential type.
2633
# By default use Application Default Credentials (ADC) from the local
@@ -72,16 +79,62 @@
7279
],
7380
)
7481

82+
# NOTE: The generate_chart tool requires 'altair' and 'vl-convert-python' to be
83+
# installed in your environment. You can install them using:
84+
# pip install altair vl-convert-python
85+
async def generate_chart(
86+
chart_spec: dict[str, Any], tool_context: ToolContext
87+
) -> dict[str, str]:
88+
"""Generates a professional chart using Altair based on a Vega-Lite spec.
89+
90+
Args:
91+
chart_spec: A dictionary defining a Vega-Lite chart.
92+
tool_context: The tool context.
93+
94+
Returns:
95+
A dictionary containing the status of the chart generation ("success" or
96+
"error"), a detail message, and the filename if successful.
97+
"""
98+
import altair as alt
99+
import vl_convert as vlc
100+
101+
try:
102+
# Altair can take a Vega-Lite dict directly and render it.
103+
# We use vl-convert to transform the spec into a high-quality PNG.
104+
png_data = await asyncio.to_thread(vlc.vegalite_to_png, chart_spec, scale=2)
105+
106+
# Save as artifact
107+
await tool_context.save_artifact(
108+
"chart.png",
109+
types.Part.from_bytes(data=png_data, mime_type="image/png"),
110+
)
111+
title = chart_spec.get("title", "Chart")
112+
return {
113+
"status": "success",
114+
"detail": (
115+
f"Professional chart '{title}' rendered using Altair/Vega-Lite."
116+
),
117+
"filename": "chart.png",
118+
}
119+
except Exception as e: # pylint: disable=broad-exception-caught
120+
return {"status": "error", "detail": f"Failed to render chart: {str(e)}"}
121+
122+
75123
root_agent = Agent(
76124
name="data_agent",
77-
description="Agent to answer user questions using Data Agents.",
125+
description=(
126+
"Agent to answer user questions using Data Agents and generate charts."
127+
),
78128
instruction=(
79129
"## Persona\nYou are a helpful assistant that uses Data Agents"
80130
" to answer user questions about their data.\n\n## Tools\n- You can"
81131
" list available data agents using `list_accessible_data_agents`.\n-"
82132
" You can get information about a specific data agent using"
83133
" `get_data_agent_info`.\n- You can chat with a specific data"
84-
" agent using `ask_data_agent`.\n"
134+
" agent using `ask_data_agent`.\n- `generate_chart` renders"
135+
" professional charts from a `chart_spec` (Vega-Lite JSON). Use this"
136+
" whenever you need to visualize data; do not show raw JSON to the"
137+
" user.\n- You can load artifacts using `load_artifacts`.\n"
85138
),
86-
tools=[da_toolset],
139+
tools=[da_toolset, generate_chart, load_artifacts],
87140
)

0 commit comments

Comments
 (0)