استدعاء الدوال واستخدام الأدوات
تعلّم تمكين LLMs من التفاعل مع الأدوات وواجهات API الخارجية عبر تعليمها استدعاء دوال محددة استنادًا إلى مطالبات المستخدم.
استدعاء الدوال واستخدام الأدوات درس مجاني في Prompt Engineering & LLM Optimization for Developers على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Prompt Engineering & LLM Optimization for Developers، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Prompt Engineering & LLM Optimization for Developers 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
LLMs & External Tools
Large Language Models (LLMs) are amazing at understanding and generating text. However, they typically don't have direct access to:
- Real-time information: Like today's weather or current stock prices.
- External actions: Such as sending an email or booking a flight.
- Complex computations: Beyond simple arithmetic.
This is where Function Calling comes in!
Bridging the Gap with Tools
Function calling allows LLMs to interact with the outside world by letting them 'call' external tools or APIs. It bridges the gap between the LLM's language abilities and real-world actions.
Think of it as giving the LLM a set of specialized gadgets it can recommend using when needed.
How it Works: The LLM's Role
When you use function calling, the LLM doesn't *execute* code itself. Instead, it acts like a smart planner or router:
- It analyzes your prompt.
- It identifies if any defined tools could help fulfill your request.
- If so, it suggests which tool to use and what arguments (inputs) to pass to it.
Your application then takes this suggestion and performs the actual action.
Defining Your Tools (Schema)
Before an LLM can recommend a tool, it needs to know what tools are available. You provide a 'schema' – a description of each function/tool your application can perform.
This schema tells the LLM:
- The function's name (e.g.,
get_current_weather). - A clear description of what the function does.
- Its required parameters (inputs) and their types (e.g.,
location: string).
Example: A Weather Tool Definition
Here's how you might conceptually define a simple weather tool for an LLM. This description helps the LLM understand *when* and *how* to use it.
function get_current_weather(location: string, unit: "celsius" | "fahrenheit" = "fahrenheit") {
// Gets the current weather for a given location.
// Returns temperature, conditions, and humidity.
}The Interaction Flow
Let's trace a typical interaction with function calling:
- User Prompt: You ask the LLM a question (e.g., "What's the weather in Paris?").
- LLM Suggestion: The LLM sees the weather tool definition and suggests calling
get_current_weather(location="Paris"). - Your App Action: Your application receives this suggestion, calls a real weather API, and gets the data.
- LLM Final Response: Your app sends the weather data back to the LLM, which then generates a natural language response for you.
Step-by-Step Scenario
Imagine you have a get_stock_price tool defined. Here's a flow:
- User: "What's the current price of Apple stock?"
- LLM (internally): "Aha! The user wants a stock price. I have a
get_stock_pricetool that takes asymbol." - LLM Output:
tool_call: get_stock_price(symbol="AAPL") - Your App: Executes the
get_stock_pricefunction with "AAPL". - Your App: Gets "$175.50" and sends it back to the LLM.
- LLM: "The current price of Apple (AAPL) stock is $175.50."
Benefits of Function Calling
Function calling dramatically enhances LLM capabilities:
- Access to Real-time Data: Get up-to-date information that the LLM's training data might not have.
- Perform Actions: Enable LLMs to trigger real-world processes.
- Reduce Hallucinations: Ground LLM responses in factual, external data, making them more reliable.
- Complex Logic: Delegate complex calculations or data retrieval to robust external systems.
Common Use Cases
Function calling is versatile and can be applied to many scenarios:
- Data Retrieval: Fetching information from databases, APIs, or files.
- Calculations: Using a calculator tool for precise math.
- Sending Notifications: Integrating with email or messaging services.
- Database Interactions: Querying or updating records.
- Home Automation: Turning on lights or adjusting thermostats.
Quick Check: LLM's Role
You've defined a tool for your LLM. A user asks a question that requires this tool.
Recap: Tools & Smarter LLMs
You've learned about Function Calling, a powerful technique that allows LLMs to interact with external tools and APIs. By defining schemas for your functions, you enable LLMs to act as smart planners, suggesting relevant actions and arguments based on user prompts.
This capability opens up a world of possibilities, making LLM-powered applications more dynamic, factual, and capable of real-world interaction.
الأسئلة الشائعة
هل درس «استدعاء الدوال واستخدام الأدوات» مجاني؟
نعم — نص درس «استدعاء الدوال واستخدام الأدوات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Prompt Engineering & LLM Optimization for Developers، انتقل إلى CoddyKit PRO. تتضمن دورة Prompt Engineering & LLM Optimization for Developers 4 دروس في المجموع.
ماذا ستتعلم في «استدعاء الدوال واستخدام الأدوات»؟
تعلّم تمكين LLMs من التفاعل مع الأدوات وواجهات API الخارجية عبر تعليمها استدعاء دوال محددة استنادًا إلى مطالبات المستخدم. تتمرن على Prompt Engineering & LLM Optimization for Developers مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Prompt Engineering & LLM Optimization for Developers؟
لا تُشترط خبرة سابقة. Prompt Engineering & LLM Optimization for Developers على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «استدعاء الدوال واستخدام الأدوات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Prompt Engineering & LLM Optimization for Developers هذا؟
نعم. كل درس في Prompt Engineering & LLM Optimization for Developers يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التوليد المعزّز بالاسترجاع (RAG)
- استدعاء الدوال واستخدام الأدوات
- بناء وكلاء LLM بسيطين
- بث استجابات LLM إلى المستخدمين