개방형 모델의 함수 호출

함수 호출을 사용하면 모델이 외부 도구 또는 API를 호출해야 하는 시점을 식별한 다음 실행에 필요한 함수와 인수가 포함된 구조화된 데이터를 생성할 수 있습니다. Vertex AI Model as a Service (MaaS)에서 사용할 수 있는 모든 개방형 모델은 함수 호출을 지원합니다.

함수 호출 기능에 관한 자세한 개념 정보는 함수 호출 소개를 참고하세요.

함수 호출 사용

다음 사용 사례는 외부 기상 API를 통합하여 지정된 위치의 현재 날씨를 확인합니다. 이 코드는 REST 호출을 사용하여 두 도시의 날씨를 비교합니다. 이때 tools 내 스키마에 선언된 get_current_weather 함수를 사용합니다.

{
  "model": "MODEL_NAME",
  "messages": [
    {
      "role": "user",
      "content": "Which city has a higher temperature, Boston or new Delhi, and by how much in F?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}

요청을 수신한 후 모델은 get_current_weather 함수의 출력이 필요하다고 판단하고 tool_calls 내에서 필요한 함수와 인수를 제공합니다.

보스턴의 날씨를 가져오는 함수 호출:

{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"} "id":"get_current_weather","type":"function"}

뉴델리의 날씨를 가져오는 함수 호출:

{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}

상세 예시

이전 예제를 기반으로 get_current_weather 함수를 사용하여 기상 정보를 가져온 후 예상되는 전체 출력은 다음과 같습니다.

{
  "model": "MODEL_NAME",
  "messages": [
    {
      "role": "user",
      "content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?"
    },
    {
      "role": "assistant",
      "content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.",
      "tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}]
    },
    {
      "role": "tool",
      "content": "The temperature in Boston is 75 degrees Fahrenheit.",
      "tool_call_id": "get_weather"
    },
    {
      "role": "tool",
      "content": "The temperature in New Delhi is 50 degrees Fahrenheit.",
      "tool_call_id": "get_weather"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"]
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}

외부 get_current_weather 함수 호출을 통해 검색된 정보를 받은 후 모델은 두 tool 응답의 정보를 합성하여 사용자의 질문에 답할 수 있습니다.

다음은 모델 출력의 예입니다.

{
 "choices": [
  {
   "finish_reason": "stop",
   "index": 0,
   "logprobs": null,
   "message": {
    "content": "Based on the current weather data:\n\n- **Boston, MA**: 75°F
    \n- **New Delhi, India**: 50°F  \n\n**Comparison**:
    \nBoston is **25°F warmer** than New Delhi.  \n\n**Answer**:
    \nBoston has a higher temperature than New Delhi by 25 degrees Fahrenheit.",
    "role": "assistant"
   }
  }
 ],
 "created": 1750450289,
 "id": "2025-06-20|13:11:29.240295-07|6.230.75.101|-987540014",
 "model": "MODEL_NAME",
 "object": "chat.completion",
 "system_fingerprint": "",
 "usage": {
  "completion_tokens": 66,
  "prompt_tokens": 217,
  "total_tokens": 283
 }
}