İçeriğe geç

Tool Use API: Fonksiyon Çağırma Pratiği

Modül 7'deki tool use'u API üzerinden bitir: tam loop, paralel tool, error feedback ve schema doğrulama.

Şükrü Yusuf KAYA
12 dakikalık okuma
Orta
Tool use loop API perspektifi

Tam Loop

python
import json
 
def run_tool(name, args):
if name == "get_weather":
return {"temp_c": 18, "cond": "yağmurlu"}
raise ValueError(f"Unknown tool: {name}")
 
messages = [{"role":"user","content":"İstanbul'da bugün ne giyeyim?"}]
tools = [{
"name":"get_weather",
"description":"...",
"input_schema":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}
}]
 
while True:
resp = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=messages,
)
if resp.stop_reason != "tool_use":
print(resp.content[0].text)
break
 
# Modelin tüm tool çağrılarını işle
tool_results = []
for block in resp.content:
if block.type == "tool_use":
try:
result = run_tool(block.name, block.input)
tool_results.append({
"type":"tool_result",
"tool_use_id": block.id,
"content": json.dumps(result, ensure_ascii=False),
})
except Exception as e:
tool_results.append({
"type":"tool_result",
"tool_use_id": block.id,
"is_error": True,
"content": str(e),
})
 
# Konuşmaya assistant + user(tool_result) ekle
messages.append({"role":"assistant","content": resp.content})
messages.append({"role":"user","content": tool_results})
Tool use tam loop — error feedback dahil.
Boşluk doldur · text
Tool çıktısını döndürürken tipik tip _____ , id alanı ise _____ olarak gönderilir. Hata olduğunda _____ true ile feedback ver.

Sık Sorulan Sorular

Tool input streaming'de gelir; sonuçları sen ürettiğin için onları stream etmen gerekmiyor — fakat genelde gelen son mesajı stream ettirirsin.

Yorumlar & Soru-Cevap

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

İlgili İçerikler

Bağlantılı Pillar Konular

Bu yazının bağlandığı pillar konular