Function Calling

استدعاء الدوال

Intermediateai-ml1 min read
tool callingtool usefunction usetools

Definition

A feature that lets an LLM request your code to run a specific function and return the result back to the LLM.

ميزة تسمح للنموذج اللغوي الكبير بطلب تشغيل دالة معينة في كودك وإعادة النتيجة إلى النموذج.

Why It Matters

Function calling is what makes AI assistants actually useful beyond answering questions. It is what allows an AI to look up your account, book a reservation, trigger a refund, or query a database. Every real-world AI product with actions uses function calling.

استدعاء الدوال هو ما يجعل مساعدي الذكاء الاصطناعي مفيدين فعلاً بما يتجاوز الإجابة على الأسئلة. هذا ما يسمح للذكاء الاصطناعي بالبحث عن حسابك وحجز موعد وتفعيل استرداد مالي أو الاستعلام عن قاعدة بيانات. كل منتج ذكاء اصطناعي للعالم الحقيقي مع إجراءات يستخدم استدعاء الدوال.

Full Definition

Function calling (also called tool use) is a protocol where you describe functions to an LLM, and the LLM can respond with a structured request to call one of those functions rather than generating plain text. Your code executes the function, gets the result, and sends it back to the LLM for the next step. This is how agents take actions: they call a `search_web` function, get back search results, then generate a response based on real data. The LLM does not actually execute code — it only generates the function call structure; your code does the execution.
استدعاء الدوال (المعروف أيضاً باستخدام الأدوات) هو بروتوكول تصف فيه الدوال للنموذج اللغوي الكبير، ويمكن للنموذج الردّ بطلب منظّم لاستدعاء إحدى تلك الدوال بدلاً من توليد نص عادي. ينفّذ كودك الدالة ويحصل على النتيجة ويُعيدها إلى النموذج للخطوة التالية. هكذا يتخذ الوكلاء إجراءات: يستدعون دالة `search_web` ويحصلون على نتائج البحث ثم يُنشئون استجابة بناءً على بيانات حقيقية. النموذج لا يُنفّذ الكود فعلياً — بل يُنشئ فقط بنية استدعاء الدالة؛ كودك هو من يقوم بالتنفيذ.

Example Usage

You define: { name: "get_weather", description: "Get current weather for a city", parameters: { city: "string" } }. User says "What's the weather in Dubai?" The LLM responds with a tool call: { name: "get_weather", arguments: { city: "Dubai" } }. Your code calls the weather API and returns "Dubai: 38°C, sunny". The LLM then generates: "It's currently 38°C and sunny in Dubai."

تُعرّف: { name: 'get_weather', description: 'احصل على الطقس الحالي لمدينة', parameters: { city: 'string' } }. يقول المستخدم 'ما حال الطقس في دبي؟' يردّ النموذج باستدعاء أداة: { name: 'get_weather', arguments: { city: 'Dubai' } }. يستدعي كودك واجهة برمجة الطقس ويُعيد 'دبي: 38°م، مشمس'. يُنشئ النموذج بعدها: 'الطقس في دبي حالياً 38°م ومشمس.'

Knowledge Graph

Avoid these mistakes when using Function Calling:

1

Writing vague function descriptions — the LLM uses the description to decide when to call the function; vague descriptions lead to wrong calls

2

Not validating function arguments from the LLM before executing — the LLM may hallucinate invalid arguments

3

Giving the LLM access to destructive functions (delete, payment) without a confirmation step

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Function Calling.

Explain:
1. What is Function Calling and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Writing vague function descriptions — the LLM uses the description to decide when to call the function; vague descriptions lead to wrong calls, Not validating function arguments from the LLM before executing — the LLM may hallucinate invalid arguments, Giving the LLM access to destructive functions (delete, payment) without a confirmation step
5. Best practices and production tips

Official Resources