更新时间:2026-06-10 gmt 08:00
在智能体中集成沙箱工具-j9九游会登录
创建沙箱工具后,您需要将沙箱工具集成到智能体代码中,以便智能体可以调用隔离环境来完成代码运行等复杂任务。
前提条件
在智能体中集成沙箱工具
- 登录。
- 在左侧导航栏选择“开发中心 > 组件库 ”,单击“沙箱工具”页签,进入沙箱工具界面。
- 在工具列表单击操作列的“调用代码”,通过sdk调用工具实现执行代码的能力,并通过tools装饰器封装成工具,sdk提供code_session函数,可快速启动代码解释器的会话,并支持自动停止会话。
“调用代码”仅为代码示例,请根据实际情况替换代码示例中的“arguments”、“operate_type”。
api_key认证代码示例如下,获取api_key请参考如何获取沙箱工具的api_key。from agentarts.sdk.tools import code_session api_key = "your_actual_api_key" # your api key with code_session("cn-southwest-2", "your_code_interpreter_name", api_key=api_key) as code_client: response = code_client.invoke( operate_type="execute_code", api_key=api_key, arguments={ "code": code, # your code "language": "python", "clear_context": false } )
iam认证代码示例如下,获取ak/sk请参考如何获取华为云ak/sk。
from agentarts.sdk.tools import code_session # please configure the huaweicloud_sdk_ak/huaweicloud_sdk_sk in your environment variables in advance. with code_session("cn-southwest-2", "your_code_interpreter_name", auth_type="iam") as code_client: response = code_client.invoke( operate_type="execute_code", arguments={ "code": code, # your code "language": "python", "clear_context": false } )沙箱工具支持的操作如下:
- execute_code:执行代码。 arguments:执行代码参数。
{ "code": 'print("hello world!")', "language": "python", "clear_context": false } - execute_command:执行命令。 arguments:执行代码参数。
{ "command": "ls -la" } - read_files:读取文件。 arguments:执行代码参数。
{ "paths": ["/home/user/file1.txt", "/home/user/file2.txt"] } - write_files:写入文件。 arguments:执行代码参数。
{ "write_contents": [{"path": "/home/user/file1.txt", "text": "content"}] }
参数说明:
- code:在代码解释器会话中执行的代码。这是指定编程语言的源代码,将由代码解释器执行。
长度限制:最小长度为 0,最大长度为 1048576。
- command: 要使用该工具执行的命令。长度限制:最小长度为 0,最大长度为 65536。
- language: 要执行的代码所使用的编程语言,告诉代码解释器使用哪种语言运行时来执行代码。支持语言:python。
- write_contents:当请求操作为write_files时,待写入的内容,包含待写入数据内容和文件路径。列表限制:最小元素数量为0,最大元素数量为1000。
- paths:当请求操作为read_files时, 所需读取的文件路径,需保证每个路径唯一。列表限制:最小元素数量为0,最大元素数量为1000。
- execute_code:执行代码。
- 将3中的执行代码能力封装成工具,此处使用langgraph的tool装饰器作为演示。
import json from agentarts.sdk.tools import code_session from langchain_core.tools import tool @tool def execute_python_tool(code: str, description: str) -> str | none: """execute python code in the sandbox""" api_key = "your_actual_api_key" # your api key with code_session("your_region", "your_code_interpreter_name", api_key=api_key) as code_client: response = code_client.invoke( operate_type="execute_code", api_key=api_key, arguments={ "code": 'print("hello world!")', "language": "python", "clear_context": false } ) return json.dumps(response["result"])
- 将4集成到智能体中,此处以langgraph框架开发智能体为例。
from langchain_openai import chatopenai llm = chatopenai( model="deepseek-v3", api_key=os.environ.get("model_api_key", ""), base_url=os.environ.get("base_url", ""), max_tokens=1000, temperature=0.7, ) # 创建工具列表 tools = [execute_python_tool] # 工具绑定agent llm = llm.bind_tools(tools)
后续操作
将开发好的agent打包成镜像部署到智能体运行时,详细操作请参见通过控制台部署智能体运行时。
相关文档
创建工具详情请参考创建沙箱工具。
父主题:
相关文档
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好! 您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨