Skip to content

Function Calling: Yapılandırılmış Tool Kullanımı

Modele 'şu fonksiyonları çağırabilirsin' deyip, hangisini çağıracağını model seçtirme. Agent'ların temeli.

Şükrü Yusuf KAYA
11 min read
Advanced
Function Calling: Yapılandırılmış Tool Kullanımı

Function Calling = "Modelin elleri"#

Model'e "şu fonksiyonları çağırabilirsin" deyip, gerektiğinde hangisini çağıracağını model seçer. Sen JSON-formatında parametreleri alırsın → kendi koduna gönderirsin → sonucu modele döndürürsün.
Bu pattern agent'ların temelidir.
javascript
import OpenAI from "openai";
const openai = new OpenAI();
 
const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "Şehir için anlık hava durumu",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "Şehir adı (örn. İstanbul)" },
unit: { type: "string", enum: ["celsius", "fahrenheit"] }
},
required: ["city"]
}
}
},
{
type: "function",
function: {
name: "send_email",
description: "Bir alıcıya e-posta gönder",
parameters: {
type: "object",
properties: {
to: { type: "string", description: "Alıcı email" },
subject: { type: "string" },
body: { type: "string" }
},
required: ["to", "subject", "body"]
}
}
}
];
 
const messages = [
{ role: "user", content: "İstanbul'da hava nasıl, sonra patrona e-posta at." }
];
 
const response = await openai.chat.completions.create({
model: "gpt-5",
messages,
tools,
tool_choice: "auto", // model otomatik seçer
});
 
const msg = response.choices[0].message;
 
if (msg.tool_calls) {
for (const call of msg.tool_calls) {
console.log("Çağırılan fonksiyon:", call.function.name);
console.log("Argümanlar:", JSON.parse(call.function.arguments));
// → kendi kodunda fonksiyonu çalıştır
// → sonucu yeni mesaj olarak geri döndür
}
}
Function calling — 2 tool tanımı, model gerektiğinde çağırır.
🔄 Çağrı döngüsü
Function calling tek seferlik değil. Model fonksiyon çağırır → sen çalıştırırsın → sonucu mesaj olarak geri verirsin → model devam eder. Bazen 5-10 turluk zincirleme. Bu zincire agent loop denir; Modül 12 ve sonrasında ele alıyoruz.
javascript
// Tam agent loop
async function runAgent(userMessage) {
const messages = [{ role: "user", content: userMessage }];
 
while (true) {
const response = await openai.chat.completions.create({
model: "gpt-5",
messages,
tools,
tool_choice: "auto",
});
 
const msg = response.choices[0].message;
messages.push(msg);
 
// Eğer tool çağrı yoksa, modelin son cevabı bu — döngüyü bitir
if (!msg.tool_calls) {
return msg.content;
}
 
// Her tool çağrıyı çalıştır
for (const call of msg.tool_calls) {
const fnName = call.function.name;
const args = JSON.parse(call.function.arguments);
const result = await myFunctions[fnName](args);
 
// Sonucu mesaj olarak ekle
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
});
}
// Loop tekrar başlar — model sonraki adımı düşünür
}
}
Tam agent loop — model bitirene kadar fonksiyon çağrıları.

Özet#

✓ Function calling = modele "şu fonksiyonları kullanabilirsin" deme ✓ Model JSON arg üretir, sen çalıştırırsın, sonucu döndürürsün ✓ Agent loop: model bitirene kadar zincirleme çağrılar
Sıradaki ders: Structured Outputs — JSON Schema ile garantili format.

Yorumlar & Soru-Cevap

(0)
Yorum yazmak için giriş yap.
Yorumlar yükleniyor...

Related Content

Connected pillar topics

Pillar topics this article maps to