Appel de fonctions et utilisation d’outils
Apprenez à permettre aux LLM d’interagir avec des outils et des API externes en leur apprenant à appeler des fonctions précises selon les prompts des utilisateurs.
Appel de fonctions et utilisation d’outils est une leçon Prompt Engineering & LLM Optimization for Developers gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Prompt Engineering & LLM Optimization for Developers, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Prompt Engineering & LLM Optimization for Developers comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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.
Questions Fréquemment Posées
La leçon « Appel de fonctions et utilisation d’outils » est-elle gratuite ?
Oui — le texte complet de « Appel de fonctions et utilisation d’outils » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Prompt Engineering & LLM Optimization for Developers, passe à CoddyKit PRO. Le cours Prompt Engineering & LLM Optimization for Developers comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Appel de fonctions et utilisation d’outils » ?
Apprenez à permettre aux LLM d’interagir avec des outils et des API externes en leur apprenant à appeler des fonctions précises selon les prompts des utilisateurs. Tu pratiques Prompt Engineering & LLM Optimization for Developers avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Prompt Engineering & LLM Optimization for Developers ?
Aucune expérience préalable n'est requise. Prompt Engineering & LLM Optimization for Developers sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.
Combien de temps prend la leçon « Appel de fonctions et utilisation d’outils » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Prompt Engineering & LLM Optimization for Developers ?
Oui. Chaque leçon Prompt Engineering & LLM Optimization for Developers inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Génération augmentée par récupération (RAG)
- Appel de fonctions et utilisation d’outils
- Créer des agents LLM simples
- Diffuser les réponses des LLM aux utilisateurs