도구가 있는 OpenAI Responses 클라이언트

도구가 있는 OpenAI Responses 클라이언트 (OpenAI Responses Client With Tools)

OpenAI의 Responses API를 사용해 vLLM 서버에서 함수 호출을 수행하는 예제입니다. client.responses.create로 모델이 도구를 호출하게 하고, 반환된 function_call을 실제 함수로 실행한 뒤, 그 결과(function_call_output)를 다시 넣어 최종 답변을 받는 대화 흐름을 보여줍니다.

출처: 문서

원본: https://github.com/vllm-project/vllm/blob/main/examples/tool_calling/openai_responses_client_with_tools.py

본문

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Set up this example by starting a vLLM OpenAI-compatible server with tool call
options enabled.
Reasoning models can be used through the Responses API as seen here
https://platform.openai.com/docs/api-reference/responses
For example:
vllm serve Qwen/Qwen3-1.7B --reasoning-parser qwen3 \
      --structured-outputs-config.backend xgrammar \
      --enable-auto-tool-choice --tool-call-parser hermes
"""

import json

from openai import OpenAI

def get_weather(latitude: float, longitude: float) -> str:
    """Mock function to simulate getting weather data.
    In a real application, this would call an external weather API.
    """
    return f"Current temperature at ({latitude}, {longitude}) is 20°C."

tools = [
    {
        "type": "function",
        "name": "get_weather",
        "description": "Get current temperature for provided coordinates in celsius.",
        "parameters": {
            "type": "object",
            "properties": {
                "latitude": {"type": "number"},
                "longitude": {"type": "number"},
            },
            "required": ["latitude", "longitude"],
            "additionalProperties": False,
        },
        "strict": True,
    }
]

input_messages = [
    {"role": "user", "content": "What's the weather like in Paris today?"}
]

def main():
    base_url = "http://0.0.0.0:8000/v1"
    client = OpenAI(base_url=base_url, api_key="empty")
    model = client.models.list().data[0].id
    response = client.responses.create(
        model=model, input=input_messages, tools=tools, tool_choice="required"
    )

    for out in response.output:
        if out.type == "function_call":
            print("Function call:", out.name, out.arguments)
            tool_call = out
    args = json.loads(tool_call.arguments)
    result = get_weather(args["latitude"], args["longitude"])

    input_messages.append(tool_call)  # append model's function call message
    input_messages.append(
        {  # append result message
            "type": "function_call_output",
            "call_id": tool_call.call_id,
            "output": str(result),
        }
    )
    response_2 = client.responses.create(
        model=model,
        input=input_messages,
        tools=tools,
    )
    print(response_2.output_text)

if __name__ == "__main__":
    main()

이 예제를 실행하기 전에 도구 호출 옵션을 켠 vLLM OpenAI 호환 서버를 시작해야 합니다. 추론(reasoning) 모델은 Responses API를 통해 사용할 수 있습니다(위 모듈 주석의 vllm serve 명령 참고).

더 알아보기 (Learn more)