Pemanggilan fungsi untuk model terbuka

Panduan ini menunjukkan cara menggunakan panggilan fungsi dengan model terbuka di Vertex AI, yang mencakup topik berikut:

Diagram berikut merangkum alur kerja keseluruhan:

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

Untuk menggunakan panggilan fungsi, Anda memberikan serangkaian definisi alat ke model dalam permintaan Anda. Model menggunakan definisi ini untuk menentukan apakah perlu memanggil fungsi untuk menjawab kueri Anda.

Contoh berikut menunjukkan permintaan yang meminta model untuk membandingkan cuaca di dua kota. Permintaan menyertakan array tools dengan deklarasi fungsi untuk 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"
}

Setelah menerima permintaan, model akan menentukan bahwa model perlu memanggil fungsi get_current_weather untuk kedua kota. Tindakan ini menampilkan respons yang berisi array tool_calls dengan argumen yang sesuai untuk setiap panggilan fungsi.

Cuplikan berikut menunjukkan dua objek panggilan fungsi yang ditampilkan model dalam array tool_calls.

Boston

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

New Delhi

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

Contoh mendetail

Bagian ini melanjutkan contoh sebelumnya dan menunjukkan percakapan multi-turn yang lengkap.

Setelah aplikasi Anda menjalankan fungsi berdasarkan tool_calls model, Anda mengirim permintaan lain ke model. Permintaan baru ini harus menyertakan histori percakapan dan hasil panggilan fungsi. Hasilnya diberikan dalam pesan dengan peran tool.

Berikut adalah contoh payload permintaan yang menyertakan hasil alat:

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

Model menggunakan hasil fungsi untuk menjawab pertanyaan awal Anda. Berikut adalah contoh respons akhir dari model:

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