用于 ADK 的代理运行时代码执行工具(Agent Runtime Code Execution Tool for ADK)¶
“Agent Runtime Code Execution ADK Tool”提供了一种低延迟、高效的方法,用于运行由人工智能(AI)生成的代码。 Google Cloud Agent Runtime 该工具专为快速执行而设计,适用于需要代理(agent)参与的工作流程,并通过使用沙箱环境来提升安全性。代码执行工具(Code Execution Tool)允许代码和数据在多次请求之间保持持久性,从而支持复杂的、多步骤的编码任务。
- 代码开发与调试: 创建代理任务,这些任务会在多次请求中测试代码的不同版本,并对这些版本进行迭代(即不断修改代码并重新测试)。
- 结合数据分析进行编程: 您可以上传最大容量为 100MB 的数据文件,并运行多个基于代码的分析任务;无需在每次运行代码时都重新加载数据。
这个代码执行工具是 Agent Runtime 套件的一部分,但您无需将您的代理程序部署到 Agent Runtime 中才能使用该工具。您可以在本地运行您的代理程序,或者将其与其他服务结合使用,同时仍然可以使用这个工具。有关 Agent Runtime 中的代码执行功能的更多信息,请参阅相关文档。 代理运行时代码执行(Agent Runtime Code Execution) documentation.
使用该工具。¶
在使用“Agent Runtime Code Execution”工具之前,您需要先使用 Google Cloud Agent Runtime 创建一个沙箱环境。只有在该沙箱环境中,才能将该工具与 ADK(Android Development Kit)代理结合使用。
要将“代码执行”工具与您的 ADK 代理一起使用,请按照以下步骤操作:
- 请按照 “Agent Runtime” 中的说明进行操作。 代码执行快速入门指南 用于创建一个代码执行沙箱环境。
- 创建一个 ADK(Android Development Kit)代理,并为其配置相应的设置,以便能够访问您创建沙箱环境的 Google Cloud 项目。
- 以下代码示例展示了一个被配置为使用 Code Executor 工具的代理(agent)。请替换相关内容。
SANDBOX_RESOURCE_NAME使用您创建的资源名称,在沙箱环境中进行操作。
from google.adk.agents.llm_agent import Agent
from google.adk.code_executors.agent_engine_sandbox_code_executor import AgentEngineSandboxCodeExecutor
root_agent = Agent(
model="gemini-flash-latest",
name="agent_engine_code_execution_agent",
instruction="You are a helpful agent that can write and execute code to answer questions and solve problems.",
code_executor=AgentEngineSandboxCodeExecutor(
sandbox_resource_name="SANDBOX_RESOURCE_NAME",
),
)
有关该文件预期格式的详细信息,请参阅……(请提供相关文档或链接)。 sandbox_resource_name “value”以及“alternative”(即另一种选择或替代方案)。 agent_engine_resource_name 参数,请参见…… 配置参数对于一个更高级的示例(其中包含了该工具的推荐系统使用说明),请参见…… 高级功能
示例 或者全部内容
代理代码示例.
它是如何工作的?¶
The AgentEngineSandboxCodeExecutor 该工具在整个代理任务执行过程中维护一个统一的沙箱环境;也就是说,沙箱的状态会持续存在于 ADK 工作流会话中的所有操作过程中。
- 沙箱创建: 对于需要执行代码的多步骤任务, Agent Runtime 会创建一个具有指定语言和机器配置的沙箱环境,从而将代码执行环境与其他系统部分隔开。如果事先没有创建沙箱环境,代码执行工具会自动使用默认设置来创建一个沙箱环境。
- 具有持久化功能的代码执行: 由 AI 生成的代码(用于调用某个工具)会被传输到沙箱环境中,然后在隔离的环境中执行。执行完成后,沙箱环境会被关闭。 保持活跃状态 在同一个会话中,后续的工具调用会保留变量、导入的模块以及文件的状态,以便下次由同一代理执行的工具调用能够继续使用这些数据。
- 结果检索: 标准输出结果以及所有捕获到的错误信息都会被收集起来,并传回调用这些功能的代理程序(即调用这些函数的程序)。
- 沙箱环境清理: 一旦代理任务或对话结束,代理可以手动删除沙箱,或者依赖在创建沙箱时所设置的沙箱的“TTL”(Time-To-Live)机制来自动删除沙箱。
主要优势¶
- 持久状态(Persistent state): 解决复杂的任务,其中数据操作或变量状态需要在多次工具调用之间保持一致(即这些操作/状态需要在不同的工具之间连续进行)。
- **针对性隔离(Targeted Isolation):** 提供了强大的进程级隔离机制,确保工具代码在执行过程中的安全性,同时保持了轻量级的特性(即不会占用过多的系统资源)。
- 代理运行时(Agent Runtime)集成: 该功能与 Agent Runtime 的工具使用及协调层(tool-use and orchestration layer)实现了紧密集成。
- 低延迟性能: 该系统专为提高效率而设计,使客服人员能够高效地执行复杂的工具使用流程,而不会产生过多的额外开销(即不会显著降低工作效率)。
- 灵活的计算配置: 创建具有特定编程语言、处理能力和内存配置的沙箱环境。
系统要求¶¶
要成功地将 “Agent Runtime Code Execution” 工具与您的 ADK(Application Development Kit)代理程序一起使用,必须满足以下要求:
- 已启用 Agent Platform API 的 Google Cloud 项目
- 代理的服务账户需要(某些特定的配置或权限)。 roles/aiplatform.user 该角色(role)具备以下功能(allow it to):
- 创建、获取、列出和删除代码执行沙箱(code execution sandboxes)。
- 执行代码:使用沙箱环境(sandbox)来运行代码。
配置参数¶
“Agent Runtime Code Execution”工具具有以下参数。您必须设置以下资源参数中的一个:
sandbox_resource_name这是一个用于指向现有沙箱环境的资源路径,该沙箱环境会在每次工具调用时被使用。预期的字符串格式如下:agent_engine_resource_name:该工具用于创建沙箱环境的代理运行时资源名称。预期的字符串格式如下:
您可以使用 Google Cloud Agent Runtime 的 API,通过 Google Cloud 客户端连接来分别配置 Agent Runtime 沙箱环境,并设置以下参数:
- 编程语言, 包括 Python 和 JavaScript
- 计算环境(包括 CPU 和内存的容量)
有关如何连接到 Google Cloud Agent Runtime 以及配置沙箱环境的更多信息,请参阅 Agent Runtime 文档。 代码执行快速入门指南.
高级示例¶
以下示例代码展示了如何在 ADK 代理中实现 Code Executor 工具的使用。该示例包含……(此处应继续列出示例代码的具体内容)。 base_system_instruction 该条款用于设置代码执行的操作指南。虽然这个指令性条款是可选的,但为了从这个工具中获得最佳效果,强烈建议使用它。
from google.adk.agents.llm_agent import Agent
from google.adk.code_executors.agent_engine_sandbox_code_executor import AgentEngineSandboxCodeExecutor
def base_system_instruction():
"""Returns: data science agent system instruction."""
return """
# Guidelines
**Objective:** Assist the user in achieving their data analysis goals, **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 sandbox 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.
**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_output
(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_output
x=999751168
```
- You **never** generate ```tool_output 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-flash-latest",
name="agent_engine_code_execution_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 relevant 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=AgentEngineSandboxCodeExecutor(
# Replace with your sandbox resource name if you already have one.
sandbox_resource_name="SANDBOX_RESOURCE_NAME",
# Replace with agent engine resource name used for creating sandbox if
# sandbox_resource_name is not set:
# agent_engine_resource_name="AGENT_ENGINE_RESOURCE_NAME",
),
)
有关使用此示例代码实现的 ADK 代理的完整版本,请参阅…… `agent_engine_code_execution_sample`.