콘텐츠로 이동
Search

OpenCodeInterpreter

Agentic Tool-Use Example

vLLM + Qwen 3.6을 Claude Code처럼 작동시키는 것은 agentic tool-use 아키텍처로 구현하는 것이 핵심입니다.

# vLLM을 백엔드으로 하는 OpenCodeInterpreter 래퍼
from openai import OpenAI
import json
import subprocess
import os

client = OpenAI(
    api_key="ignored",  # vLLM doesn't require real key
    base_url="http://vllm-dgx-spark:8000/v1"  # 당신의 vLLM 엔드포인트
)

# Tool definitions - Claude Code와 유사하게
TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "execute_python",
            "description": "Execute Python code",
            "parameters": {
                "type": "object",
                "properties": {
                    "code": {"type": "string"}
                },
                "required": ["code"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "read_file",
            "description": "Read file contents",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {"type": "string"}
                },
                "required": ["path"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "write_file",
            "description": "Write to file",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {"type": "string"},
                    "content": {"type": "string"}
                },
                "required": ["path", "content"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "bash",
            "description": "Execute shell commands",
            "parameters": {
                "type": "object",
                "properties": {
                    "command": {"type": "string"}
                },
                "required": ["command"]
            }
        }
    }
]

def execute_tool(tool_name, tool_input):
    """Tool 실행 엔진"""
    if tool_name == "execute_python":
        result = subprocess.run(
            ["python", "-c", tool_input["code"]],
            capture_output=True,
            text=True,
            timeout=30
        )
        return result.stdout + result.stderr

    elif tool_name == "read_file":
        with open(tool_input["path"], "r") as f:
            return f.read()

    elif tool_name == "write_file":
        with open(tool_input["path"], "w") as f:
            f.write(tool_input["content"])
        return f"File written: {tool_input['path']}"

    elif tool_name == "bash":
        result = subprocess.run(
            tool_input["command"],
            shell=True,
            capture_output=True,
            text=True,
            timeout=30
        )
        return result.stdout + result.stderr

def agentic_loop(user_message, max_iterations=10):
    """Claude Code와 유사한 agentic 루프"""
    messages = [{"role": "user", "content": user_message}]

    for iteration in range(max_iterations):
        # vLLM 호출
        response = client.chat.completions.create(
            model="qwen-3.6-27b",  # 모델 이름
            messages=messages,
            tools=TOOLS,
            tool_choice="auto",
            temperature=0.7,
            max_tokens=2048
        )

        # 응답 처리
        if response.stop_reason == "end_turn":
            return response.choices[0].message.content

        # Tool use 처리
        tool_calls = response.choices[0].message.tool_calls
        if not tool_calls:
            return response.choices[0].message.content

        # Tool 결과 수집
        messages.append({"role": "assistant", "content": response.choices[0].message.content})

        for tool_call in tool_calls:
            tool_result = execute_tool(
                tool_call.function.name,
                json.loads(tool_call.function.arguments)
            )
            messages.append({
                "role": "user",
                "content": f"Tool result for {tool_call.function.name}:\n{tool_result}"
            })

    return "Max iterations reached"

# 사용 예시
result = agentic_loop("Python으로 피보나치 함수를 작성하고 테스트해줘")
print(result)

See also

lastmod 2026-09-04created 2026-09-04author Zer0 sitemap