From edfe5539421d196ca4da14d3a37fac7b598f8c8d Mon Sep 17 00:00:00 2001 From: Artem Oroberts Date: Tue, 28 Oct 2025 15:51:35 -0700 Subject: [PATCH] feat: Add sample agent for VertexAiCodeExecutor This change introduces a new sample agent and documentation to demonstrate the usage of the `VertexAiCodeExecutor`. The new agent, located at `vertex_code_execution/agent.py`, is a direct counterpart to the existing sample at `code_execution/agent.py`. The key difference is that this new agent uses `VertexAiCodeExecutor` to execute code within the Vertex AI Code Interpreter Extension, whereas the original sample uses `BuiltInCodeExecutor` to run code in the model's built-in sandbox. A `README.md` file is also included to provide an overview, setup instructions, and sample usage for the new agent. Related: #2993 PiperOrigin-RevId: 825239758 --- .../samples/vertex_code_execution/README.md | 59 +++++++++++ .../samples/vertex_code_execution/__init__.py | 15 +++ .../samples/vertex_code_execution/agent.py | 100 ++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 contributing/samples/vertex_code_execution/README.md create mode 100644 contributing/samples/vertex_code_execution/__init__.py create mode 100644 contributing/samples/vertex_code_execution/agent.py diff --git a/contributing/samples/vertex_code_execution/README.md b/contributing/samples/vertex_code_execution/README.md new file mode 100644 index 00000000..121de737 --- /dev/null +++ b/contributing/samples/vertex_code_execution/README.md @@ -0,0 +1,59 @@ +# Vertex AI Code Execution Agent Sample + +This directory contains a sample agent that demonstrates how to use the +`VertexAiCodeExecutor` for data science tasks. + +## Overview + +The agent is designed to assist with data analysis in a Python environment. It +can execute Python code to perform tasks like data manipulation, analysis, and +visualization. This agent is particularly useful for tasks that require a secure +and sandboxed code execution environment with common data science libraries +pre-installed. + +This sample is a direct counterpart to the +[code execution sample](../code_execution/) which uses the +`BuiltInCodeExecutor`. The key difference in this sample is the use of +`VertexAiCodeExecutor`. + +## `VertexAiCodeExecutor` + +The `VertexAiCodeExecutor` leverages the +[Vertex AI Code Interpreter Extension](https://cloud.google.com/vertex-ai/generative-ai/docs/extensions/code-interpreter) +to run Python code. This provides several advantages: + +- **Security**: Code is executed in a sandboxed environment on Google Cloud, + isolating it from your local system. +- **Pre-installed Libraries**: The environment comes with many common Python + data science libraries pre-installed, such as `pandas`, `numpy`, and + `matplotlib`. +- **Stateful Execution**: The execution environment is stateful, meaning + variables and data from one code execution are available in subsequent + executions within the same session. + +## How to use + +### Prerequisites + +Ensure you have configured your environment for using +[Google Cloud Vertex AI](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-cloud-vertex-ai). +You will need to have a Google Cloud Project with the Vertex AI API enabled. + +### Running the agent + +You can run this agent using the ADK CLI from the root of the repository. + +To interact with the agent through the command line: + +```bash +adk run contributing/samples/vertex_code_execution "Plot a sine wave from 0 to 10" +``` + +To use the web interface: + +```bash +adk web contributing/samples/ +``` + +Then select `vertex_code_execution` from the list of agents and interact with +it. diff --git a/contributing/samples/vertex_code_execution/__init__.py b/contributing/samples/vertex_code_execution/__init__.py new file mode 100644 index 00000000..c48963cd --- /dev/null +++ b/contributing/samples/vertex_code_execution/__init__.py @@ -0,0 +1,15 @@ +# 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 . import agent diff --git a/contributing/samples/vertex_code_execution/agent.py b/contributing/samples/vertex_code_execution/agent.py new file mode 100644 index 00000000..39ad65d3 --- /dev/null +++ b/contributing/samples/vertex_code_execution/agent.py @@ -0,0 +1,100 @@ +# 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. + +"""Data science agent that uses Vertex AI code interpreter.""" + +from google.adk.agents.llm_agent import Agent +from google.adk.code_executors.vertex_ai_code_executor import VertexAiCodeExecutor + + +def base_system_instruction(): + """Returns: data science agent system instruction.""" + + return """ + # Guidelines + + **Objective:** Assist the user in achieving their data analysis goals within the context of a Python Colab notebook, **with emphasis on avoiding assumptions and ensuring accuracy.** Reaching that goal can involve multiple steps. When you need to generate code, you **don't** need to solve the goal in one go. Only generate the next step at a time. + + **Code Execution:** All code snippets provided will be executed within the Colab environment. + + **Statefulness:** All code snippets are executed and the variables stays in the environment. You NEVER need to re-initialize variables. You NEVER need to reload files. You NEVER need to re-import libraries. + + **Imported Libraries:** The following libraries are ALREADY imported and should NEVER be imported again: + + ```tool_code + import io + import math + import re + import matplotlib.pyplot as plt + import numpy as np + import pandas as pd + import scipy + ``` + + **Output Visibility:** Always print the output of code execution to visualize results, especially for data exploration and analysis. For example: + - To look a the shape of a pandas.DataFrame do: + ```tool_code + print(df.shape) + ``` + The output will be presented to you as: + ```tool_outputs + (49, 7) + + ``` + - To display the result of a numerical computation: + ```tool_code + x = 10 ** 9 - 12 ** 5 + print(f'{{x=}}') + ``` + The output will be presented to you as: + ```tool_outputs + x=999751168 + + ``` + - You **never** generate ```tool_outputs yourself. + - You can then use this output to decide on next steps. + - Print just variables (e.g., `print(f'{{variable=}}')`. + + **No Assumptions:** **Crucially, avoid making assumptions about the nature of the data or column names.** Base findings solely on the data itself. Always use the information obtained from `explore_df` to guide your analysis. + + **Available files:** Only use the files that are available as specified in the list of available files. + + **Data in prompt:** Some queries contain the input data directly in the prompt. You have to parse that data into a pandas DataFrame. ALWAYS parse all the data. NEVER edit the data that are given to you. + + **Answerability:** Some queries may not be answerable with the available data. In those cases, inform the user why you cannot process their query and suggest what type of data would be needed to fulfill their request. + + """ + + +root_agent = Agent( + model="gemini-2.5-flash", + name="data_science_agent", + instruction=base_system_instruction() + """ + + +You need to assist the user with their queries by looking at the data and the context in the conversation. +You final answer should summarize the code and code execution relavant to the user query. + +You should include all pieces of data to answer the user query, such as the table from code execution results. +If you cannot answer the question directly, you should follow the guidelines above to generate the next step. +If the question can be answered directly with writing any code, you should do that. +If you doesn't have enough data to answer the question, you should ask for clarification from the user. + +You should NEVER install any package on your own like `pip install ...`. +When plotting trends, you should make sure to sort and order the data by the x-axis. + + +""", + code_executor=VertexAiCodeExecutor(), +)