Panggilan fungsi memungkinkan Anda menentukan fungsi kustom dan memberikan kemampuan kepada LLM untuk memanggilnya guna mengambil informasi real-time atau berinteraksi dengan sistem eksternal seperti database SQL atau alat layanan pelanggan.
Untuk mengetahui informasi konseptual selengkapnya tentang panggilan fungsi, lihat Pengantar panggilan fungsi.
Menggunakan panggilan fungsi
Contoh berikut menunjukkan cara menggunakan panggilan fungsi.
Python
Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Panduan memulai Vertex AI menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Python Vertex AI.
Untuk melakukan autentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.
Sebelum menjalankan contoh ini, pastikan untuk menetapkan variabel lingkungan OPENAI_BASE_URL
.
Untuk mengetahui informasi selengkapnya, lihat Autentikasi dan kredensial.
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="MODEL", messages=[ {"role": "user", "content": "CONTENT"} ], tools=[ { "type": "function", "function": { "name": "FUNCTION_NAME", "description": "FUNCTION_DESCRIPTION", "parameters": PARAMETERS_OBJECT, } } ], tool_choice="auto", )
- MODEL: Nama model yang ingin Anda gunakan,
misalnya
qwen/qwen3-next-80b-a3b-instruct-maas
. - CONTENT: Perintah pengguna yang akan dikirim ke model.
- FUNCTION_NAME: Nama fungsi yang akan dipanggil.
- FUNCTION_DESCRIPTION: Deskripsi fungsi.
- PARAMETERS_OBJECT: Kamus yang menentukan parameter fungsi, misalnya:
{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}
REST
Setelah Anda menyiapkan lingkungan, Anda dapat menggunakan REST untuk menguji perintah teks. Contoh berikut mengirim permintaan ke endpoint model penayang.
Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:
- PROJECT_ID: ID project Google Cloud Anda.
- LOCATION: Region yang mendukung model terbuka.
- MODEL: Nama model yang ingin Anda gunakan,
misalnya
qwen/qwen3-next-80b-a3b-instruct-maas
. - CONTENT: Perintah pengguna yang akan dikirim ke model.
- FUNCTION_NAME: Nama fungsi yang akan dipanggil.
- FUNCTION_DESCRIPTION: Deskripsi fungsi.
- PARAMETERS_OBJECT: Objek skema JSON yang menentukan parameter fungsi, misalnya:
{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}
Metode HTTP dan URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions
Isi JSON permintaan:
{ "model": "MODEL", "messages": [ { "role": "user", "content": "CONTENT" } ], "tools": [ { "type": "function", "function": { "name": "FUNCTION_NAME", "description": "FUNCTION_DESCRIPTION", "parameters": PARAMETERS_OBJECT } } ], "tool_choice": "auto" }
Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:
curl
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions"
PowerShell
Simpan isi permintaan dalam file bernama request.json
,
dan jalankan perintah berikut:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions" | Select-Object -Expand Content
Anda akan menerima kode status yang menandakan proses berhasil (2xx), dan sebuah respons kosong.
Contoh
Berikut adalah output lengkap yang dapat Anda harapkan setelah menggunakan fungsi
get_current_weather
untuk mengambil informasi meteorologi.
Python
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="qwen/qwen3-next-80b-a3b-instruct-maas", 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_current_weather" }, { "role": "tool", "content": "The temperature in New Delhi is 50 degrees Fahrenheit.", "tool_call_id": "get_current_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" )
curl
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/sample-project/locations/us-central1/endpoints/openapi/chat/completions -d \ '{ "model": "qwen/qwen3-next-80b-a3b-instruct-maas", "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_current_weather" }, { "role": "tool", "content": "The temperature in New Delhi is 50 degrees Fahrenheit.", "tool_call_id": "get_current_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" }'
{ "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": "qwen/qwen3-next-80b-a3b-instruct-maas", "object": "chat.completion", "system_fingerprint": "", "usage": { "completion_tokens": 66, "prompt_tokens": 217, "total_tokens": 283 } }
Panduan khusus model
Bagian berikut memberikan panduan khusus model untuk panggilan fungsi.
DeepSeek
Model DeepSeek tidak berfungsi sebaik panggilan fungsi jika Anda menggunakan perintah sistem. Untuk performa optimal, hilangkan perintah sistem saat menggunakan panggilan fungsi dengan model DeepSeek.
Llama
meta/llama3-405b-instruct-maas
tidak mendukung tool_choice = 'required'
.
OpenAI
Saat menggunakan openai/gpt-oss-120b-instruct-maas
dan
openai/gpt-oss-20b-instruct-maas
, tempatkan definisi alat Anda di
prompt sistem untuk performa yang optimal. Contoh:
{"messages": [ {"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required:\n..."}, {"role": "user", "content": "What's the weather like in Boston?"}, ... ]}
Model ini tidak mendukung tool_choice = 'required'
atau panggilan alat bernama.
Qwen
Model Qwen memiliki performa terbaik jika tool_choice
ditetapkan secara eksplisit ke auto
atau
none
. Jika tool_choice
tidak disetel, performa model mungkin tidak
sebaik biasanya.
Langkah berikutnya
- Pelajari Output terstruktur.
- Pelajari Pemikiran.
- Pelajari Prediksi batch.