Pemanggilan fungsi untuk model terbuka

Panggilan fungsi memungkinkan model mengidentifikasi kapan harus memanggil alat atau API eksternal, lalu membuat data terstruktur yang berisi fungsi dan argumen yang diperlukan untuk eksekusi. Semua model terbuka yang tersedia di Vertex AI Model as a Service (MaaS) mendukung panggilan fungsi.

Untuk mengetahui informasi konseptual selengkapnya tentang kemampuan panggilan fungsi, lihat Pengantar panggilan fungsi.

Menggunakan panggilan fungsi

Kasus penggunaan berikut mengintegrasikan API meteorologi eksternal untuk memeriksa cuaca saat ini di lokasi tertentu. Kode ini menggunakan panggilan REST untuk membandingkan cuaca di dua kota, menggunakan fungsi get_current_weather yang dideklarasikan dalam skema di tools.

{
  "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"
}

Setelah menerima permintaan, model menentukan bahwa model memerlukan output dari fungsi get_current_weather, dan menyediakan fungsi serta argumen yang diperlukan dalam tool_calls.

Panggilan fungsi untuk mengambil cuaca di Boston:

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

Panggilan fungsi untuk mengambil cuaca di New Delhi:

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

Contoh mendetail

Berdasarkan contoh sebelumnya, berikut adalah output lengkap yang dapat Anda harapkan setelah menggunakan fungsi get_current_weather untuk mengambil informasi meteorologi.

{
  "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"
}

Setelah menerima informasi yang diambil melalui panggilan fungsi get_current_weather eksternal, model dapat menyintesis informasi dari dua respons tool dan menjawab pertanyaan pengguna.

Berikut adalah contoh tampilan output model tersebut:

{
 "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
 }
}