Function calling (tools)
Cara menggunakan function calling (tool use) dengan GetVibe API: parameter tools dan tool_choice serta percakapan tool multi-turn. Loop agen lengkap dalam Python dan curl.
Function calling (penggunaan tool) memungkinkan model meminta pemanggilan ke fungsi Anda dan menerima hasilnya kembali. Didukung pada semua model yang kompatibel dengan OpenAI.
Parameter
| Parameter | Tipe | Deskripsi |
|---|---|---|
tools | arrayGPT · Claude | Daftar tools. Setiap objek: { type: "function", function: { name, description, parameters } }. |
tool_choice | string | objectGPT · Claude | "auto" (model memutuskan), "none" (jangan panggil), "required" (wajib panggil), atau { type: "function", function: { name } } — sebuah tool tertentu. |
parallel_tool_calls | booleanGPT | true — model meminta beberapa tool sekaligus. Default true. Claude tidak mendukung flag ini tetapi melakukan panggilan paralel sendiri. |
Contoh: panggilan tunggal
curl https://getvibeapi.com/v1/chat/completions \
-H "Authorization: Bearer $GETVIBE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6.1-sol",
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Returns the current weather in a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City name" }
},
"required": ["city"]
}
}
}],
"tool_choice": "auto",
"messages": [{"role": "user", "content": "What is the weather in London?"}]
}'Multi-turn: mengirimkan kembali hasilnya
Setelah tool dipanggil, tambahkan pesan peran tool berisi hasilnya lalu kirim permintaan berikutnya.
# messages after the first response with tool_call:
messages = [
{"role": "user", "content": "What is the weather in London?"},
# model response with the tool call
{"role": "assistant", "tool_calls": msg.tool_calls, "content": None},
# the result of your function
{
"role": "tool",
"tool_call_id": msg.tool_calls[0].id,
"content": json.dumps({"city": "London", "temp": 18, "desc": "Cloudy"}),
},
]
resp2 = client.chat.completions.create(model="gpt-6.1-sol", tools=tools, messages=messages)
print(resp2.choices[0].message.content)
# → "It is currently 18°C and cloudy in London."Contoh respons (tool_call)
{
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"London\"}"
}
}]
},
"finish_reason": "tool_calls"
}]
}Kolom
tools dan tool_choice diterima dalam bentuk yang kompatibel dengan OpenAI; dukungan sebenarnya bergantung pada model yang dipilih. Jangan mengandalkan pass-through byte-per-byte atau kolom respons yang spesifik pada penyedia hulu.