E2BToolset
E2BToolset
Agent에게 실제 동작하는 E2B 클라우드 샌드박스에 접근할 수 있게 해 주는 Toolset이에요. bash 명령 실행과 파일 관리를 지원하죠.
출처: 문서
본문
- 필수 초기화 변수:
api_key(E2B API 키.E2B_API_KEY환경 변수로도 설정 가능)
개요 (Overview)
E2BToolset은 같은 E2B 클라우드 샌드박스 안에서 동작하는 네 가지 도구를 묶어서, Agent에게 코드 실행과 파일 조작을 위한 안전하고 격리된 Linux 환경을 제공해요.
run_bash_command(RunBashCommandTool): bash 명령을 실행하고 결합된exit_code,stdout,stderr를 반환해요. 셸 스크립트, 패키지 설치, 코드 컴파일, 또는 시스템 수준의 작업에 사용해요.read_file(ReadFileTool): 샌드박스 파일시스템에서 파일의 텍스트 내용을 읽어요.write_file(WriteFileTool): 샌드박스의 파일에 텍스트 내용을 써요. 상위 디렉터리는 자동으로 만들어지고, 기존 파일은 덮어써져요.list_directory(ListDirectoryTool): 주어진 경로의 파일과 하위 디렉터리를 나열해요.
네 도구 모두 단일 E2BSandbox 인스턴스를 공유하므로, write_file로 쓴 파일은 같은 Agent 실행에서 run_bash_command와 read_file에 바로 사용할 수 있어요. 이 toolset이 샌드박스 수명 주기를 관리해요. warm_up()은 샌드박스를 시작하고, close()는 종료하며, YAML 직렬화 왕복(round-trip)은 공유 샌드박스 관계를 유지해요.
파라미터 (Parameters)
api_key는 필수이며 E2B API 키여야 해요. 기본 설정은E2B_API_KEY환경 변수를 사용해요. 키는 e2b.dev에서 얻을 수 있어요.sandbox_template는 선택이며 기본값은"base"예요. 사용할 E2B 샌드박스 템플릿을 지정해요.timeout은 선택이며 기본값은120이에요. 샌드박스 비활성 타임아웃을 초 단위로 설정해요.environment_vars는 선택이며 샌드박스 프로세스에 환경 변수를 주입할 수 있게 해줘요.
사용법 (Usage)
E2BToolset을 사용하려면 E2B 통합을 설치하세요.
pip install e2b-haystack
E2B API 키를 설정하세요.
export E2B_API_KEY="your-e2b-api-key"
Agent와 함께 사용 (With an Agent)
E2BToolset을 Agent 컴포넌트와 함께 사용할 수 있어요. Agent가 자동으로 샌드박스를 시작하고, 도구를 호출해 코드를 쓰고 실행하고 검사하며, LLM이 같은 샌드박스 프로세스 안에서 호출을 연결 짓게 해요.
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack_integrations.tools.e2b import E2BToolset
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
tools=E2BToolset(),
system_prompt=(
"You are a helpful coding assistant with access to a live Linux sandbox. "
"Use the available tools freely to explore, write files, and run commands. "
"All tools operate inside the same sandbox environment, so files written "
"with write_file are immediately available to run_bash_command and read_file."
),
max_agent_steps=15,
)
response = agent.run(
messages=[
ChatMessage.from_user(
"Write a Python script to /tmp/primes.py that prints all prime numbers "
"up to 50, run it, and then read the file back so I can see both the "
"script and its output.",
),
],
)
print(response["last_message"].text)
개별 도구 사용 (Using individual tools)
도구의 일부만 필요하다면 직접 인스턴스화하고 공유 E2BSandbox를 전달할 수 있어요.
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack_integrations.tools.e2b import (
E2BSandbox,
ListDirectoryTool,
ReadFileTool,
RunBashCommandTool,
WriteFileTool,
)
sandbox = E2BSandbox(sandbox_template="base", timeout=300)
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
tools=[
RunBashCommandTool(sandbox=sandbox),
ReadFileTool(sandbox=sandbox),
WriteFileTool(sandbox=sandbox),
ListDirectoryTool(sandbox=sandbox),
],
)
도구를 단독으로(Agent나 Pipeline 밖에서) 사용할 때는 첫 호출 전에 sandbox.warm_up()을 호출하고, 작업이 끝나면 sandbox.close()를 호출해 클라우드 리소스를 해제하세요.
파이프라인에서 사용 (In a Pipeline)
E2BToolset은 완전히 직렬화 가능하므로, 이를 사용하는 Agent를 Pipeline으로 감싸고 YAML로 저장할 수 있어요.
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.core.pipeline import Pipeline
from haystack.dataclasses import ChatMessage
from haystack_integrations.tools.e2b import E2BToolset
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
tools=E2BToolset(sandbox_template="base", timeout=120),
system_prompt="You are a helpful coding assistant with access to a live Linux sandbox.",
max_agent_steps=10,
)
pipeline = Pipeline()
pipeline.add_component("agent", agent)
# Serialize and restore - all four tools still share the same E2BSandbox after the round-trip.
yaml_str = pipeline.dumps()
restored = Pipeline.loads(yaml_str)
result = restored.run(
data={
"agent": {
"messages": [
ChatMessage.from_user(
"Write a Python one-liner to /tmp/hello.py that prints "
"'Hello from E2B!', run it, then show me the output.",
),
],
},
},
)
print(result["agent"]["last_message"].text)