Skip to main content
Daytona 提供安全且弹性的基础设施来运行您 AI 生成的代码。在 Agno,我们与它集成,使您的 Agents 和 Teams 能够在您的 Daytona 沙箱中运行代码。

先决条件

Daytona 工具需要 daytona_sdk Python 包:
pip install daytona_sdk
您还需要一个 Daytona API 密钥。您可以从您的 Daytona 账户 获取:
export DAYTONA_API_KEY=your_api_key

示例

以下示例展示了如何创建一个能够运行 Python 代码的 Daytona 沙箱代理:
cookbook/tools/daytona_tools.py
from agno.agent import Agent
from agno.models.anthropic import Claude
from agno.tools.daytona import DaytonaTools

daytona_tools = DaytonaTools()

# 设置一个专注于编码任务的 Agent,并允许其访问 Daytona 工具
agent = Agent(
    name="Coding Agent with Daytona tools",
    agent_id="coding-agent",
    model=Claude(id="claude-sonnet-4-20250514"),
    tools=[daytona_tools],
    markdown=True,
    show_tool_calls=True,
    instructions=[
        "You are an expert at writing and validating Python code. You have access to a remote, secure Daytona sandbox.",
        "Your primary purpose is to:",
        "1. Write clear, efficient Python code based on user requests",
        "2. Execute and verify the code in the Daytona sandbox",
        "3. Share the complete code with the user, as this is the main use case",
        "4. Provide thorough explanations of how the code works",
        "You can use the run_python_code tool to run Python code in the Daytona sandbox.",
        "Guidelines:",
        "- ALWAYS share the complete code with the user, properly formatted in code blocks",
        "- Verify code functionality by executing it in the sandbox before sharing",
        "- Iterate and debug code as needed to ensure it works correctly",
        "- Use pandas, matplotlib, and other Python libraries for data analysis when appropriate",
        "- Create proper visualizations when requested and add them as image artifacts to show inline",
        "- Handle file uploads and downloads properly",
        "- Explain your approach and the code's functionality in detail",
        "- Format responses with both code and explanations for maximum clarity",
        "- Handle errors gracefully and explain any issues encountered",
    ],
)

# 示例:生成斐波那契数列
agent.print_response(
    "Write Python code to generate the first 10 Fibonacci numbers and calculate their sum and average"
)

Toolkit 参数

参数类型默认值描述
api_keystrNoneDaytona API 密钥。如果未提供,则使用 DAYTONA_API_KEY 环境变量。
api_urlstrNoneDaytona API URL。如果未提供,则使用 DAYTONA_API_URL 环境变量或 Daytona 默认值。
sandbox_languageCodeLanguagePython在沙箱中运行的编程语言。
sandbox_target_regionstrNone将创建沙箱的区域。
sandbox_osstrNone在沙箱中运行的操作系统(默认:ubuntu)。
sandbox_os_userstrNone作为哪个用户运行沙箱(默认:root)。
sandbox_env_varsdictNone将在沙箱中设置的环境变量。
sandbox_labelsdictNone将在沙箱中设置的标签。
sandbox_publicboolNone沙箱是否应公开。
sandbox_auto_stop_intervalintNone沙箱将自动停止的间隔(秒)。
organization_idstrNone将用于沙箱的组织 ID。
timeoutint300与沙箱通信的超时(秒)(默认:5 分钟)。

代码执行工具

函数描述
run_python_code在上下文 Daytona 沙箱中运行 Python 代码。
run_code在上下文 Daytona 沙箱中运行非 Python 代码。

开发者资源