코드 인터프리터
코드 인터프리터 (Code Interpreter)
OpenAI의 Code Interpreter 도구를 사용해서 Python 코드를 안전한 샌드박스 환경에서 실행하는 방법을 알려드릴게요.
출처: 문서
본문
| 기능 | 지원 |
|---|---|
| LiteLLM Python SDK | ✅ |
| LiteLLM AI Gateway | ✅ |
| 지원 프로바이더 | openai |
자신의 샌드박스로 코드 인터프리터 라우팅하기
프록시가 /v1/responses의 code_interpreter를 가로채서(intercept), OpenAI의 컨테이너 대신 설정해 둔 샌드박스(현재는 e2b)에서 코드를 실행하게 할 수 있어요. 클라이언트 요청은 그대로 두고요. 응답 모양도 동일하게 유지됩니다(message 옆에 code_interpreter_call이 붙어요). 자세한 내용은 Code Interpreter Sandbox Interception을 참고하세요.
LiteLLM AI Gateway
API (OpenAI SDK)
LiteLLM Gateway를 가리키도록 OpenAI SDK를 사용해요:
code_interpreter_gateway.py
from openai import OpenAI
client = OpenAI(
api_key="sk-<your-litellm-api-key>", # Your LiteLLM API key
base_url="http://localhost:4000"
)
response = client.responses.create(
model="openai/gpt-5.6-terra",
tools=[{"type": "code_interpreter"}],
input="Calculate the first 20 fibonacci numbers and plot them"
)
print(response)
스트리밍 (Streaming)
code_interpreter_streaming.py
from openai import OpenAI
client = OpenAI(
api_key="sk-<your-litellm-api-key>",
base_url="http://localhost:4000"
)
stream = client.responses.create(
model="openai/gpt-5.6-terra",
tools=[{"type": "code_interpreter"}],
input="Generate sample sales data CSV and create a visualization",
stream=True
)
for event in stream:
print(event)
생성된 파일 콘텐츠 가져오기
get_file_content_gateway.py
from openai import OpenAI
client = OpenAI(
api_key="sk-<your-litellm-api-key>",
base_url="http://localhost:4000"
)
# 1. Run code interpreter
response = client.responses.create(
model="openai/gpt-5.6-terra",
tools=[{"type": "code_interpreter"}],
input="Create a scatter plot and save as PNG"
)
# 2. Get container_id from response
container_id = response.output[0].container_id
# 3. List files
files = client.containers.files.list(container_id=container_id)
# 4. Download file content
for file in files.data:
content = client.containers.files.content(
container_id=container_id,
file_id=file.id
)
with open(file.filename, "wb") as f:
f.write(content.read())
print(f"Downloaded: {file.filename}")
AI Gateway UI
LiteLLM Admin UI에는 코드 인터프리터 지원이 내장되어 있어요.
- LiteLLM UI의 Playground로 이동
- OpenAI 모델 선택 (예:
openai/gpt-5.6-terra) - Endpoint Type 아래 Endpoint로
/v1/responses선택 - 왼쪽 패널에서 Code Interpreter 토글
- 코드 실행 또는 파일 생성을 요청하는 프롬프트 전송
UI에서는 다음을 보여줘요:
- 실행된 Python 코드 (접었다 펼 수 있음)
- 생성된 이미지를 인라인으로 표시
- 파일 다운로드 링크 (CSV 등)
LiteLLM Python SDK
코드 인터프리터 실행하기
code_interpreter.py
import litellm
response = litellm.responses(
model="openai/gpt-5.6-terra",
input="Generate a bar chart of quarterly sales and save as PNG",
tools=[{"type": "code_interpreter"}]
)
print(response)
생성된 파일 콘텐츠 가져오기
코드 인터프리터 실행 후 생성된 파일을 조회해요:
get_file_content.py
import litellm
# 1. Run code interpreter
response = litellm.responses(
model="openai/gpt-5.6-terra",
input="Create a pie chart of market share and save as PNG",
tools=[{"type": "code_interpreter"}]
)
# 2. Extract container_id from response
container_id = response.output[0].container_id # e.g. "cntr_abc123..."
# 3. List files in container
files = litellm.list_container_files(
container_id=container_id,
custom_llm_provider="openai"
)
# 4. Download each file
for file in files.data:
content = litellm.containers.retrieve_container_file_content(
container_id=container_id,
file_id=file.id,
custom_llm_provider="openai"
)
with open(file.filename, "wb") as f:
f.write(content)
print(f"Downloaded: {file.filename}")
관련 문서 (Related)
- Containers API - 컨테이너 관리
- Container Files API - 컨테이너 내 파일 관리
- OpenAI Code Interpreter Docs - OpenAI 공식 문서