Function/Tool Calling: Şema Tasarımı, Validation, Hata Yönetimi
Tool calling'in API perspektifinden detayı. Tool şeması, parameter validation, error recovery, paralel tool çağırımları.
Şükrü Yusuf KAYA
13 dakikalık okuma
OrtaTool / Function Calling
Modern LLM'ler dış fonksiyonları çağırabilir. Bu, agent'ların temel building block'u.
Tool Şeması#
json
{ "name": "get_weather", "description": "Bir şehrin güncel hava durumunu döndürür.", "input_schema": { "type": "object", "properties": { "city": { "type": "string", "description": "Türkçe şehir adı (örn. İstanbul)" }, "units": { "type": "string", "enum": ["celsius", "fahrenheit"], "default": "celsius" } }, "required": ["city"] }}Tool şema şablonu
'description' kritik! Model bu açıklamayı okuyarak hangi tool'u ne zaman çağıracağına karar veriyor. Belirsiz description → yanlış tool seçimi.
python
# Tam tool calling örneğiimport os, jsonfrom anthropic import Anthropicfrom dotenv import load_dotenvload_dotenv() client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY")) # Sahte tool implementasyonlarıdef get_weather(city: str, units: str = "celsius") -> dict: return {"city": city, "temp": 18, "condition": "yağmurlu", "units": units} def get_currency(base: str, target: str) -> dict: rates = {"USD-TRY": 35.4, "EUR-TRY": 38.2} return {"rate": rates.get(f"{base}-{target}", 0)} TOOLS = [ { "name": "get_weather", "description": "Bir şehrin güncel hava durumunu döndürür. Örnek: get_weather(city='İstanbul')", "input_schema": { "type": "object", "properties": { "city": {"type": "string"}, "units": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["city"] } }, { "name": "get_currency", "description": "İki para birimi arasındaki güncel kuru döndürür.", "input_schema": { "type": "object", "properties": { "base": {"type": "string", "description": "USD, EUR, TRY"}, "target": {"type": "string"} }, "required": ["base", "target"] } }] # 1. İlk istekmessages = [{"role": "user", "content": "İstanbul'da hava nasıl ve 1 USD kaç TL?"}]r = client.messages.create( model="claude-sonnet-4-6", max_tokens=1000, tools=TOOLS, messages=messages) # 2. Tool çağrılarını işlewhile r.stop_reason == "tool_use": messages.append({"role": "assistant", "content": r.content}) tool_results = [] for block in r.content: if block.type == "tool_use": tool_name = block.name tool_input = block.input print(f"→ Calling {tool_name}({tool_input})") # Tool'u çağır if tool_name == "get_weather": result = get_weather(**tool_input) elif tool_name == "get_currency": result = get_currency(**tool_input) tool_results.append({ "type": "tool_result", "tool_use_id": block.id, "content": json.dumps(result) }) messages.append({"role": "user", "content": tool_results}) r = client.messages.create( model="claude-sonnet-4-6", max_tokens=1000, tools=TOOLS, messages=messages ) # 3. Final cevapprint("\n=== Final ===")print(r.content[0].text) Multi-tool, multi-turn tool use loop.
Hata Yönetimi#
Yorumlar & Soru-Cevap
(0)Yorum yazmak için giriş yap.
Yorumlar yükleniyor...
İlgili İçerikler
1. Temeller — Yapay Zekâ ve LLM'lere Giriş
Bu Eğitim Hakkında ve Verimli Çalışma Yöntemi
Öğrenmeye Başla1. Temeller — Yapay Zekâ ve LLM'lere Giriş
Yapay Zekâ → Üretken AI → LLM: Bağlamsal Harita
Öğrenmeye Başla1. Temeller — Yapay Zekâ ve LLM'lere Giriş
LLM'ler Aslında Nasıl Düşünür? (Token, Embedding, Attention)
Öğrenmeye BaşlaBağlantılı Pillar Konular